Benchmarks: lean list queries

listBenchmarks() selected every column of every run, including the raw A/B
series, all quality eval items, hardware facts and descriptions, while lists
show only the snapshot, settings, headline numbers and the quality score. With
200 runs /api/benchmarks returned 732 KB, re-fetched every 3 s while a run was
active; the table used about 55 KB of it.

- listBenchmarks() returns lean rows: the headline numbers are computed on the
  server from the summary, the quality score comes from json_extract().
- The model page asks for 5 runs (it shows 5), the ⌘K index for 300.
- getBenchmark()/getBenchmarks() keep the full rows for detail and compare;
  cancel and delete only read the state.
- ListRow (lists) and RunRow (detail) are separate types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
Claude committed 2026-11-01 20:52:12 +00:00
1 parent 7239ba2692
commit 2e56351e96
10 files changed
+111 -34

No files matched your search

+48 -11
View File
@@ -1,9 +1,10 @@
import "server-only";
import { rm } from "node:fs/promises";
import { and, asc, desc, eq, gt, inArray, or } from "drizzle-orm";
import { and, asc, desc, eq, gt, inArray, or, sql } from "drizzle-orm";
import { z } from "zod";
import { runDir } from "@/lib/benchmark-paths";
import { libraryModelId, modelFor, snapshotOf } from "@/lib/benchmark-snapshot";
import { headline } from "@/lib/benchmark-view";
import { db } from "@/lib/db/client";
import { BENCHMARK_PHASES, benchmarkLogs, benchmarkRuns, endpoints, QUALITY_SECTIONS } from "@/lib/db/schema";
import { probeEndpoint } from "@/lib/endpoint-probe";
@@ -95,7 +96,8 @@ export async function createBenchmark(input: BenchmarkInput) {
return row;
}
const listColumns = {
/** Everything about a run: the detail and compare pages, results downloads. */
const runColumns = {
id: benchmarkRuns.id,
endpointId: benchmarkRuns.endpointId,
modelId: benchmarkRuns.modelId,
@@ -123,10 +125,37 @@ const listColumns = {
createdAt: benchmarkRuns.createdAt,
};
export type BenchmarkRow = Awaited<ReturnType<typeof listBenchmarks>>[number];
/**
* What lists show: no A/B series, eval items, hardware facts or descriptions (lists got all of it,
* ~13× what they use, re-fetched every 3 s while a run was active). The summary is read only to
* compute the headline numbers.
*/
const listColumns = {
id: benchmarkRuns.id,
endpointId: benchmarkRuns.endpointId,
endpointBId: benchmarkRuns.endpointBId,
modelId: benchmarkRuns.modelId,
label: benchmarkRuns.label,
kind: benchmarkRuns.kind,
settings: benchmarkRuns.settings,
snapshot: benchmarkRuns.snapshot,
state: benchmarkRuns.state,
phase: benchmarkRuns.phase,
cancelRequestedAt: benchmarkRuns.cancelRequestedAt,
startedAt: benchmarkRuns.startedAt,
finishedAt: benchmarkRuns.finishedAt,
hasReport: benchmarkRuns.hasReport,
createdAt: benchmarkRuns.createdAt,
summary: benchmarkRuns.summary,
qualityScore: sql<number | null>`json_extract(${benchmarkRuns.quality}, '$.score')`,
};
export async function listBenchmarks(filter: { endpointId?: string; modelId?: string } = {}) {
return db()
export type BenchmarkRow = NonNullable<Awaited<ReturnType<typeof getBenchmark>>>;
export type BenchmarkListRow = Awaited<ReturnType<typeof listBenchmarks>>[number];
/** Runs for lists, newest first; `endpointId` matches either side of an A/B. */
export async function listBenchmarks(filter: { endpointId?: string; modelId?: string } = {}, options: { limit?: number } = {}) {
let query = db()
.select(listColumns)
.from(benchmarkRuns)
.where(
@@ -135,23 +164,31 @@ export async function listBenchmarks(filter: { endpointId?: string; modelId?: st
filter.modelId ? eq(benchmarkRuns.modelId, filter.modelId) : undefined,
),
)
.orderBy(desc(benchmarkRuns.createdAt));
.orderBy(desc(benchmarkRuns.createdAt))
.$dynamic();
if (options.limit) query = query.limit(options.limit);
return (await query).map(({ summary, ...r }) => ({ ...r, headline: headline(summary) }));
}
export async function getBenchmark(id: string) {
const [row] = await db().select(listColumns).from(benchmarkRuns).where(eq(benchmarkRuns.id, id));
const [row] = await db().select(runColumns).from(benchmarkRuns).where(eq(benchmarkRuns.id, id));
return row ?? null;
}
export async function getBenchmarks(ids: string[]) {
if (ids.length === 0) return [];
const rows = await db().select(listColumns).from(benchmarkRuns).where(inArray(benchmarkRuns.id, ids));
return ids.map((id) => rows.find((r) => r.id === id)).filter((r) => r != null);
const rows = await db().select(runColumns).from(benchmarkRuns).where(inArray(benchmarkRuns.id, ids));
const byId = new Map(rows.map((r) => [r.id, r]));
return ids.map((id) => byId.get(id)).filter((r) => r != null);
}
async function runState(id: string) {
return db().query.benchmarkRuns.findFirst({ columns: { id: true, state: true }, where: eq(benchmarkRuns.id, id) });
}
/** Queued runs are cancelled right away; a running one is stopped by the worker. */
export async function cancelBenchmark(id: string) {
const run = await getBenchmark(id);
const run = await runState(id);
if (!run) throw new BenchmarkError("not_found", "Benchmark not found.", 404);
if (run.state === "queued") {
await db().update(benchmarkRuns).set({ state: "cancelled", finishedAt: new Date() }).where(and(eq(benchmarkRuns.id, id), eq(benchmarkRuns.state, run.state)));
@@ -172,7 +209,7 @@ export async function updateBenchmark(id: string, patch: { label?: string | null
/** Deletes the run, its log and its files. Cancel a running one first. */
export async function deleteBenchmark(id: string) {
const run = await getBenchmark(id);
const run = await runState(id);
if (!run) throw new BenchmarkError("not_found", "Benchmark not found.", 404);
if (run.state === "running") throw new BenchmarkError("busy", "Cancel the run before deleting it.", 409);
await db().delete(benchmarkLogs).where(eq(benchmarkLogs.runId, id));
+35
View File
@@ -129,3 +129,38 @@ describe("queuing runs", () => {
assert.equal(run!.snapshotB && "model" in run!.snapshotB ? run!.snapshotB.model : null, "Qwen/Qwen3-8B");
});
});
describe("listing runs", () => {
it("lists headline numbers and the quality score, not the heavy results", async () => {
const { benchmarkRuns } = M.schema;
const snapshot = { type: "endpoint" as const, endpointName: "x", model: "m", engine: null, engineVersion: null, contextSize: null, hardware: {} };
const settings = { quick: false, passes: null, warmup: null, phases: [] };
const summary = {
v: 2,
combined: { decode: 42.5, ttft_p50: 120, itl_low1: null, update_p50: null, update_p99: null },
single: [],
prefill: [{ target_depth: 4000, skipped: false, pp_med: 900 }],
concurrency: [{ level: 4, ok: 4, requests: 4, aggregate_tps: 150, ttft_p50: null, ttft_p99: null, decode_med: null }],
phases: [],
cacheHits: 0,
betterbenchVersion: "0.6.1",
corpusVersion: "v1",
passes: null,
quick: false,
};
const quality = { evalSet: "v1", score: 71, sections: [], items: [{ section: "tools" as const, id: "t1", pass: true }], config: { temperature: 0, seed: 1, thinking: false, contextTokens: null } };
await M.db.db().insert(benchmarkRuns).values([
{ kind: "run", state: "completed", settings, snapshot, summary, createdAt: new Date(Date.now() + 1000) },
{ kind: "eval", state: "completed", settings, snapshot, quality, createdAt: new Date(Date.now() + 2000) },
]);
const rows = await M.benchmarks.listBenchmarks();
const evalRow = rows.find((r) => r.kind === "eval")!;
const runRow = rows.find((r) => r.kind === "run" && r.headline.decode != null)!;
assert.equal(evalRow.qualityScore, 71);
assert.deepEqual(runRow.headline, { decode: 42.5, ttftMs: 120, prefill: { tps: 900, depth: 4000 }, peak: { tps: 150, level: 4 } });
for (const r of rows) for (const k of ["summary", "quality", "ab", "meta", "metaB", "system", "description", "snapshotB"]) assert.ok(!(k in r), k);
const [newest] = await M.benchmarks.listBenchmarks({}, { limit: 1 });
assert.equal(newest.id, evalRow.id, "newest first, limited");
assert.equal((await M.benchmarks.getBenchmark(evalRow.id))!.quality?.items.length, 1, "the detail still has everything");
});
});