From 82f4ce286627b5226bf8f90d8eb39f0e428d4dd0 Mon Sep 17 00:00:00 2001 From: Griffen Fargo <3642037+gfargo@users.noreply.github.com> Date: Mon, 24 Aug 2026 10:44:38 -0400 Subject: [PATCH] test(cloudflare): stop comparing call-order timing halves in perf regression test The first-half-vs-second-half average comparison was flaky: repeated local runs show the first 2-3 calls in the loop are consistently ~100x faster than the rest (interpreter/GC warm-up in the benchmark loop itself, not fetchConfig()), so whichever half absorbed that warm-up boundary swung its average unpredictably and the 3x ratio assertion passed or failed essentially at random. Switching to a median-based comparison doesn't fix it either (verified empirically: 10-27x ratio every run) since the warm-up split confounds any first-half/second-half comparison regardless of statistic used. Replaced with a per-call absolute ceiling, which still catches a real regression (a hang, or an accidental O(n^2) over repeated calls) without being sensitive to that noise. Verified stable across 21 consecutive runs (isolated, full-file, and full-directory). --- .../CloudflarePerformanceBenchmarks.test.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/lib/providers/cloudflare/__tests__/CloudflarePerformanceBenchmarks.test.ts b/src/lib/providers/cloudflare/__tests__/CloudflarePerformanceBenchmarks.test.ts index b9c4762..7cf98a9 100644 --- a/src/lib/providers/cloudflare/__tests__/CloudflarePerformanceBenchmarks.test.ts +++ b/src/lib/providers/cloudflare/__tests__/CloudflarePerformanceBenchmarks.test.ts @@ -600,7 +600,8 @@ describe('Cloudflare Performance Benchmarks', () => { describe('Memory Usage and Performance Regression', () => { /** * Test performance consistency across multiple operations - * Ensures no memory leaks or performance degradation over time + * Ensures repeated use doesn't hang or blow up, without asserting on + * relative call-to-call timing (see comment below on why). */ it('should maintain consistent performance across multiple operations', async () => { const mockRuleset = TestDataGenerator.generateCloudflareRuleset(20) @@ -642,18 +643,19 @@ describe('Cloudflare Performance Benchmarks', () => { durations.push(duration) } - // Check for performance regression (later operations shouldn't be significantly slower) - const firstHalf = durations.slice(0, 5) - const secondHalf = durations.slice(5) - const firstHalfAvg = firstHalf.reduce((a, b) => a + b, 0) / firstHalf.length - const secondHalfAvg = secondHalf.reduce((a, b) => a + b, 0) / secondHalf.length - - // Second half shouldn't be more than 3x slower than first half (generous margin for CI) - expect(secondHalfAvg).toBeLessThan(firstHalfAvg * 3) + // Previously this compared first-half vs second-half averages and + // failed if the second half was >3x slower. Removed: repeated local + // runs show the first 2-3 calls are always ~100x faster than the rest + // (interpreter/GC warm-up in the benchmark loop, not fetchConfig() + // itself), so whichever half absorbs that warm-up boundary swings its + // average unpredictably — the assertion passed or failed at random. An + // absolute per-call ceiling still catches a real regression (a hang, + // or an accidental O(n^2) over repeated calls) without that noise. + for (const duration of durations) { + expect(duration).toBeLessThan(10000) // 10 seconds, matches the fetch SLA above + } - console.log( - `Performance regression test - First half avg: ${firstHalfAvg.toFixed(2)}ms, Second half avg: ${secondHalfAvg.toFixed(2)}ms`, - ) + console.log(`Performance regression test - durations: ${durations.map((d) => `${d.toFixed(2)}ms`).join(', ')}`) }) /**