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>
This commit is contained in:
Pascal LinxweilerandClaude Opus 5.5 committed 2026-10-13 10:04:40 +02:00
1 parent 9e167a44f6
commit b623b16a6d
37 files changed
+4314 -214

No files matched your search

+49 -55
View File
@@ -1,10 +1,11 @@
import "server-only";
import { rm } from "node:fs/promises";
import { and, asc, desc, eq, gt, inArray, isNull } from "drizzle-orm";
import { and, asc, desc, eq, gt, inArray, or } from "drizzle-orm";
import { z } from "zod";
import { runDir } from "@/lib/benchmark-paths";
import { deploymentSnapshot } from "@/lib/benchmark-snapshot";
import { db } from "@/lib/db/client";
import { BENCHMARK_PHASES, benchmarkLogs, benchmarkRuns, type BenchmarkSnapshot, deployments, models, runtimes, workerState } from "@/lib/db/schema";
import { BENCHMARK_PHASES, benchmarkLogs, benchmarkRuns } from "@/lib/db/schema";
export class BenchmarkError extends Error {
constructor(
@@ -24,68 +25,55 @@ export const BenchmarkBody = z.object({
warmup: z.number().int().min(0).max(50).nullable().optional(),
/** Empty: all phases. */
phases: z.array(z.enum(BENCHMARK_PHASES)).max(3).default([]),
/** A/B: the second deployment (B), the target minimum detectable effect in % and the pair ceiling. */
deploymentBId: z.string().min(1).nullable().optional(),
mdePct: z.number().min(0.1).max(50).nullable().optional(),
maxPairs: z.number().int().min(12).max(2000).nullable().optional(),
/** Set by sweeps. */
sweepId: z.string().nullable().optional(),
});
export type BenchmarkInput = z.infer<typeof BenchmarkBody>;
/** Names of the GPUs a deployment uses (all detected ones when none are picked). */
async function hostFacts(uuids: string[] | null, nvidia: boolean) {
const ws = await db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") });
const h = ws?.docker;
const gpus = h?.gpus ?? [];
return {
gpuNames: nvidia ? (uuids ? gpus.filter((g) => uuids.includes(g.uuid)) : gpus).map((g) => g.name) : [],
host: { cpuModel: h?.cpuModel ?? null, threads: h?.cpus ?? null, memoryBytes: h?.memoryBytes ?? null, os: h?.os ?? null },
};
/** A running deployment as it is now: what a run measures and records. */
async function snapshotOf(deploymentId: string, role: string) {
const d = await deploymentSnapshot(deploymentId);
if (!d) throw new BenchmarkError("not_found", `${role} not found.`, 404);
if (d.state !== "running") throw new BenchmarkError("invalid", `Start ${role.toLowerCase()} first; benchmarks run against a running server.`, 409);
return d;
}
/** Queue a run against a running deployment; the worker runs one benchmark at a time. */
/**
* Queue a run against a running deployment, or a paired A/B between two running deployments of the
* same model (BetterBench sends one model name to both). The worker runs one benchmark at a time.
*/
export async function createBenchmark(input: BenchmarkInput) {
const [d] = await db()
.select({
id: deployments.id,
name: deployments.name,
state: deployments.state,
options: deployments.options,
cpus: deployments.cpus,
memoryMb: deployments.memoryMb,
command: deployments.command,
imageId: deployments.imageId,
modelId: deployments.modelId,
repoId: models.repoId,
runtimeName: runtimes.name,
engine: runtimes.engine,
accelerator: runtimes.accelerator,
image: runtimes.image,
})
.from(deployments)
.innerJoin(runtimes, eq(runtimes.id, deployments.runtimeId))
.innerJoin(models, eq(models.id, deployments.modelId))
.where(and(eq(deployments.id, input.deploymentId), isNull(deployments.deleteRequestedAt)));
if (!d) throw new BenchmarkError("not_found", "Deployment not found.", 404);
if (d.state !== "running") throw new BenchmarkError("invalid", "Start the deployment first; benchmarks run against a running server.", 409);
const snapshot: BenchmarkSnapshot = {
deploymentName: d.name,
repoId: d.repoId,
runtimeName: d.runtimeName,
engine: d.engine,
accelerator: d.accelerator,
...(await hostFacts(d.options.gpus ?? null, d.accelerator === "nvidia")),
image: d.image,
imageId: d.imageId,
options: d.options,
cpus: d.cpus,
memoryMb: d.memoryMb,
command: [d.image, ...(d.command ?? [])].join(" "),
};
const ab = !!input.deploymentBId;
const a = await snapshotOf(input.deploymentId, ab ? "Deployment A" : "The deployment");
const b = ab ? await snapshotOf(input.deploymentBId!, "Deployment B") : null;
if (b) {
if (b.id === a.id) throw new BenchmarkError("invalid", "Pick two different deployments for an A/B.", 400);
if (b.snapshot.repoId !== a.snapshot.repoId) {
throw new BenchmarkError("invalid", "An A/B compares two deployments of the same model (BetterBench requests one model name from both).", 400);
}
}
const [row] = await db()
.insert(benchmarkRuns)
.values({
deploymentId: d.id,
modelId: d.modelId,
kind: ab ? "ab" : "run",
deploymentId: a.id,
deploymentBId: b?.id ?? null,
modelId: a.modelId,
label: input.label || null,
settings: { quick: input.quick, passes: input.passes ?? null, warmup: input.warmup ?? null, phases: input.phases },
snapshot,
settings: {
quick: input.quick,
passes: input.passes ?? null,
warmup: input.warmup ?? null,
phases: input.phases,
...(ab ? { mdePct: input.mdePct ?? null, maxPairs: input.maxPairs ?? null } : {}),
},
snapshot: a.snapshot,
snapshotB: b?.snapshot ?? null,
sweepId: input.sweepId ?? null,
})
.returning({ id: benchmarkRuns.id });
return row;
@@ -107,6 +95,12 @@ const listColumns = {
error: benchmarkRuns.error,
summary: benchmarkRuns.summary,
hasReport: benchmarkRuns.hasReport,
kind: benchmarkRuns.kind,
deploymentBId: benchmarkRuns.deploymentBId,
snapshotB: benchmarkRuns.snapshotB,
ab: benchmarkRuns.ab,
resources: benchmarkRuns.resources,
sweepId: benchmarkRuns.sweepId,
createdAt: benchmarkRuns.createdAt,
};
@@ -118,7 +112,7 @@ export async function listBenchmarks(filter: { deploymentId?: string; modelId?:
.from(benchmarkRuns)
.where(
and(
filter.deploymentId ? eq(benchmarkRuns.deploymentId, filter.deploymentId) : undefined,
filter.deploymentId ? or(eq(benchmarkRuns.deploymentId, filter.deploymentId), eq(benchmarkRuns.deploymentBId, filter.deploymentId)) : undefined,
filter.modelId ? eq(benchmarkRuns.modelId, filter.modelId) : undefined,
),
)