From ffa45b27f6447312bded0a95c9312aa4b624f0e3 Mon Sep 17 00:00:00 2001 From: abiwab Date: Sat, 21 Nov 2026 21:09:13 +0100 Subject: [PATCH] test(fuzz): count a stage as slow only if it fails three measurements in a row --- fuzz/tests/pathological.test.ts | 50 ++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/fuzz/tests/pathological.test.ts b/fuzz/tests/pathological.test.ts index ce1eb12e..67d88d4f 100644 --- a/fuzz/tests/pathological.test.ts +++ b/fuzz/tests/pathological.test.ts @@ -52,6 +52,8 @@ const GENERATORS: Record string> = { const SMALL = 3_125; const LARGE = 50_000; 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. */ const NOISE_FLOOR_MS = 20; @@ -63,23 +65,51 @@ const NOISE_FLOOR_MS = 20; */ const KNOWN_SLOW: Record = {}; -function exponents(generate: (n: number) => string, scale: number): string[] { - const small = Math.round(SMALL * scale); - const large = Math.round(LARGE * scale); +/** Growth exponent of each stage above the noise floor, from one measurement. */ +function measureExponents( + generate: (n: number) => string, + small: number, + large: number, +): Partial> { measure(generate(small)); // warm-up const a = measure(generate(small)); const b = measure(generate(large)); - const slow: string[] = []; + const out: Partial> = {}; for (const stage of ["parse", "compile", "format"] as const) { if (b[stage] < NOISE_FLOOR_MS) continue; const k = Math.log(b[stage] / a[stage]) / Math.log(large / small); - if (k > MAX_EXPONENT) { - slow.push( - `${stage}: n^${k.toFixed(2)} (${a[stage].toFixed(1)} ms at ${small}, ${b[stage].toFixed(1)} ms at ${large})`, - ); - } + out[stage] = { k, a: a[stage], b: b[stage] }; } - return slow; + 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> | 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})`, + ); } describe("growth stays near linear", () => { -- 2.54.0