From 2888ce9249dea5ac6ade596992a853ce9bc28165 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:39:22 -0800 Subject: [PATCH 1/2] fix(scripts): server benchmark keeps the rounds it finished when a later run fails `server-metrics ab` wrote a.json and b.json only after the last round, so one failed run threw away up to two hours of finished rounds. It now writes both files up front and again after every run, and on a failure says how many A and B runs it kept. compare already accepts unequal run counts; a test pins that. Co-Authored-By: Claude Opus 5.5 --- scripts/server-metrics.test.ts | 11 +++++++++ scripts/server-metrics.ts | 41 ++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/scripts/server-metrics.test.ts b/scripts/server-metrics.test.ts index e87174969c9ed..f44f14269764e 100644 --- a/scripts/server-metrics.test.ts +++ b/scripts/server-metrics.test.ts @@ -103,6 +103,17 @@ describe("compareMetrics", () => { expect(compareMetrics(fixed, cpuRuns(2.2, 5, 9), 10, gatedRules).regressed).toEqual([]); }); + it("compares the unequal run counts an ab that stopped early leaves", () => { + const steady = cpuRuns(26.1, 27.4, 25.8); + const partial = compareMetrics(steady, cpuRuns(2.2, 2.5), 10, gatedRules); + expect(partial.improved).toContain("quiet.cpuPct"); + expect(partial.table).toContain("(3 runs), after `abc` (2 runs)"); + const single = compareMetrics(steady, cpuRuns(2.2), 10, gatedRules); + expect(single.improved).toContain("quiet.cpuPct"); + expect(single.table).toContain("| quiet.cpuPct | 26.1 | 2.2 | -92% | | better |"); + expect(compareMetrics(steady, cpuRuns(), 10, gatedRules).compared).toBe(0); + }); + it("reads a file written before loop delay and GC were recorded", () => { const older = decodeMetrics({ meta, diff --git a/scripts/server-metrics.ts b/scripts/server-metrics.ts index c3482c72e70ff..337a549cb0649 100644 --- a/scripts/server-metrics.ts +++ b/scripts/server-metrics.ts @@ -2376,19 +2376,36 @@ const abCommand = Command.make( yield* withSeed(unseeded[1], input), ] as const; const runs: [Measured[], Measured[]] = [[], []]; - for (let round = 0; round < input.rounds; round++) { - // A B, then B A, so a drift in machine load lands on both sides. - for (const side of round % 2 === 0 ? ([0, 1] as const) : ([1, 0] as const)) { - yield* Console.error(`round ${round + 1}/${input.rounds}: ${side === 0 ? "A" : "B"}`); - runs[side].push((yield* runOnce(sides[side])).measured); + // Written up front and after every run, so a failure keeps the finished runs and never leaves + // them beside an older file. compare takes unequal run counts. + const save = Effect.gen(function* () { + const loadAfter = yield* machineLoad; + const files = [ + { meta: { ...metas[0], loadAfter }, runs: runs[0] }, + { meta: { ...metas[1], loadAfter }, runs: runs[1] }, + ] as const satisfies readonly [ServerMetrics, ServerMetrics]; + yield* writeMetrics(Option.some(path.join(outDir, "a.json")), files[0]); + yield* writeMetrics(Option.some(path.join(outDir, "b.json")), files[1]); + return files; + }); + let saved = yield* save; + yield* Effect.gen(function* () { + for (let round = 0; round < input.rounds; round++) { + // A B, then B A, so a drift in machine load lands on both sides. + for (const side of round % 2 === 0 ? ([0, 1] as const) : ([1, 0] as const)) { + yield* Console.error(`round ${round + 1}/${input.rounds}: ${side === 0 ? "A" : "B"}`); + runs[side].push((yield* runOnce(sides[side])).measured); + saved = yield* save; + } } - } - const loadAfter = yield* machineLoad; - const before: ServerMetrics = { meta: { ...metas[0], loadAfter }, runs: runs[0] }; - const after: ServerMetrics = { meta: { ...metas[1], loadAfter }, runs: runs[1] }; - yield* writeMetrics(Option.some(path.join(outDir, "a.json")), before); - yield* writeMetrics(Option.some(path.join(outDir, "b.json")), after); - yield* report(before, after, input.threshold); + }).pipe( + Effect.onError(() => + Console.error( + `ab stopped early; ${outDir} keeps ${runs[0].length} A and ${runs[1].length} B runs`, + ), + ), + ); + yield* report(saved[0], saved[1], input.threshold); }, Effect.scoped), ).pipe( Command.withDescription( From 3e14821257e28744f5d7f1d719dcd0c26c3c3cce Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:48:54 -0800 Subject: [PATCH 2/2] fix(scripts): ab saves through a tested core, atomically, and reports what reached disk The round loop moves into an exported runAbRounds that takes the run and save steps, so a test can fail B after A and check that a.json keeps A's run. Each file is written to a sibling and renamed, so a failed write keeps the previous checkpoint, and the stopped-early message counts runs only after their file is written. Co-Authored-By: Claude Opus 5.5 --- scripts/server-metrics.test.ts | 21 ++++++++ scripts/server-metrics.ts | 94 +++++++++++++++++++++++----------- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/scripts/server-metrics.test.ts b/scripts/server-metrics.test.ts index f44f14269764e..6597e9297514b 100644 --- a/scripts/server-metrics.test.ts +++ b/scripts/server-metrics.test.ts @@ -1,3 +1,5 @@ +import { it as effectIt } from "@effect/vitest"; +import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { describe, expect, it } from "vite-plus/test"; @@ -19,6 +21,7 @@ import { ServerMetrics, slope, } from "./server-metrics-report.ts"; +import { runAbRounds } from "./server-metrics.ts"; const decodeMetrics = Schema.decodeSync(ServerMetrics); @@ -612,3 +615,21 @@ describe("activityRenewalMissed", () => { expect(activityRenewalMissed(0, sentAt, repliedAt)).toBe(missed); }); }); + +describe("runAbRounds", () => { + effectIt.effect("keeps the runs that finished when a later run fails", () => + Effect.gen(function* () { + const files: Record<0 | 1, ReadonlyArray> = { 0: [], 1: [] }; + const failure = yield* Effect.flip( + runAbRounds({ + rounds: 3, + outDir: "out", + runSide: (side) => (side === 0 ? Effect.succeed("a1") : Effect.fail("b crashed")), + save: (side, runs) => Effect.sync(() => (files[side] = [...runs])), + }), + ); + expect(failure).toBe("b crashed"); + expect(files).toEqual({ 0: ["a1"], 1: [] }); + }), + ); +}); diff --git a/scripts/server-metrics.ts b/scripts/server-metrics.ts index 337a549cb0649..9fa06fcfdea5c 100644 --- a/scripts/server-metrics.ts +++ b/scripts/server-metrics.ts @@ -2129,8 +2129,56 @@ const writeMetrics = Effect.fn("serverMetrics.writeMetrics")(function* ( ) { const fs = yield* FileSystem.FileSystem; const json = yield* encodeMetrics(metrics); - if (Option.isSome(out)) yield* fs.writeFileString(out.value, `${json}\n`); - else yield* Console.log(json); + if (Option.isNone(out)) return yield* Console.log(json); + // A sibling then a rename, so a failed write keeps the previous file whole. + const temp = `${out.value}.tmp`; + yield* fs.writeFileString(temp, `${json}\n`); + yield* fs.rename(temp, out.value); +}); + +/** + * Runs A and B alternately, saving a side after each of its runs so a failure keeps the finished + * runs. On a failure it reports how many runs of each side reached disk. + */ +export const runAbRounds = Effect.fn("serverMetrics.abRounds")(function* < + A, + S, + E, + R, + ES, + RS, +>(options: { + readonly rounds: number; + readonly outDir: string; + readonly runSide: (side: 0 | 1) => Effect.Effect; + readonly save: (side: 0 | 1, runs: ReadonlyArray) => Effect.Effect; +}) { + const runs: [A[], A[]] = [[], []]; + const persisted = [0, 0]; + const saveSide = Effect.fn("serverMetrics.abSave")(function* (side: 0 | 1) { + const count = runs[side].length; + const saved = yield* options.save(side, runs[side]); + persisted[side] = count; + return saved; + }); + const saved: [S, S] = [yield* saveSide(0), yield* saveSide(1)]; + yield* Effect.gen(function* () { + for (let round = 0; round < options.rounds; round++) { + // A B, then B A, so a drift in machine load lands on both sides. + for (const side of round % 2 === 0 ? ([0, 1] as const) : ([1, 0] as const)) { + yield* Console.error(`round ${round + 1}/${options.rounds}: ${side === 0 ? "A" : "B"}`); + runs[side].push(yield* options.runSide(side)); + saved[side] = yield* saveSide(side); + } + } + }).pipe( + Effect.onError(() => + Console.error( + `ab stopped early; ${options.outDir} keeps ${persisted[0]} A and ${persisted[1]} B runs`, + ), + ), + ); + return saved; }); /** Prints the comparison and sets exit code 1 when a metric got worse. */ @@ -2375,36 +2423,20 @@ const abCommand = Command.make( yield* withSeed(unseeded[0], input), yield* withSeed(unseeded[1], input), ] as const; - const runs: [Measured[], Measured[]] = [[], []]; - // Written up front and after every run, so a failure keeps the finished runs and never leaves - // them beside an older file. compare takes unequal run counts. - const save = Effect.gen(function* () { - const loadAfter = yield* machineLoad; - const files = [ - { meta: { ...metas[0], loadAfter }, runs: runs[0] }, - { meta: { ...metas[1], loadAfter }, runs: runs[1] }, - ] as const satisfies readonly [ServerMetrics, ServerMetrics]; - yield* writeMetrics(Option.some(path.join(outDir, "a.json")), files[0]); - yield* writeMetrics(Option.some(path.join(outDir, "b.json")), files[1]); - return files; + // Written up front and after every run; compare takes unequal run counts. + const saved = yield* runAbRounds({ + rounds: input.rounds, + outDir, + runSide: (side) => runOnce(sides[side]).pipe(Effect.map((run) => run.measured)), + save: Effect.fn("serverMetrics.abSaveFile")(function* (side, runs) { + const file: ServerMetrics = { + meta: { ...metas[side], loadAfter: yield* machineLoad }, + runs: [...runs], + }; + yield* writeMetrics(Option.some(path.join(outDir, side === 0 ? "a.json" : "b.json")), file); + return file; + }), }); - let saved = yield* save; - yield* Effect.gen(function* () { - for (let round = 0; round < input.rounds; round++) { - // A B, then B A, so a drift in machine load lands on both sides. - for (const side of round % 2 === 0 ? ([0, 1] as const) : ([1, 0] as const)) { - yield* Console.error(`round ${round + 1}/${input.rounds}: ${side === 0 ? "A" : "B"}`); - runs[side].push((yield* runOnce(sides[side])).measured); - saved = yield* save; - } - } - }).pipe( - Effect.onError(() => - Console.error( - `ab stopped early; ${outDir} keeps ${runs[0].length} A and ${runs[1].length} B runs`, - ), - ), - ); yield* report(saved[0], saved[1], input.threshold); }, Effect.scoped), ).pipe(