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));