Performance Budgets In CI
A performance budget in continuous integration is an automated check that fails a build when a measured speed metric crosses a set limit. The most common metric for services is p95 latency: the response time that 95% of requests beat and 5% exceed. If a checkout endpoint has a p95 of 310 ms, one request in twenty takes longer than that.
Teams choose p95 over the average because averages hide slow outliers. A change that leaves the mean at 120 ms while pushing the slowest 5% from 310 ms to 900 ms hurts real users and never moves the average much.
In practice the gate is a few lines of configuration. In k6, a threshold such as p(95) below 400 on request duration makes the tool exit with a non-zero code, and the pipeline step fails. Lighthouse CI does the same for page metrics through assertions on Largest Contentful Paint or Total Blocking Time.
Writing the rule takes five minutes. Keeping it trustworthy is the work.
Why Thresholds Flake
The usual first attempt copies a number from a production dashboard into the pipeline. Production p95 reflects real traffic on tuned hardware. A CI job runs on a shared virtual machine with two to four cores, a cold cache, and neighbours competing for CPU. Run-to-run variation of 10% to 30% on hosted runners is ordinary, so a limit set 5% above yesterday’s reading fails at random.
Sample size is the second trap. A p95 computed from 100 requests rests on the five slowest. One garbage-collection pause among them shifts the result by tens of milliseconds. With 2,000 requests, 100 samples sit in the tail and the estimate settles down.
Third, many load tools understate tail latency through coordinated omission. A closed-loop generator waits for each response before sending the next request, so when the server stalls, the tool sends fewer requests and never records the queueing delay real users would feel. The result looks better than the truth, which, frankly, is the last thing a gate should do.
The gate also depends on its surroundings. The test needs a production-like build, a seeded database of realistic size, stubbed or stable third-party calls, and a load generator that does not share a CPU with the system under test. Results need somewhere to live between runs, such as a time-series store or a tracking service like Bencher, or there is no baseline to compare against.
Setting Limits That Hold
Measure The Noise First
Before choosing any limit, run the unchanged main branch 20 times on the CI runner and record p95 each time. Calculate the median and the standard deviation of those 20 values. Suppose the median is 310 ms and the standard deviation is 12 ms. That 12 ms is your noise floor. A gate cannot reliably detect a regression smaller than about two to three times that figure, here roughly 25 to 35 ms.
Derive The Limit From Data
Set the hard limit at the baseline median plus three standard deviations plus the regression you are willing to accept. With a 310 ms baseline, 12 ms deviation, and a tolerance of 15 ms, the limit lands near 360 ms. Expect a false failure rate well under 1% at that margin if noise is roughly normal. Add a softer warning at baseline plus two deviations that posts a comment on the pull request without blocking it. You measure the spread, add the tolerance, and the number defends itself in review.
Compare Against A Baseline Run
Absolute limits drift as runner hardware changes. A relative gate runs the base commit and the candidate commit in the same job, on the same machine, interleaved, and fails only if the candidate’s p95 is worse by more than a set percentage, commonly 5% to 10%. For small differences, apply a rank-based test such as Mann-Whitney U across repeated runs instead of comparing two single numbers. This doubles job time and removes most machine-to-machine variance.
Control The Test Conditions
Discard a warm-up period, typically the first 30 to 60 seconds, so just-in-time compilation and connection pools settle. Drive load at a fixed arrival rate, using k6’s constant-arrival-rate executor or wrk2, to avoid coordinated omission. Aim for at least 1,000 requests per gated endpoint. Pin the job to a dedicated or self-hosted runner where budget permits. Where it does not, tools such as CodSpeed count CPU instructions under simulation instead of timing wall-clock, trading realism for repeatability.
Two Pipeline Examples
An API team. A six-engineer team gates its order-search endpoint with a k6 test: 90 seconds at 40 requests per second after a 30-second warm-up, about 3,600 measured requests. The first limit, 300 ms copied from production, fails one build in four. They run the 20-build baseline and find a median p95 of 340 ms on their runner with a deviation of 18 ms. The new hard limit is 410 ms, with a warning at 376 ms. Over the following month the gate fails twice. One failure traces to a missing database index in a migration. The other was a noisy runner, cleared by an automatic single retry. Not perfect. Trusted.
A frontend team. A marketing site uses Lighthouse CI with a budget of 2.5 seconds for Largest Contentful Paint under simulated mobile throttling. Single runs swing by 400 ms. The team switches to five runs per URL and asserts on the median, a setting Lighthouse CI supports directly, and adds a JavaScript bundle size budget of 180 KB compressed as a second gate. Bundle size is deterministic, so it catches most regressions before the timing check has anything to say. The timing budget stays as a warning for two weeks, then becomes blocking once its false alarm rate is known.
Gate Types Compared
| Gate Type | How It Decides | Strength | Weakness |
|---|---|---|---|
| Absolute p95 limit | Fails above a fixed value, such as 360 ms | Simple to read and explain | Breaks when runner hardware changes; needs periodic recalibration |
| Relative to base commit | Fails if candidate is 5% to 10% slower in the same job | Cancels most machine variance | Doubles run time; slow drift across many small changes slips through |
| Statistical test | Rank test across repeated runs of both versions | Detects smaller regressions with a known error rate | Needs 10 or more runs per side; harder to explain |
| Deterministic proxy | Bundle size, query count, or instruction count | No timing noise at all | Measures a cause, not the latency users feel |
| Trend alert | Flags a shift in the rolling median over recent builds | Catches gradual creep | Reports after merge, so it cannot block |
Most durable setups combine two rows: a deterministic proxy or relative gate that blocks merges, and a trend alert that watches for drift.
Common Mistakes
Gating on p99 with a short test. At 1,000 requests the 99th percentile rests on ten samples. Use p95 for the gate and record p99 for information until test length supports it.
Retrying until green. One automatic retry to filter runner noise is reasonable. Three retries turn a 30% failure chance into a 3% one and hide real regressions the same way.
Raising the limit after every failure. If the number moves each time someone complains, it stops being a budget. Change it only with a new baseline measurement attached to the pull request.
Testing against an empty database. A query that returns in 4 ms on 50 rows may take 400 ms on five million. Seed data at a realistic scale, and it rarely takes as long to set up as teams fear.
Running the load generator on the same two-core runner as the service. The tool and the target then compete for CPU, and the measurement reflects that contest instead of the code.
FAQ
What is a good p95 latency threshold?
There is no universal figure. Derive it from your own baseline: median p95 on the CI runner plus three standard deviations plus the regression you accept. For web pages, Google’s Core Web Vitals use 2.5 seconds for Largest Contentful Paint as a reference point.
Why use p95 instead of average response time?
The average can stay flat while the slowest requests get much worse. p95 tracks the experience of the unluckiest one in twenty users and is stable enough to measure in a short test.
How many requests do I need for a stable p95?
At least 1,000 per endpoint, which puts 50 samples in the tail. Several thousand is better. Below a few hundred, the figure swings too much to gate on.
How do I stop performance tests from being flaky in CI?
Measure run-to-run noise, set limits outside it, add a warm-up, use fixed arrival rates, compare against the base commit in the same job, and prefer dedicated runners.
Should a performance budget block merges or only warn?
Start with warnings for two to four weeks to learn the false alarm rate. Promote to blocking once failures are rare and each one has a traceable cause.
Author’s Insight
Across tool documentation and published engineering write-ups, failed performance gates share one pattern: the limit was chosen before the noise was measured. The statistics involved are modest, a median, a standard deviation, a minimum sample count, yet skipping them turns the gate into a coin toss that developers learn to ignore. Deterministic proxies such as bundle size and query counts deserve more use than they get, because they block cleanly and leave timing checks to confirm instead of decide. A budget’s worth shows in how rarely it is overridden, and that depends more on calibration than on the tool chosen.
Key Takeaways
- A CI performance budget fails the build when a metric such as p95 latency crosses a limit.
- Production numbers do not transfer to shared runners, where 10% to 30% variation is normal.
- Run the baseline 20 times, then set the limit at median plus three standard deviations plus accepted regression.
- Collect at least 1,000 requests per endpoint, discard warm-up, and drive load at a fixed arrival rate.
- Relative comparison against the base commit removes most hardware variance at the cost of longer jobs.
- Pair a blocking gate with a trend alert, begin in warning mode, and change limits only with fresh measurements.