test(sse): time the specificity detector on warm runs, keep the 5 ms bound (#17159)

The `<5ms for 20 messages` check timed one call. Run alone, that call is cold
(JIT warm-up) and took 5.3-7.9 ms in 6 of 10 local runs; inside the whole file
it passes only because earlier cases happen to warm the code, and on busy hosted
runners it still failed 4 of the last 32 PR Vitest jobs (18.23 ms, 5.26 ms), the
most frequent Vitest red on PRs.

Warm up with three calls, then assert on the fastest of ten timed runs. A pause
only adds time, so the bound still catches a slower analysis: with 6 ms of busy
work injected into analyzeSpecificity the test fails (6.33 ms). Run alone it now
passes 10/10, the whole file 10/10, and 5/5 when sharing one core with a busy loop.
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-10-19 09:57:10 -03:00
committed by GitHub
parent 84cf629bec
commit da38fc4f9c
2 changed files with 13 additions and 4 deletions

View File

@@ -0,0 +1 @@
- **test(sse):** the specificity detector's `<5ms for 20 messages` check measures the fastest of ten warm runs instead of one cold call, which a busy CI runner stretched to 5-18 ms in 4 of 32 PR Vitest jobs; the 5 ms bound is unchanged.

View File

@@ -230,10 +230,18 @@ describe("SpecificityDetector", () => {
content:
"Write a function that implements merge sort with O(n log n) complexity. Step 1: divide array. Therefore, use recursion.",
});
const t0 = performance.now();
analyzeSpecificity({ messages: msgs });
const elapsed = performance.now() - t0;
expect(elapsed, `Expected < 5ms, got ${elapsed.toFixed(2)}ms`).toBeLessThan(5);
// A single cold call pays JIT warm-up and any scheduler or GC pause of a busy CI runner:
// run alone it took 5.3-7.9 ms in 6 of 10 runs, and it failed 4 of 32 PR Vitest jobs
// (18.23 ms, 5.26 ms). Warm up, then keep the fastest of ten runs: a pause only adds
// time, so the fastest run still crosses 5 ms if the analysis itself gets slower.
for (let i = 0; i < 3; i++) analyzeSpecificity({ messages: msgs });
let fastest = Infinity;
for (let i = 0; i < 10; i++) {
const t0 = performance.now();
analyzeSpecificity({ messages: msgs });
fastest = Math.min(fastest, performance.now() - t0);
}
expect(fastest, `Expected < 5ms, got ${fastest.toFixed(2)}ms`).toBeLessThan(5);
});
});
});