@@ -2,6 +2,16 @@ import { beforeEach, describe, expect, it } from 'bun:test'
22import { CSSGenerator } from '../src/generator'
33import { defaultConfig } from '../src/config'
44
5+ // GitHub-hosted runners share CPU with other workloads and routinely show
6+ // 1.3-1.6x variance on sub-20ms samples. Keep local regression budgets strict,
7+ // while allowing one scheduler-sized interruption in CI. Correctness and
8+ // algorithmic guards below remain identical in both environments.
9+ const PERFORMANCE_BUDGET_MULTIPLIER = process . env . CI ? 2 : 1
10+
11+ function performanceBudget ( localMilliseconds : number ) : number {
12+ return localMilliseconds * PERFORMANCE_BUDGET_MULTIPLIER
13+ }
14+
515/**
616 * Performance Regression Tests
717 *
@@ -53,7 +63,7 @@ describe('Performance Regression Tests', () => {
5363 const duration = performance . now ( ) - start
5464
5565 // Current: ~2ms, threshold: 5ms (2.5x margin for safety)
56- expect ( duration ) . toBeLessThan ( 5 ) // 5ms
66+ expect ( duration ) . toBeLessThan ( performanceBudget ( 5 ) ) // 5ms locally
5767 } )
5868 } )
5969
@@ -74,7 +84,7 @@ describe('Performance Regression Tests', () => {
7484 const duration = performance . now ( ) - start
7585
7686 // Current: ~0.2ms locally, up to ~1.5ms on CI runners
77- expect ( duration ) . toBeLessThan ( 3 ) // 3ms
87+ expect ( duration ) . toBeLessThan ( performanceBudget ( 3 ) ) // 3ms locally
7888 } )
7989 } )
8090
@@ -97,7 +107,7 @@ describe('Performance Regression Tests', () => {
97107 const duration = performance . now ( ) - start
98108
99109 // Current: ~0.05ms locally, up to ~1ms on CI runners
100- expect ( duration ) . toBeLessThan ( 2 ) // 2ms
110+ expect ( duration ) . toBeLessThan ( performanceBudget ( 2 ) ) // 2ms locally
101111 } )
102112 } )
103113
@@ -122,7 +132,7 @@ describe('Performance Regression Tests', () => {
122132 const duration = performance . now ( ) - start
123133
124134 // Current: ~0.5ms, threshold: 2ms
125- expect ( duration ) . toBeLessThan ( 2 ) // 2ms
135+ expect ( duration ) . toBeLessThan ( performanceBudget ( 2 ) ) // 2ms locally
126136 } )
127137 } )
128138
@@ -140,7 +150,7 @@ describe('Performance Regression Tests', () => {
140150 const duration = performance . now ( ) - start
141151
142152 // Current: ~5ms, threshold: 15ms (3x margin)
143- expect ( duration ) . toBeLessThan ( 15 ) // 15ms
153+ expect ( duration ) . toBeLessThan ( performanceBudget ( 15 ) ) // 15ms locally
144154 } )
145155 } )
146156
@@ -162,7 +172,7 @@ describe('Performance Regression Tests', () => {
162172 const duration = performance . now ( ) - start
163173
164174 // Current: ~2ms, threshold: 5ms
165- expect ( duration ) . toBeLessThan ( 5 ) // 5ms
175+ expect ( duration ) . toBeLessThan ( performanceBudget ( 5 ) ) // 5ms locally
166176 expect ( css . length ) . toBeGreaterThan ( 0 )
167177 } )
168178 } )
@@ -188,7 +198,7 @@ describe('Performance Regression Tests', () => {
188198 const duration = performance . now ( ) - start
189199
190200 // Current: ~1ms, threshold: 3ms
191- expect ( duration ) . toBeLessThan ( 3 ) // 3ms
201+ expect ( duration ) . toBeLessThan ( performanceBudget ( 3 ) ) // 3ms locally
192202 } )
193203 } )
194204
@@ -210,7 +220,7 @@ describe('Performance Regression Tests', () => {
210220 const duration = performance . now ( ) - start
211221
212222 // Current: ~4ms locally, up to ~16ms on CI runners
213- expect ( duration ) . toBeLessThan ( 25 ) // 25ms
223+ expect ( duration ) . toBeLessThan ( performanceBudget ( 25 ) ) // 25ms locally
214224 } )
215225 } )
216226
@@ -232,7 +242,7 @@ describe('Performance Regression Tests', () => {
232242 const duration = performance . now ( ) - start
233243
234244 // Current: ~2.5ms locally, up to ~10ms on CI runners
235- expect ( duration ) . toBeLessThan ( 16 ) // 16ms
245+ expect ( duration ) . toBeLessThan ( performanceBudget ( 16 ) ) // 16ms locally
236246 } )
237247 } )
238248
@@ -252,7 +262,7 @@ describe('Performance Regression Tests', () => {
252262 const duration = performance . now ( ) - start
253263
254264 // Current: ~0.12ms, threshold: 0.5ms (this should be VERY fast due to caching)
255- expect ( duration ) . toBeLessThan ( 0.5 ) // 0.5ms
265+ expect ( duration ) . toBeLessThan ( performanceBudget ( 0.5 ) ) // 0.5ms locally
256266 } )
257267 } )
258268
@@ -276,34 +286,29 @@ describe('Performance Regression Tests', () => {
276286
277287 // These should be EXTREMELY fast (< 0.05ms locally, up to ~1ms on CI runners)
278288 // because they match in the first 3 rules
279- expect ( duration ) . toBeLessThan ( 2 ) // 2ms
289+ expect ( duration ) . toBeLessThan ( performanceBudget ( 2 ) ) // 2ms locally
280290 } )
281291 } )
282292
283293 describe ( 'Cache Effectiveness' , ( ) => {
284- it ( 'should benefit from parse cache on repeated patterns ' , ( ) => {
294+ it ( 'should keep repeated patterns within the cached budget ' , ( ) => {
285295 const pattern = 'hover:bg-blue-500'
286296
287- // First pass - no cache
288- const gen1 = new CSSGenerator ( defaultConfig )
289- const start1 = performance . now ( )
290- for ( let i = 0 ; i < 100 ; i ++ ) {
291- gen1 . generate ( pattern )
292- }
293- const duration1 = performance . now ( ) - start1
297+ // Prime the global parse cache with a separate generator.
298+ const warmGenerator = new CSSGenerator ( defaultConfig )
299+ warmGenerator . generate ( pattern )
294300
295- // Second pass - with cache (different generator, but parse cache is global)
296- const gen2 = new CSSGenerator ( defaultConfig )
297- const start2 = performance . now ( )
301+ const cachedGenerator = new CSSGenerator ( defaultConfig )
302+ const start = performance . now ( )
298303 for ( let i = 0 ; i < 100 ; i ++ ) {
299- gen2 . generate ( pattern )
304+ cachedGenerator . generate ( pattern )
300305 }
301- const duration2 = performance . now ( ) - start2
306+ const duration = performance . now ( ) - start
302307
303- // Second pass should be faster or equal (cache helps)
304- // Due to class cache, second pass should be near-instant
305- expect ( duration2 ) . toBeLessThanOrEqual ( duration1 )
306- expect ( duration2 ) . toBeLessThan ( 0.05 ) // Should be < 50µs with full caching
308+ // Avoid comparing two sub-millisecond samples: scheduler noise can make
309+ // either one appear faster. The absolute cached-path budget is the
310+ // meaningful regression guard.
311+ expect ( duration ) . toBeLessThan ( performanceBudget ( 0.05 ) ) // < 50µs locally with full caching
307312 } )
308313
309314 it ( 'should handle class cache efficiently' , ( ) => {
@@ -324,7 +329,7 @@ describe('Performance Regression Tests', () => {
324329 const duration = performance . now ( ) - start
325330
326331 // 4000 cached lookups should be fast
327- expect ( duration ) . toBeLessThan ( 1 ) // < 1ms for 4000 cached lookups
332+ expect ( duration ) . toBeLessThan ( performanceBudget ( 1 ) ) // < 1ms locally for 4000 cached lookups
328333 } )
329334 } )
330335
@@ -370,7 +375,7 @@ describe('Performance Regression Tests', () => {
370375
371376 // Average per iteration should be < 20µs
372377 // If someone moves rules back, this will break
373- expect ( avgPerIteration ) . toBeLessThan ( 0.02 ) // 20 microseconds per iteration
378+ expect ( avgPerIteration ) . toBeLessThan ( performanceBudget ( 0.02 ) ) // 20µs locally per iteration
374379 } )
375380 } )
376381} )
0 commit comments