84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { benchmark } from "../../scripts/scale/benchmark.ts";
|
|
import {
|
|
assertBenchmarkResult,
|
|
median,
|
|
} from "../../scripts/scale/benchmark-result.ts";
|
|
|
|
function fakeDurations(values: readonly number[]): () => number {
|
|
let index = 0;
|
|
return () => values[index++ % values.length] as number;
|
|
}
|
|
|
|
describe("benchmark runner", () => {
|
|
it("records median rather than a single timing sample", async () => {
|
|
const result = await benchmark("compose", {
|
|
samples: 5,
|
|
run: fakeDurations([10, 11, 100, 12, 9]),
|
|
});
|
|
|
|
expect(result.medianMs).toBe(11);
|
|
expect(result.samples).toHaveLength(5);
|
|
});
|
|
|
|
it("runs warm-up iterations that are not sampled", async () => {
|
|
let calls = 0;
|
|
const result = await benchmark("warm", {
|
|
samples: 3,
|
|
warmup: 2,
|
|
run: () => {
|
|
calls += 1;
|
|
return calls;
|
|
},
|
|
});
|
|
expect(calls).toBe(5);
|
|
expect(result.samples).toEqual([3, 4, 5]);
|
|
});
|
|
|
|
it("produces the machine-readable result schema", async () => {
|
|
const result = await benchmark("registry-composition", {
|
|
samples: 3,
|
|
profile: "S2",
|
|
cardinality: 10_000,
|
|
measureHeap: true,
|
|
metadata: { implementation: "composeUniqueRecord" },
|
|
run: () => {
|
|
Array.from({ length: 1_000 }, (_, i) => i);
|
|
},
|
|
});
|
|
expect(result).toMatchObject({
|
|
name: "registry-composition",
|
|
profile: "S2",
|
|
cardinality: 10_000,
|
|
metadata: { implementation: "composeUniqueRecord" },
|
|
});
|
|
expect(result.medianMs).toBeGreaterThanOrEqual(0);
|
|
expect(typeof result.heapBytes).toBe("number");
|
|
});
|
|
|
|
it("computes even-length medians and rejects inconsistent results", () => {
|
|
expect(median([4, 1, 3, 2])).toBe(2.5);
|
|
expect(() =>
|
|
assertBenchmarkResult({
|
|
name: "x",
|
|
profile: "S0",
|
|
cardinality: 1,
|
|
samples: [1, 2, 3],
|
|
medianMs: 3,
|
|
metadata: {},
|
|
}),
|
|
).toThrow(/median/);
|
|
expect(() =>
|
|
assertBenchmarkResult({
|
|
name: "x",
|
|
profile: "S9" as never,
|
|
cardinality: 1,
|
|
samples: [1],
|
|
medianMs: 1,
|
|
metadata: {},
|
|
}),
|
|
).toThrow(/profile/);
|
|
});
|
|
});
|