longctx-bench 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +44 -0
- data/exe/longctx-bench +31 -0
- data/lib/longctx/bench.rb +29 -0
- data/lib/longctx/client.rb +56 -0
- data/lib/longctx/prompt.rb +41 -0
- data/lib/longctx/version.rb +5 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0f98993121e27fa709664d7599ea7a7cbf0028c4c6d5d9c5c276b2a937cfce4a
|
|
4
|
+
data.tar.gz: 87f7967b4b12c7f0af6ff631e73d4184f13aaeae280263bde86c3e5d463a976e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d322ef706527530288850d3b69a17fec5711e9a34cb26ce3bd6780a453385e961aa80f506c464cc5d667efd067890e854412af7ab543bb6dd606a55ae135650f
|
|
7
|
+
data.tar.gz: 03f3b353bef3b0be7dce6c838a3d9f7c975c3a7f1e5ddb03281cd9b0fd896e21ceae41fd57cff8461ce4045d58449abe6b2dbcf0ea51b3fd5009d0b8a43ae05e
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Rivera
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# longctx-bench
|
|
2
|
+
|
|
3
|
+
A dependency-free Ruby harness for measuring how OpenAI-compatible chat
|
|
4
|
+
endpoints behave under long-context continuation loads.
|
|
5
|
+
|
|
6
|
+
It builds deterministic prompts of a target approximate token budget
|
|
7
|
+
(~4 chars/token), sends them as a single user message, and records the HTTP
|
|
8
|
+
status, latency, token usage and the first 200 characters of the answer.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
gem install longctx-bench
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
export LONGCTX_BASE_URL=https://api.example.com
|
|
17
|
+
export LONGCTX_API_KEY=sk-...
|
|
18
|
+
longctx-bench --model example-model --tokens 100000 --runs 1 --out run.json
|
|
19
|
+
|
|
20
|
+
Twenty long runs (the usual "does the free tier survive" check):
|
|
21
|
+
|
|
22
|
+
longctx-bench --model example-model --tokens 200000 --runs 20 --out soak.json
|
|
23
|
+
|
|
24
|
+
## Output
|
|
25
|
+
|
|
26
|
+
One line per run plus an optional JSON file:
|
|
27
|
+
|
|
28
|
+
run=1 est_tokens=100012 status=200 latency_ms=8421 usage={"prompt_tokens"=>100012,...} error=nil
|
|
29
|
+
|
|
30
|
+
Status `402`/`429` in the JSON file means the provider ran out of credits or
|
|
31
|
+
rate limit during the soak, which is exactly what this tool is meant to
|
|
32
|
+
surface.
|
|
33
|
+
|
|
34
|
+
## Library use
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
require "longctx/bench"
|
|
38
|
+
Longctx.run(base_url: "https://api.example.com", api_key: ENV["KEY"],
|
|
39
|
+
model: "example-model", tokens: 200_000, runs: 20)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
data/exe/longctx-bench
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "optparse"
|
|
5
|
+
require "longctx/bench"
|
|
6
|
+
|
|
7
|
+
opts = {
|
|
8
|
+
base_url: ENV["LONGCTX_BASE_URL"],
|
|
9
|
+
api_key: ENV["LONGCTX_API_KEY"],
|
|
10
|
+
model: ENV["LONGCTX_MODEL"],
|
|
11
|
+
tokens: 100_000,
|
|
12
|
+
runs: 1,
|
|
13
|
+
seed: 42,
|
|
14
|
+
out: nil
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
OptionParser.new do |o|
|
|
18
|
+
o.banner = "Usage: longctx-bench --base-url URL --model NAME [options]"
|
|
19
|
+
o.on("--base-url URL", "OpenAI-compatible base URL") { |v| opts[:base_url] = v }
|
|
20
|
+
o.on("--api-key KEY", "Bearer token (or LONGCTX_API_KEY)") { |v| opts[:api_key] = v }
|
|
21
|
+
o.on("--model NAME", "Model id to call") { |v| opts[:model] = v }
|
|
22
|
+
o.on("--tokens N", Integer, "Approximate input tokens per run (default 100000)") { |v| opts[:tokens] = v }
|
|
23
|
+
o.on("--runs N", Integer, "Number of runs (default 1)") { |v| opts[:runs] = v }
|
|
24
|
+
o.on("--seed N", Integer, "Prompt seed (default 42)") { |v| opts[:seed] = v }
|
|
25
|
+
o.on("--out FILE", "Write JSON results to FILE") { |v| opts[:out] = v }
|
|
26
|
+
end.parse!
|
|
27
|
+
|
|
28
|
+
abort "missing --base-url" unless opts[:base_url]
|
|
29
|
+
abort "missing --model" unless opts[:model]
|
|
30
|
+
|
|
31
|
+
Longctx.run(**opts)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "version"
|
|
4
|
+
require_relative "prompt"
|
|
5
|
+
require_relative "client"
|
|
6
|
+
|
|
7
|
+
module Longctx
|
|
8
|
+
# Tiny benchmark harness for long-context continuation workloads.
|
|
9
|
+
#
|
|
10
|
+
# Longctx.run(base_url: "https://api.example.com", api_key: ENV["KEY"],
|
|
11
|
+
# model: "example-model", tokens: 100_000, runs: 1)
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def run(base_url:, api_key:, model:, tokens: 100_000, runs: 1, seed: 42, out: nil)
|
|
15
|
+
client = Client.new(base_url: base_url, api_key: api_key, model: model)
|
|
16
|
+
results = (1..runs).map do |i|
|
|
17
|
+
prompt, est = Prompt.build(target_tokens: tokens, seed: seed, chapter: i)
|
|
18
|
+
res = client.complete(prompt, run: i)
|
|
19
|
+
line = format("run=%d est_tokens=%d status=%d latency_ms=%d usage=%s error=%s",
|
|
20
|
+
i, est, res.status, res.latency_ms, res.usage.inspect, res.error.inspect)
|
|
21
|
+
puts line
|
|
22
|
+
{ run: i, est_tokens: est, status: res.status, latency_ms: res.latency_ms,
|
|
23
|
+
usage: res.usage, error: res.error, preview: res.preview }
|
|
24
|
+
end
|
|
25
|
+
File.write(out, JSON.pretty_generate(results)) if out
|
|
26
|
+
results
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Longctx
|
|
8
|
+
# Minimal OpenAI-compatible chat-completions client used by the benchmark.
|
|
9
|
+
# Deliberately dependency-free so the gem can run anywhere Ruby runs.
|
|
10
|
+
class Client
|
|
11
|
+
Result = Struct.new(:run, :status, :latency_ms, :usage, :error, :preview,
|
|
12
|
+
keyword_init: true)
|
|
13
|
+
|
|
14
|
+
def initialize(base_url:, api_key:, model:, timeout: 600)
|
|
15
|
+
@base_url = base_url.sub(%r{/+\z}, "")
|
|
16
|
+
@api_key = api_key
|
|
17
|
+
@model = model
|
|
18
|
+
@timeout = timeout
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def complete(prompt, run: 1, max_tokens: 400)
|
|
22
|
+
uri = URI.parse("#{@base_url}/v1/chat/completions")
|
|
23
|
+
req = Net::HTTP::Post.new(uri)
|
|
24
|
+
req["Content-Type"] = "application/json"
|
|
25
|
+
req["Authorization"] = "Bearer #{@api_key}" if @api_key && !@api_key.empty?
|
|
26
|
+
req.body = JSON.generate(
|
|
27
|
+
model: @model,
|
|
28
|
+
messages: [{ role: "user", content: prompt }],
|
|
29
|
+
max_tokens: max_tokens,
|
|
30
|
+
temperature: 0.7
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
started = Time.now
|
|
34
|
+
begin
|
|
35
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
36
|
+
read_timeout: @timeout, open_timeout: 30) do |http|
|
|
37
|
+
http.request(req)
|
|
38
|
+
end
|
|
39
|
+
latency = ((Time.now - started) * 1000).round
|
|
40
|
+
payload = JSON.parse(res.body) rescue {}
|
|
41
|
+
usage = payload["usage"]
|
|
42
|
+
preview = payload.dig("choices", 0, "message", "content").to_s[0, 200]
|
|
43
|
+
error = nil
|
|
44
|
+
if res.code.to_i >= 400
|
|
45
|
+
error = payload["error"].is_a?(Hash) ? payload["error"]["message"] : res.body.to_s[0, 200]
|
|
46
|
+
end
|
|
47
|
+
Result.new(run: run, status: res.code.to_i, latency_ms: latency, usage: usage,
|
|
48
|
+
error: error, preview: preview)
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
latency = ((Time.now - started) * 1000).round
|
|
51
|
+
Result.new(run: run, status: 0, latency_ms: latency, usage: nil,
|
|
52
|
+
error: "#{e.class}: #{e.message}", preview: "")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Longctx
|
|
4
|
+
# Builds deterministic, reproducible prompts of a target approximate
|
|
5
|
+
# token budget. Token counts are estimated with the usual ~4 chars/token
|
|
6
|
+
# heuristic; the estimate is printed alongside every run so results stay
|
|
7
|
+
# comparable across providers.
|
|
8
|
+
module Prompt
|
|
9
|
+
CHARS_PER_TOKEN = 4
|
|
10
|
+
|
|
11
|
+
SENTENCES = [
|
|
12
|
+
"The harbour lights blinked twice before the fog swallowed them whole.",
|
|
13
|
+
"She counted the bells of the old clock tower and lost track at eleven.",
|
|
14
|
+
"Every map in the archive agreed on one thing: the river had moved.",
|
|
15
|
+
"He wrote the same paragraph four times and each version ended differently.",
|
|
16
|
+
"The train was late, the platform empty, the timetable a rumour.",
|
|
17
|
+
"Rain drummed on the tin roof like a drummer who had forgotten the song.",
|
|
18
|
+
"They kept the letters in a biscuit tin under the stairs.",
|
|
19
|
+
"The lighthouse keeper had not spoken to anyone in nine days.",
|
|
20
|
+
"A paper boat drifted past the window and nobody mentioned it.",
|
|
21
|
+
"In the morning the snow had erased the road and the argument alike."
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Returns [text, estimated_tokens]
|
|
27
|
+
def build(target_tokens:, seed: 42, chapter: 1)
|
|
28
|
+
rng = Random.new(seed + chapter)
|
|
29
|
+
target_chars = [target_tokens * CHARS_PER_TOKEN, 200].max
|
|
30
|
+
body = +"Chapter #{chapter}. "
|
|
31
|
+
body << "Context bundle for a long-form continuation experiment.\n\n"
|
|
32
|
+
while body.length < target_chars
|
|
33
|
+
body << SENTENCES[rng.rand(SENTENCES.length)] << " "
|
|
34
|
+
body << "(note #{rng.rand(100_000)})\n" if rng.rand(25).zero?
|
|
35
|
+
end
|
|
36
|
+
body << "\n\nContinue the story for roughly 300 words. Keep the same narrator, " \
|
|
37
|
+
"reuse at most two named characters, and end on an unresolved image."
|
|
38
|
+
[body, (body.length.to_f / CHARS_PER_TOKEN).round]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: longctx-bench
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alex Rivera
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-10-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Builds deterministic ~100k/200k token prompts, calls an OpenAI-compatible
|
|
14
|
+
/v1/chat/completions endpoint and records status, latency, usage and errors. Intended
|
|
15
|
+
for evaluating free-tier limits under long-context novel-continuation workloads.
|
|
16
|
+
email:
|
|
17
|
+
- longctxbench@users.noreply.github.com
|
|
18
|
+
executables:
|
|
19
|
+
- longctx-bench
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- exe/longctx-bench
|
|
26
|
+
- lib/longctx/bench.rb
|
|
27
|
+
- lib/longctx/client.rb
|
|
28
|
+
- lib/longctx/prompt.rb
|
|
29
|
+
- lib/longctx/version.rb
|
|
30
|
+
homepage: https://rubygems.org/gems/longctx-bench
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
source_code_uri: https://rubygems.org/gems/longctx-bench
|
|
35
|
+
documentation_uri: https://rubygems.org/gems/longctx-bench
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '3.0'
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubygems_version: 3.3.15
|
|
52
|
+
signing_key:
|
|
53
|
+
specification_version: 4
|
|
54
|
+
summary: Dependency-free long-context continuation benchmark for OpenAI-compatible
|
|
55
|
+
endpoints
|
|
56
|
+
test_files: []
|