Files
Kaname/src/worker/betterbench.ts
T
Pascal LinxweilerandClaude Opus 5.5 b623b16a6d Benchmarks: paired A/B runs, sweeps, resource usage
- A/B with betterbench ab between two deployments of the same model, with verdict, confidence
  interval and caveats.
- Sweeps vary one or two deployment settings, deploy, benchmark and remove each variant, and
  compare them in one table.
- Every run records CPU, RAM, VRAM, GPU utilization and power.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 10:04:40 +02:00

135 lines
4.3 KiB
TypeScript

// BetterBench (https://github.com/GGZ14/BetterBench, Apache-2.0): the benchmark Kaname runs
// against deployments. Flags verified against v0.6.1 (`betterbench run --help`, betterbench/cli.py).
import { dirname, join } from "node:path";
import type { BenchmarkSettings } from "@/lib/db/schema";
import { coreEnv } from "@/lib/env";
export function betterbenchBin(): string {
return coreEnv().BETTERBENCH_BIN;
}
/** The Python of BetterBench's own venv (it imports `betterbench`). */
export function betterbenchPython(): string {
const bin = betterbenchBin();
return bin.includes("/") ? join(dirname(bin), "python") : "python3";
}
export function runArgs(o: {
endpoint: string;
model: string;
out: string;
settings: BenchmarkSettings;
maxModelLen: number | null;
notes: Record<string, string>;
}): string[] {
const s = o.settings;
return [
"run",
"--endpoint",
o.endpoint,
"--model",
o.model,
"--out",
o.out,
// Kaname knows the context window; llama-server's /v1/models doesn't report it.
...(o.maxModelLen ? ["--max-model-len", String(o.maxModelLen)] : []),
...(s.quick ? ["--quick"] : []),
...(s.passes != null ? ["--passes", String(s.passes)] : []),
...(s.warmup != null ? ["--warmup", String(s.warmup)] : []),
...s.phases.map((p) => `--${p}`),
...Object.entries(o.notes).flatMap(([k, v]) => ["--note", `${k}=${v}`]),
"--no-update-check",
];
}
/** `betterbench ab`: interleaved paired A/B until the CI is tighter than --mde (or --max-pairs). */
export function abArgs(o: {
endpointA: string;
endpointB: string;
model: string;
out: string;
mdePct: number;
maxPairs: number;
notes: Record<string, string>;
}): string[] {
return [
"ab",
"--endpoint-a",
o.endpointA,
"--endpoint-b",
o.endpointB,
"--model",
o.model,
"--out",
o.out,
"--mde",
String(o.mdePct),
"--max-pairs",
String(o.maxPairs),
...Object.entries(o.notes).flatMap(([k, v]) => ["--note", `${k}=${v}`]),
"--no-update-check",
];
}
export { AB_FILE } from "@/lib/benchmark-paths";
export const AB_DEFAULT_MDE_PCT = 1;
export const AB_DEFAULT_MAX_PAIRS = 200;
/**
* Prints Kaname's BenchmarkSummary for a results.json, using BetterBench's own report functions
* so the numbers match its HTML report.
*/
export const SUMMARY_SCRIPT = `
import json, math, sys
from betterbench import report as r
res = json.load(open(sys.argv[1]))
def clean(v):
if isinstance(v, dict):
return {str(k): clean(x) for k, x in v.items()}
if isinstance(v, (list, tuple)):
return [clean(x) for x in v]
if isinstance(v, (bool, str)) or v is None:
return v
try:
f = float(v)
except (TypeError, ValueError):
return None
if math.isnan(f) or math.isinf(f):
return None
return int(f) if float(f).is_integer() and not isinstance(v, float) else f
rows = r.single_rows(res)
cfg = res.get("config", {})
env = res.get("env", {})
reasoning = r.reasoning_rows(res)
gate = res.get("sample_gate") or r.sample_gate(res)
trunc, ok_runs = r.truncation_summary(res)
failed = sum(1 for recs in res.get("single_stream", {}).values() for x in recs if not x.get("ok"))
keep_cfg = ("warmup", "runs_per_category", "temperature", "top_p", "top_k", "greedy", "unique_nonce", "weights",
"concurrency_levels", "concurrency_requests", "prefill_runs", "prefill_warmup", "prefill_max_tokens")
out = {
"v": 2,
"combined": r.combined_score(res, rows),
"single": rows,
"prefill": r.prefill_rows(res),
"concurrency": r.concurrency_rows(res),
"reasoning": reasoning if r.has_reasoning_evidence(reasoning) else [],
"gate": gate.get("under_sampled", []),
"truncated": [trunc, ok_runs],
"failedSingle": failed,
"phases": r.phases_present(res),
"cacheHits": r.cache_hits(res),
"betterbenchVersion": res.get("betterbench_version", ""),
"corpusVersion": res.get("corpus_version", ""),
"corpusHash": env.get("corpus_hash"),
"timestamp": env.get("timestamp"),
"maxModelLen": env.get("max_model_len"),
"config": {k: cfg.get(k) for k in keep_cfg},
"passes": cfg.get("runs_per_category"),
}
print(json.dumps(clean(out)))
`;
/** Version of the summary shape above; older stored summaries are recomputed from results.json. */
export const SUMMARY_VERSION = 2;