test(fuzz): count a stage as slow only if it fails three measurements in a row #214

Merged
abiwab merged 1 commit from test/fuzz-pathological-retry into main 2026-11-21 21:14:21 +01:00

View file

@ -52,6 +52,8 @@ const GENERATORS: Record<string, (n: number) => string> = {
const SMALL = 3_125; const SMALL = 3_125;
const LARGE = 50_000; const LARGE = 50_000;
const MAX_EXPONENT = 1.15; const MAX_EXPONENT = 1.15;
/** Measurements a stage must fail in a row to count as slow. */
const ATTEMPTS = 3;
/** Below this, the large input is too fast for its time to mean anything. */ /** Below this, the large input is too fast for its time to mean anything. */
const NOISE_FLOOR_MS = 20; const NOISE_FLOOR_MS = 20;
@ -63,23 +65,51 @@ const NOISE_FLOOR_MS = 20;
*/ */
const KNOWN_SLOW: Record<string, { issue: string; scale?: number }> = {}; const KNOWN_SLOW: Record<string, { issue: string; scale?: number }> = {};
function exponents(generate: (n: number) => string, scale: number): string[] { /** Growth exponent of each stage above the noise floor, from one measurement. */
const small = Math.round(SMALL * scale); function measureExponents(
const large = Math.round(LARGE * scale); generate: (n: number) => string,
small: number,
large: number,
): Partial<Record<Stage, { k: number; a: number; b: number }>> {
measure(generate(small)); // warm-up measure(generate(small)); // warm-up
const a = measure(generate(small)); const a = measure(generate(small));
const b = measure(generate(large)); const b = measure(generate(large));
const slow: string[] = []; const out: Partial<Record<Stage, { k: number; a: number; b: number }>> = {};
for (const stage of ["parse", "compile", "format"] as const) { for (const stage of ["parse", "compile", "format"] as const) {
if (b[stage] < NOISE_FLOOR_MS) continue; if (b[stage] < NOISE_FLOOR_MS) continue;
const k = Math.log(b[stage] / a[stage]) / Math.log(large / small); const k = Math.log(b[stage] / a[stage]) / Math.log(large / small);
if (k > MAX_EXPONENT) { out[stage] = { k, a: a[stage], b: b[stage] };
slow.push( }
`${stage}: n^${k.toFixed(2)} (${a[stage].toFixed(1)} ms at ${small}, ${b[stage].toFixed(1)} ms at ${large})`, return out;
}
/**
* A stage is slow only if it is above `MAX_EXPONENT` in each of `ATTEMPTS`
* measurements. A shared CI runner can stretch one large run (a GC pause, a
* busy neighbour) and push a linear stage to n^1.2 once; a super-linear one
* (F5 was n^2) is above the line every time. Kept: the lowest exponent seen.
*/
function exponents(generate: (n: number) => string, scale: number): string[] {
const small = Math.round(SMALL * scale);
const large = Math.round(LARGE * scale);
let slow: Partial<Record<Stage, { k: number; a: number; b: number }>> | null =
null;
for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
const now = measureExponents(generate, small, large);
const next: typeof now = {};
for (const stage of ["parse", "compile", "format"] as const) {
const m = now[stage];
if (!m || m.k <= MAX_EXPONENT) continue;
const before = slow ? slow[stage] : m;
if (before) next[stage] = before.k < m.k ? before : m;
}
slow = next;
if (Object.keys(slow).length === 0) return [];
}
return Object.entries(slow ?? {}).map(
([stage, m]) =>
`${stage}: n^${m.k.toFixed(2)} (${m.a.toFixed(1)} ms at ${small}, ${m.b.toFixed(1)} ms at ${large})`,
); );
}
}
return slow;
} }
describe("growth stays near linear", () => { describe("growth stays near linear", () => {