@@ -5,9 +5,18 @@ import { parseMd } from "../../parse.js";
55import { resolveMdOcPath as resolveOcPath } from "../../resolve.js" ;
66
77const perfBudgetMultiplier = process . env . CI ? 4 : 1 ;
8+ const perfSampleCount = 3 ;
89
9- function expectWithinPerfBudget ( elapsedMs : number , localBudgetMs : number ) {
10- expect ( elapsedMs ) . toBeLessThan ( localBudgetMs * perfBudgetMultiplier ) ;
10+ function expectWithinPerfBudget ( run : ( ) => void , localBudgetMs : number ) {
11+ // Loaded shared-vCPU CI can pause any single sample, so use the best of a few runs.
12+ // The minimum still catches consistently slow regressions without treating contention as one.
13+ let bestElapsedMs = Number . POSITIVE_INFINITY ;
14+ for ( let sample = 0 ; sample < perfSampleCount ; sample ++ ) {
15+ const start = performance . now ( ) ;
16+ run ( ) ;
17+ bestElapsedMs = Math . min ( bestElapsedMs , performance . now ( ) - start ) ;
18+ }
19+ expect ( bestElapsedMs ) . toBeLessThan ( localBudgetMs * perfBudgetMultiplier ) ;
1120}
1221
1322describe ( "perf + determinism" , ( ) => {
@@ -20,32 +29,27 @@ describe("perf + determinism", () => {
2029 }
2130 }
2231 const raw = lines . join ( "\n" ) ;
23- const start = performance . now ( ) ;
24- parseMd ( raw ) ;
25- const elapsed = performance . now ( ) - start ;
26- expectWithinPerfBudget ( elapsed , 200 ) ;
32+ expectWithinPerfBudget ( ( ) => parseMd ( raw ) , 200 ) ;
2733 } ) ;
2834
2935 it ( "parses 1000 small files in under 500 ms" , ( ) => {
3036 const raw = `## H\n- a\n- b: c\n## I\n- d\n` ;
31- const start = performance . now ( ) ;
32- for ( let i = 0 ; i < 1000 ; i ++ ) {
33- parseMd ( raw ) ;
34- }
35- const elapsed = performance . now ( ) - start ;
36- expectWithinPerfBudget ( elapsed , 500 ) ;
37+ expectWithinPerfBudget ( ( ) => {
38+ for ( let i = 0 ; i < 1000 ; i ++ ) {
39+ parseMd ( raw ) ;
40+ }
41+ } , 500 ) ;
3742 } ) ;
3843
3944 it ( "100k OcPath resolutions on parsed AST in under 500 ms" , ( ) => {
4045 const raw = `## A\n- a1\n- a2\n## B\n- b1\n- b2\n## C\n- c1: cv\n` ;
4146 const { ast } = parseMd ( raw ) ;
4247 const path = { file : "X.md" , section : "b" , item : "b1" } ;
43- const start = performance . now ( ) ;
44- for ( let i = 0 ; i < 100_000 ; i ++ ) {
45- resolveOcPath ( ast , path ) ;
46- }
47- const elapsed = performance . now ( ) - start ;
48- expectWithinPerfBudget ( elapsed , 500 ) ;
48+ expectWithinPerfBudget ( ( ) => {
49+ for ( let i = 0 ; i < 100_000 ; i ++ ) {
50+ resolveOcPath ( ast , path ) ;
51+ }
52+ } , 500 ) ;
4953 } ) ;
5054
5155 it ( "same input → byte-identical AST.raw across runs" , ( ) => {
@@ -115,11 +119,11 @@ describe("perf + determinism", () => {
115119 lines . push ( "" ) ;
116120 }
117121 const raw = lines . join ( "\n" ) ;
118- const start = performance . now ( ) ;
119- const { ast } = parseMd ( raw ) ;
120- const out = emitMd ( ast ) ;
121- const elapsed = performance . now ( ) - start ;
122+ let out = "" ;
123+ expectWithinPerfBudget ( ( ) => {
124+ const { ast } = parseMd ( raw ) ;
125+ out = emitMd ( ast ) ;
126+ } , 100 ) ;
122127 expect ( out ) . toBe ( raw ) ;
123- expectWithinPerfBudget ( elapsed , 100 ) ;
124128 } ) ;
125129} ) ;
0 commit comments