23.12 Run comparison and the regression gate
The load generator turns a run into numbers. Nothing yet turns two runs into a decision. CI needs to fail a pull request that makes TTFT worse, and when a performance incident lands on twenty commits, you want to ask which one did it. git bisect run can answer that only if one command exits 0 for “fine” and 1 for “regressed”.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Pull request (PR): a proposed change, made of a branch, a description, and a diff against a base branch. Read more: 6.2 Code review in Software Engineering
- TTFT (time to first token): time from a request’s arrival to its first generated token. Read more: 23.4 Chunked prefill (mixed prefill + decode batches)
- p-value: the fraction of null arrangements at least as extreme as the observed one, with a correction. Read more: 29.4 Paired A/B experiments
- nearest-rank percentile: the value at rank of the sorted values. Read more: 1.11 SQL and SQLite: schema, transactions, WAL mode, indexes, aggregates in Programming and Algorithms
- Standard error: , the estimated standard deviation of the sample mean. Read more: 13.5 LLN, CLT, confidence intervals, bootstrap in Mathematics
- Service Level Objective (SLO): a target for a defined measure of service behavior over a specified window. Read more: Chapter 13: Engineering Acronyms in Software Engineering
- Confidence interval: a procedure that, over repeated samples, produces intervals covering the fixed true parameter at the stated rate. Read more: 13.11 Probability problem set, part c: limit theorems and confidence intervals in Mathematics
The difficulty is that latency is noisy. Two runs of the same build differ by several percent, so “head p95 > base p95” fails half of all pull requests, and “head p95 > 1.05 base p95” still fails a few percent of identical builds while missing real regressions hidden in noise. This section builds the gate that does better: an effect-size budget combined with a significance test.
This section is optional. Only the {loadgen} compare entry point calls it, and the CI pipeline in Systems Engineering runs its perf-gate job only when the workflow has one. It builds on the report, histograms, and rng.Stream of the load generator, and re-implements in Go the permutation test from hypothesis tests in Mathematics.
The input format is formats/loadgen-report.schema.json, and the {loadgen} compare role is defined in spec/cli-roles.md. The solution is go/loadgen/compare/compare.go: ParseRegress and ParseMetric, Samples (histogram reports expanded into samples), Stat.Value (nearest-rank statistics by selection), PermutationTest (a seeded one-sided two-sample permutation test), and Gate / Compare, which fail a head run only when it is worse beyond the budget and significantly so. The loadgen compare verb lives in go/cmd/loadgen and runs as:
loadgen compare <base.json> <head.json> --metric ttft_p95 --max-regress 5% [--alpha 0.05] [--permutations 1000] [--seed 0]It reads the two reports, calls compare.Compare, prints the verdict as its final JSON line, and exits 1 when Regressed.
Learning objectives
- Explain why a regression gate needs both an effect-size budget and a significance test.
- Run a one-sided permutation test on a percentile or mean, and compute its p-value so that it is never zero.
- Expand histogram reports into samples, including 0/1 samples for error rate.
- Compute nearest-rank statistics by selection without disturbing the caller’s data.
Two claims, not one
Section titled “Two claims, not one”Here is the notation.
| Symbol | Meaning | Type |
|---|---|---|
| , | base and head samples of one metric (ms, or 0/1 errors) | []float64 |
| the statistic: nearest-rank percentile or mean | function | |
| relative change, worse when positive | float64 | |
the budget (--max-regress 5% is 0.05) | float64 | |
| random relabelings; the significance level (0.05) |
A gate must make two separate claims. “Worse by more than we tolerate” is about effect size. “Not explained by noise” is about evidence. Either alone fails in a predictable way, so the verdict is the conjunction:
The budget keeps tiny real changes from failing CI: a 2% slowdown on a huge sample is significant but allowed. The test keeps big apparent changes on tiny samples from failing CI: one request each, ten times slower, gives . With a base statistic of 0 (no errors), any rise is .
Samples from a report
Section titled “Samples from a report”A report does not hold raw samples. It keeps each latency as histogram buckets [upper_ms, count], so a bucket becomes count copies of its upper bound, which is within 1/128 of the true values. That keeps the sample size, which drives the test’s power. error_rate becomes one 1 per failed request and one 0 per other request, so the same test covers it.
The permutation test
Section titled “The permutation test”Latencies are not normal. They are skewed, multi-modal, and heavy-tailed, so a t-test’s assumptions do not hold, and a percentile has no simple standard error. A permutation test needs only exchangeability under the null.
If base and head come from the same distribution, the labels are arbitrary: every way of choosing which of the pooled samples are “base” is equally likely. So observe . Draw random relabelings (Fisher-Yates with rng.Stream(seed, shuffle) from the load generator), compute for each, and count those with . The one-sided p-value is
The observed labeling is one of the possible ones, so it is counted, and : a Monte Carlo p-value must not claim certainty. Only “worse” counts. A head that got faster has and near 1, because a faster head is never a regression.
The verdict must also repeat: CI and git bisect run need the same answer on the same files, which is why the shuffles draw from a seeded stream.
Statistics by selection
Section titled “Statistics by selection”The nearest-rank percentile is the -th smallest value, the same rule as the load generator’s histogram, so the gate and the report agree. The statistic is computed thousands of times, since each of relabelings needs it on a fresh split. So the gate uses quickselect (expected ) on a copy, never sorting or reordering the caller’s slice.
Working it by hand
Section titled “Working it by hand”Take base e2e samples ms, head ms, metric e2e_mean, and a budget of 5%.
- , , so .
- . There are ways to pick three of the six values as “head”, and only gives a difference of 30 or more (every other choice moves a smaller value into the head mean). So the exact , and 20000 random relabelings estimate it within 0.005.
- is not below . Three samples cannot establish a regression, however large the change. Not regressed.
With 400 samples per run instead, a 10% shift gives far below 0.05 and : regressed.
Designing the interface
Section titled “Designing the interface”The whole gate is one small package:
type Stat struct { Name string; Q float64; Mean bool }func (s Stat) Value(xs []float64) float64 // nearest rank or mean; xs unchanged; NaN when emptytype Options struct { Metric string; MaxRegress, Alpha float64; Permutations int; Seed uint64 } // Alpha 0.05, Permutations 1000 by defaulttype Verdict struct { Metric string; Base, Head, Delta, PValue float64; Regressed bool }func ParseRegress(s string) (float64, error) // "5%" or "0.05"func ParseMetric(m string) (string, Stat, error) // "ttft_p95" -> ("ttft_ms", p95); "error_rate"func Samples(r *loadgen.Report, metric string) ([]float64, error)func PermutationTest(base, head []float64, s Stat, n int, r *rng.PCG32) float64func Gate(base, head []float64, s Stat, o Options) (Verdict, error)func Compare(base, head *loadgen.Report, o Options) (Verdict, error)Gate works on samples and is what the tests exercise directly; Compare is Gate applied to two reports through ParseMetric and Samples.
Building it step by step
Section titled “Building it step by step”Reasoning from first principles
Section titled “Reasoning from first principles”The design follows from seven observations, each already motivated above.
- A gate must make two separate claims, so the verdict is .
- Latencies are not normal, so the test is a permutation test, which needs only exchangeability under the null.
- Only “worse” is a regression. The test statistic is , and only relabelings at least that large count.
- A Monte Carlo p-value must not claim certainty, so .
- The verdict must repeat, so the shuffles draw from a seeded
rng.Stream(seed, PurposeShuffle). - The report holds histograms, not raw samples. Each bucket becomes
countcopies ofupper_ms, and errors become 0/1 samples. - The statistic is computed thousands of times, so selection in expected , on a copy, beats sorting.
Parsing the gate’s inputs
Section titled “Parsing the gate’s inputs”Parsing is where a silent unit error would do the most damage. In compare.go:
// ParseRegress reads a budget: "5%" or "0.05" both give 0.05.func ParseRegress(s string) (float64, error) { s = strings.TrimSpace(s) pct := strings.HasSuffix(s, "%") v, err := strconv.ParseFloat(strings.TrimSuffix(s, "%"), 64) if err != nil || v < 0 || math.IsNaN(v) || math.IsInf(v, 0) { return 0, fmt.Errorf("compare: bad regression budget %q", s) } if pct { v /= 100 // "5%" must not become a budget of 5 (500%) } return v, nil}ParseMetric splits ttft_p95 at the last underscore into the report key ttft_ms and a Stat{Name: "p95", Q: 0.95}; error_rate maps to the mean of 0/1 samples.
Samples from a report
Section titled “Samples from a report”Samples expands either the error count or a histogram:
func Samples(r *loadgen.Report, metric string) ([]float64, error) { if metric == "error_rate" { // ... errors must be within [0, requests] out := make([]float64, r.Requests) for i := 0; i < r.Errors; i++ { out[i] = 1 // the mean of these is the error rate } return out, nil } rows, ok := r.Histogram[metric] // ... a missing metric is an error var out []float64 for _, b := range rows { n := int(b[1]) // ... the count must be a non-negative integer for i := 0; i < n; i++ { out = append(out, b[0]) // count copies of the bucket's upper bound } } return out, nil}The statistic by selection
Section titled “The statistic by selection”The statistic uses the load generator’s rank rule and selects on a copy:
func (s Stat) Value(xs []float64) float64 { if len(xs) == 0 { return math.NaN() } if s.Mean { // ... the plain mean } // the same rank rule as loadgen.Histogram.Quantile, so the gate and the report agree rank := int(math.Ceil(s.Q*float64(len(xs)) - 1e-9)) // ... clamp to [1, n] tmp := append([]float64(nil), xs...) // a copy: the caller's slice (the shuffled pool) is untouched return nth(tmp, rank-1)}nth is Hoare-partition quickselect with a median-of-three pivot, so already sorted input (common, since histogram expansion produces sorted runs) stays linear in expectation.
The permutation test
Section titled “The permutation test”With a statistic in hand, the test itself is a loop of shuffles:
func PermutationTest(base, head []float64, s Stat, n int, r *rng.PCG32) float64 { obs := s.Value(head) - s.Value(base) // positive: head is worse pool := make([]float64, 0, len(base)+len(head)) pool = append(append(pool, base...), head...) nb := len(base) count := 0 for i := 0; i < n; i++ { // a uniformly random relabeling: Fisher-Yates from the load generator's PCG32 r.Shuffle(len(pool), func(a, b int) { pool[a], pool[b] = pool[b], pool[a] }) // one-sided: only relabelings at least as bad as observed count if s.Value(pool[nb:])-s.Value(pool[:nb]) >= obs { count++ } } return float64(1+count) / float64(1+n) // the observed labeling is one of them: p > 0}The pool is shuffled in place each time, which is fine: a uniformly random shuffle of any permutation is again uniformly random, so the starting order does not matter.
The gate
Section titled “The gate”Finally, Gate combines effect size and evidence:
func Gate(base, head []float64, s Stat, o Options) (Verdict, error) { // ... both runs need samples; Alpha 0.05 and Permutations 1000 by default v := Verdict{Metric: o.Metric, Base: s.Value(base), Head: s.Value(head)} switch { case v.Base > 0: v.Delta = (v.Head - v.Base) / v.Base case v.Head > v.Base: v.Delta = math.Inf(1) // from 0 errors to some errors: an unbounded relative rise } // 0 to 0: Delta stays 0, no change v.PValue = PermutationTest(base, head, s, n, rng.Stream(o.Seed, rng.PurposeShuffle)) v.Regressed = v.Delta > o.MaxRegress && v.PValue < alpha // both conditions, always return v, nil}Run the hand-worked example through this code: Base = 20, Head = 50, Delta = 1.5; , because the only relabeling as bad as observed is the observed one, out of 20 equally likely. That is not < 0.05, so Regressed is false.
The loadgen compare verb in go/cmd/loadgen wraps this: it reads the two reports, calls compare.Compare, prints the verdict as its final JSON line, and exits 1 when Regressed, which is exactly what git bisect run needs.
The full solution is compare.go.
Why this design
Section titled “Why this design”The cost is expected: each of relabelings shuffles the pool and runs two selections. With 1000 relabelings and a few thousand samples that is well under a second, small next to the load runs it compares.
Why not a t-test or Mann-Whitney? A t-test compares means under approximate normality, and the gate usually needs a percentile. Mann-Whitney tests a shift in the whole distribution, not the statistic the SLO names. The permutation test uses exactly the statistic in the budget, with no distributional assumption.
Why the upper bound of each bucket rather than the midpoint? It is what the report’s percentiles already use, so S(head) equals the report’s own p95. And the shift from the true value is the same for base and head, so it cancels in the comparison.
Two tradeoffs are accepted. A Monte Carlo has resolution ; with and that is ample. And expanded histograms mean that samples within a bucket tie; ties make the test slightly conservative, never anti-conservative.
What can go wrong
Section titled “What can go wrong”The tests draw samples from math/rand/v2 with fixed seeds (lognormal latencies), and the statistical case bounds its counts binomially at about . A correct solution handles each of these:
| Case | Checks | Why it matters downstream |
|---|---|---|
| The hand comparison | the worked statistics, , , not regressed | the worked example |
| Metric and budget parsing | 5% is 0.05; metric names map to report keys; bad input refused | the CLI’s flags mean what they say |
| Nearest rank, input untouched | 300 random slices with ties: equal to the sorted rank; input unchanged | the statistic and the split stay correct |
| Samples from the histogram | buckets to samples; error_rate to 0/1; a missing metric is an error | reports become test data |
| Seeded and one-sided | same seed, same p; faster head ; 30% slower | CI verdicts are reproducible |
| p is never zero | completely separated samples: | an honest bound, not certainty |
| Both budget and significance | 1 sample each: noise; 2% on 4000 samples: within budget; 10%: regressed | neither condition alone decides |
| A zero baseline | 0 to 0 errors is no change; 0 to 30 of 170 is and regressed | error-rate gates |
| Reports end to end | two load generator reports, default alpha and permutations: 30% slower fails, self passes | the CI path |
| Identical runs pass, shifts are caught | 100 pairs each: at most 12 false alarms, at least 82 catches | the gate’s two promises |
Most pitfalls come from dropping one half of the design. If the budget alone decides, identical builds fail CI several percent of the time; if significance alone decides, a real but allowed 2% change fails CI. Regressed requires both Delta > MaxRegress and PValue < alpha. A two-sided test makes a faster build fail CI; PermutationTest counts only relabelings with d* >= obs. And can be 0, claiming certainty from a finite sample; the solution uses .
Warning: Reading
5%as 5 sets a budget of 500%, and a head six times slower passes. The gate never complains; it just stops gating.ParseRegressdivides by 100 after a%.
The remaining pitfalls are in the data handling. Taking one sample per bucket ignores the counts, so the test sees only a handful of values and has no power; Samples appends count copies per bucket. Never shuffling makes every relabeling equal the observed one, so and nothing is ever caught; the pool is reshuffled with the seeded stream before every relabeling. And ranking by floor makes the statistic differ from the report’s percentile; Value uses ceil(q * n - 1e-9), the load generator’s rule.
Where this leads
Section titled “Where this leads”This section reads the load generator’s Report, its histograms, and rng.Stream for the shuffles. No later section builds on it as code; instead, the {loadgen} compare entry point is used directly. The optional perf-gate job of CI for the platform repo runs it on main against the last good run, and the performance regression bisect drill in Systems Engineering uses it with git bisect run to find the commit that caused a slowdown.
Production systems take the idea further. Bencher and Conbench provide continuous benchmarking with history and change-point detection, instead of one permutation test per metric. Go’s benchstat applies Mann-Whitney U across repeated runs, with confidence intervals. And change-point detection (E-divisive, as at MongoDB) replaces a fixed budget between two runs with finding when a series shifted.
Further reading
Section titled “Further reading”- Good, Permutation, Parametric, and Bootstrap Tests of Hypotheses, ch. 3.
- Kalibera and Jones, Rigorous Benchmarking in Reasonable Time (free).
- Mytkowicz et al., Producing Wrong Data Without Doing Anything Obviously Wrong.
- Bencher, Conbench, and benchstat.
- MongoDB’s change point detection.
Key terms
Section titled “Key terms”- Regression gate: a check that fails a head run only when it is worse than the base by more than a budget and the difference is unlikely under noise.
- Effect-size budget: the tolerated relative change , so that is “worse than we accept”.
- Permutation test: a test that compares the observed statistic difference with its distribution over random relabelings of the pooled samples.
- One-sided test: a test that counts only differences in the “worse” direction, so a faster head has near 1.
- Monte Carlo p-value: , an estimate from random relabelings that counts the observed one and is never zero.
- Quickselect: a selection algorithm that finds the -th smallest value in expected without fully sorting.
Summary
Section titled “Summary”- A regression is a head run worse than the base by more than the budget and unlikely under noise; either condition alone cries wolf.
- The permutation test asks how often a random relabeling of the pooled samples is at least as bad as what was observed; it needs no distributional assumption about latencies.
- One-sided: a faster head is never a regression, so its p-value is near 1.
- is never 0, and a seeded shuffle gives the same verdict twice on the same files.
- Over many simulated pairs, identical runs pass at least 95% of the time and a +10% shift is caught at least 90% of the time.
Review questions
Section titled “Review questions”-
Two builds are identical. How often does a budget-only gate (
Delta > 5%) fail them, and how does the permutation test prevent it?Answer
Two runs of the same build differ by several percent in p95, so a fixed 5% threshold fails some fraction of identical pairs, every time noise happens to exceed it. Requiring as well means the difference must be unusual among random relabelings of the pooled samples; for identical builds it usually is not, so the false-alarm rate drops to at most about .
-
Why is computed as and not ?
Answer
The observed labeling is itself one of the labelings under the null, so it belongs in both numerator and denominator. can be 0 for completely separated samples, which claims certainty from a finite random sample; the corrected form is at least , an honest bound, and is a valid p-value.
-
A head run is 30% faster. What does the test report, and why is that the right behaviour?
Answer
obsis negative, and almost every relabeling gives a difference above it, socountis close to and is near 1: not regressed. The question CI asks is “did this get worse?”; a two-sided test would flag the speedup as a significant change and fail a good pull request. -
Why does
Valuecopyxsbefore selecting?Answer
nthpermutes its input. In the permutation test,Valueis called on slices of the pool; reordering them would change which values are in the “base” and “head” halves mid-comparison and corrupt later relabelings, and callers ofValuewould see their data reordered. The copy keeps every call pure. -
Base has 0 errors, head has 30 of 170. What are and the verdict?
Answer
Base = 0,Head > 0, soDelta = +Inf, which exceeds any budget. With 30 ones among 170 head samples and none in the base, few random relabelings put that many errors in the head, so is far below 0.05: regressed. If both had 0 errors,Deltawould stay 0 and nothing would be flagged. -
In the hand-worked example, why is the exact p-value , and why does that mean “not regressed” even though the head mean is 2.5 times the base?
Answer
With six pooled values there are equally likely ways to label three of them “head”, and only the observed labeling gives a mean difference of at least 30. So , which is not strictly below . Three samples per run are simply too few to rule out noise, however large the change looks.