Skip to content

3.4 Benchmarks and perf gates

By the time you reach this section, the C kernels of AI Engineering are correct: the kernels chapter, from Cache-blocked, packed, batch-invariant matmul to Elementwise kernels in C, matched its oracles, and a real model runs through them. Correct is not the same as fast, and fast does not stay fast. Swap two loops in matmul.c while fixing an edge case and the answer is unchanged, every test passes, and decode is 14 times slower. No unit test can notice, because a unit test checks values.

Concepts from other sections (9)

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.

  • RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias. Read more: 13.1 Pre-LN and RMSNorm in AI Engineering
  • 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. Read more: 23.12 Run comparison and the regression gate in AI Engineering
  • Process group: the set of processes led by the child, signalled together with kill(-pgid, sig). Read more: 7.9.4 Failure tests and integration in Systems Engineering
  • Binary search: a search that keeps an interval known to contain the answer and halves it with one comparison per step, taking O(log⁡n)O(\log n) steps; it needs a monotonic predicate, not necessarily a sorted array. Read more: Chapter 5: Binary search in Programming and Algorithms
  • Dot product: u⋅v=∑iuivi=∥u∥∥v∥cos⁡θu \cdot v = \sum_i u_i v_i = \lVert u \rVert \lVert v \rVert \cos\theta; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3 in Mathematics
  • 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
  • Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller in Mathematics
  • 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
  • Mutation testing: judging a test suite by planting small faults (mutants) in the code and counting how many the suite catches; it measures whether tests check behavior, which line coverage cannot. Read more: Chapter 3: The Testing Mentality

From here on, the solution protects speed the way its earlier tests protect values: a benchmark that measures it, a budget that says how much it may move, and a gate that fails the change that breaks it. This section does all three on a small kata first, so the noise, the budgets, and the failure modes are all in front of you before the real kernels are on the line. It builds the idea of a benchmark and a gate from first principles, works the arithmetic by hand, then walks through the solution kernel by kernel and line by line through the gate. Try to predict each step before reading it.

You should already know how tests are judged by planted faults (TDD, unit tests, and mutation testing) and why a fast kernel must first be a right one (Oracles, golden and differential tests, gradcheck as a test). The kata’s matmul uses the tiling and loop order of Cache-blocked, packed, batch-invariant matmul, and its RMSNorm is the one from Elementwise kernels in C.

The solution has four files: kernels.c, a kata of four C kernels whose speed depends on how they are written (kata_matmul, kata_sum, kata_rmsnorm, kata_count_below); bench.c, the benchmark of them with ol_bench.h; budget.toml, the performance budget; and gate.py, the regression gate (regressed, run, build, metrics, main). The contract is the kata’s spec in kernels.h and the timer ol_bench.h. The gate is run as:

Terminal window
python3 course/solutions/primers/craft.06/gate.py \
--base course/solutions/primers/craft.06/kernels.c --head course/solutions/primers/craft.06/kernels.c \
--include course/contracts/c/include

Run it on an otherwise idle machine, since it times things. Every build and benchmark it starts runs in its own process group with a timeout.

Learning objectives

  • Write a benchmark with fixed inputs, a warm-up, repetitions, and a minimum estimator, and explain why the minimum.
  • Build a perf gate that compares base and head on the same machine, alternately, and needs no stored numbers.
  • Choose a budget that sits above the noise and well below the smallest real regression.
  • Name slowdowns that keep every output right and explain why each one is slow.

The notation for the rest of the section:

SymbolMeaningType
tit_ithe time of repetition ii, per callseconds
t^=min⁡iti\hat t = \min_i t_ithe benchmark’s estimateseconds
tb,tht_b, t_ht^\hat t for the base and the head kernels.cseconds
Δ=th/tb−1\Delta = t_h / t_b - 1the change; +1.0+1.0 is twice as slowfraction
β\betathe budget of a metric: the largest Δ\Delta allowedfraction in (0,1)(0, 1)

A call’s time is the work plus noise: other processes, frequency scaling, cache and TLB state, interrupts. Noise is one-sided: it can only add time. So ol_bench_best(fn, ctx, reps, min_s) calls fn once to warm caches and branch predictors, then runs reps repetitions, each calling fn as often as it takes to fill at least min_s seconds (a 5 µs call is timed over thousands of calls, not one), and returns the smallest per-call time. The mean would absorb every hiccup; the minimum is the closest any run came to the work alone.

Three rules make a benchmark mean something. Use fixed inputs: a formula of the index, never rand(), so the same work happens on every run and every machine. Keep the result by writing it to a volatile variable, because an optimizing compiler deletes a computation whose result is never used, and you would time an empty loop. Choose sizes that matter: big enough to take microseconds to milliseconds, so timer resolution and call overhead vanish, and small enough that a 1000x slower version still finishes in a second.

The benchmark prints its metrics as the last stdout line, one JSON object (ol_bench_done), so any tool can read them.

The obvious gate stores yesterday’s number and fails when today’s is slower. It breaks on the first machine change: a laptop on battery, a CI runner of a different generation, a busy neighbor. The robust gate measures base and head together. It builds the benchmark with each version, runs them alternately (base, head, base, head, …) a few times, keeps each side’s minimum, and compares. Drift over the run, such as a thermal throttle or a background job, hits both sides alike, and the machine’s absolute speed cancels in Δ\Delta. This is how Run comparison and the regression gate in the serving chapter and production systems such as Go’s benchstat work.

Absolute budgets still have a place. “At least half the GFLOP/s of a reference kernel timed on the same machine” answers “is it fast enough?”; the gate answers “did this change make it slower?”

A metric regresses when th>tb(1+β)t_h > t_b (1 + \beta). Choosing β\beta is a trade between two errors, and the two extremes show it:

Too small (5%)Too large (100%)
Noise (∣Δ∣\lvert \Delta \rvert up to about 10% between identical builds)false alarms: the gate is switched off within a weekfine
A 2x slowdown (Δ=+1.0\Delta = +1.0)caughtmissed: +1.0+1.0 is not more than 1.01.0

β=0.3\beta = 0.3 sits well above the noise and far below a doubling. A metric measured over microseconds (the binary search) is noisier and gets more room. The budget lives in a file next to the benchmark and is reviewed like code: loosening it is a decision someone signs.

The regressions a gate exists for are the ones no value test sees. Each of these leaves every output the same and costs 2x to 1000x:

ChangeWhy it is slow
matmul loop order i,j,ki, j, kthe inner loop walks BB down a column (stride nn) and is a dot product the compiler may not vectorize (float addition is not associative, so it may not reorder the sum)
tiles 2 columns widethe unit-stride inner loop is too short to vectorize and its overhead dominates
one accumulator in a sumeach add waits for the previous one (a 3 to 4 cycle dependency chain); eight independent partial sums keep the adder busy and vectorize
volatile partial sumsevery add is a load and a store
a mean square recomputed per elementO(d2)O(d^2) instead of O(d)O(d) per row
the work done twiceexactly 2x: the smallest regression the budget must catch
a linear search for a binary oneO(n)O(n) instead of O(log⁡n)O(\log n)
a defensive copyan O(n)O(n) malloc and memcpy around an O(log⁡n)O(\log n) search

“Tiling disabled” (the whole matrix as one tile) is the textbook regression, yet on a machine whose cache holds the matrices it costs only 1.4x at n=256n = 256. A perf fault is only a fault if it is slow on the machine that runs the gate; measure before you plant one.

Take a gate with β=0.25\beta = 0.25 on one metric and four heads against the same base:

Base t^\hat tHead t^\hat tΔ=th/tb−1\Delta = t_h / t_b - 1th>tb(1+β)t_h > t_b (1 + \beta)?Verdict
8.0 ms9.6 ms9.6/8.0−1=+0.209.6 / 8.0 - 1 = +0.209.6>10.09.6 > 10.0: nook (noise or a small cost)
8.0 ms16.0 ms+1.00+1.0016.0>10.016.0 > 10.0: yesregressed (twice the work)
8.0 ms10.0 ms+0.25+0.2510.0>10.010.0 > 10.0: nook: the bound is inclusive
8.0 ms10.01 ms+0.25125+0.25125yesregressed

Now see why the minimum matters. Five repetitions of one build give 8.0, 8.1, 12.7 (a background job), 8.0, 8.3 ms. The mean is 9.02 ms, 13% above the work; the minimum is 8.0 ms. Compare a head whose five runs give 8.2, 8.1, 8.4, 8.2, 8.3 ms. By means, 8.24/9.02−1=−9%8.24 / 9.02 - 1 = -9\%, so the noisy base made the head look faster; by minima, 8.1/8.0−1=+1.3%8.1 / 8.0 - 1 = +1.3\%.

The gate’s regressed must reproduce the four rows of the table exactly.

The kata, primers/craft.06/kernels.c, is float32, plain C11, with its spec in kernels.h:

void kata_matmul(const float *A, const float *B, float *C, int n); /* C = A @ B, n x n: loop order i, k, j, tiled over k and j */
float kata_sum(const float *x, int n); /* eight partial sums */
void kata_rmsnorm(const float *x, const float *w, float *y, int rows, int d, float eps);
int kata_count_below(const float *sorted, int n, float t); /* binary search: entries < t */

The benchmark, primers/craft.06/bench.c, includes "kernels.h" and "ol_bench.h", times every kata kernel with ol_bench_best, and ends with ol_bench_done(), so its last line is {"matmul_ns": ..., "sum_ns": ..., "rmsnorm_ns": ..., "count_below_ns": ...} (lower is better). It uses fixed inputs only.

The budget, primers/craft.06/budget.toml, gives the run count and one fraction per metric:

runs = 3 # alternating runs per side; each side keeps its best
[budget] # one fraction in (0, 1) per metric of bench.c
matmul_ns = 0.30
sum_ns = 0.30
rmsnorm_ns = 0.30
count_below_ns = 0.50

The gate, primers/craft.06/gate.py, uses the Python standard library only:

python3 gate.py --base BASE.c --head HEAD.c [--include DIR] [--runs N]

It builds bench.c (next to gate.py) against each kernels.c with cc -std=c11 -O2 and -I for the gate’s own directory (kernels.h) and each --include (ol_bench.h), runs base and head alternately runs times, keeps each side’s minimum per metric, and prints a table and a final JSON line. It defines regressed(base, head, max_regression) -> bool (head more than max_regression slower). It exits 0 when nothing regresses, 1 when something does, and 2 when it cannot decide (a build failure, a crash, a metric missing). Every build and benchmark starts in its own process group with a timeout, so a hung benchmark cannot hang CI.

A correct solution must handle six cases, each for a reason that matters downstream:

CaseKind of checkWhy it matters downstream
the four hand rows on regressedunitthe inclusive bound is the contract every later gate inherits
bench.c uses ol_bench.h, times all four kernels, no rand(); budget.toml has runs of at least 2 and a fraction in (0,1)(0, 1) per kernelunita benchmark with random inputs or a missing metric cannot gate anything
the kata agrees with the plain kernels on awkward sizes (not multiples of the tile)goldena fast kernel that is wrong is worthless; the tile edges are where it goes wrong
the kata as base and head: exit 0, twice in a rowconformancea gate that fails a change against itself gets switched off
a candidate kata as head against the reference as base: exit 0unitthe fast version really is fast on this machine
ten planted slowdowns as head: exit 1 on at least 9, always on loop order, narrow tiles, one accumulator, linear searchfaultthe gate catches what unit tests cannot

Seven facts force the design.

  1. A timing is work plus non-negative noise. So the best estimate of the work is the smallest observation, not the mean (8.0 ms versus 9.02 ms in the worked example). This forces min everywhere: in ol_bench_best across repetitions and in the gate across alternating runs.
  2. One call can be shorter than the timer’s resolution. So each repetition loops the call until at least min_s seconds pass and divides by the count. This forces a per-call metric (nanoseconds per call), which is also what makes base and head comparable regardless of how many calls each repetition made.
  3. The compiler is allowed to delete unused work. So every result is written to a volatile sink. Without it, every version is “fast” and every planted slowdown survives.
  4. Absolute numbers vary by machine; ratios on one machine within seconds do not. So the gate builds both versions now, alternates them, and compares th/tbt_h / t_b. That forces the gate to own the build (it must compile both kernels.c files against the same bench.c) rather than read a stored number.
  5. The budget must separate two distributions: identical builds (∣Δ∣≲0.1\lvert\Delta\rvert \lesssim 0.1) and the smallest real regression (Δ=1.0\Delta = 1.0, work done twice). Any β\beta between them works; 0.30.3 leaves room on both sides. The noisiest metric (a microsecond-scale search) gets 0.50.5, still far below 1.01.0.
  6. A gate that cannot decide must say so. A build failure or a crash is neither “ok” nor “regressed,” so it gets its own exit code (2). Otherwise a broken head would read as “not slower” and pass, or a flaky machine as a regression.
  7. A hung benchmark (an infinite loop in head) must not hang CI. So each subprocess runs in its own session with a timeout, and the whole process group is killed, which also gets any children.

Each fast kernel is fast for one reason, and the planted slowdowns each remove exactly that reason. The matmul comes first:

/* Tile sizes for kata_matmul: a BK x BJ block of B (64 x 256 floats, 64 KiB)
* is reused by every row i before the next block is loaded. */
enum { BK = 64, BJ = 256 };
/* c[0..BJ) += a * b[0..BJ): a fixed trip count and restrict (no alias check
* needed) are what let gcc vectorize at -O2, as clang already does. */
static void axpy_tile(float *restrict c, const float *restrict b, float a) {
for (int j = 0; j < BJ; j++) c[j] += a * b[j];
}
void kata_matmul(const float *A, const float *B, float *C, int n) {
for (int i = 0; i < n * n; i++) C[i] = 0.0f;
for (int k0 = 0; k0 < n; k0 += BK)
for (int j0 = 0; j0 < n; j0 += BJ)
for (int i = 0; i < n; i++)
for (int k = k0; k < min_i(k0 + BK, n); k++) {
float a = A[i * n + k]; /* loop order i, k, j: a is a scalar for the inner loop */
if (j0 + BJ <= n) { /* a whole tile: unit stride over j, vectorized */
axpy_tile(C + i * n + j0, B + k * n + j0, a);
continue;
}
for (int j = j0; j < n; j++) C[i * n + j] += a * B[k * n + j]; /* ragged edge */
}
}

The inner loop is an axpy over a row of BB and a row of CC: unit stride, no reduction, so no float reassociation is needed to vectorize it. That is why loop order i,k,ji, k, j beats i,j,ki, j, k, whose inner loop is a dot product down a column of BB. The fixed BJ trip count and restrict are what make gcc vectorize; shrink the tile to 2 columns and the vector loop disappears. The ragged-edge branch exists only for nn not a multiple of 256, which is exactly where the golden check with awkward sizes looks.

The sum is fast because its source breaks the dependency chain:

float kata_sum(const float *x, int n) {
float s[8] = {0}; /* eight independent chains: no add waits on the previous one */
int i = 0;
for (; i + 8 <= n; i += 8)
for (int l = 0; l < 8; l++) s[l] += x[i + l];
for (; i < n; i++) s[0] += x[i]; /* the tail, fewer than 8 elements */
return ((s[0] + s[1]) + (s[2] + s[3])) + ((s[4] + s[5]) + (s[6] + s[7])); /* a fixed tree: deterministic */
}

The compiler may not turn one accumulator into eight, because float addition is not associative; the source has to. The fixed combination tree makes the result the same on every run. kata_rmsnorm reuses the same eight-way sum for ∑x2\sum x^2, computes inv = 1.0f / sqrtf(ss / d + eps) once per row, and then does one multiply pass; the O(d2)O(d^2) slowdown is recomputing that mean square inside the output loop.

The search is fast because it is logarithmic and touches nothing else:

int kata_count_below(const float *sorted, int n, float t) {
int lo = 0, hi = n; /* invariant: sorted[0..lo) < t <= sorted[hi..n) */
while (lo < hi) {
int mid = lo + (hi - lo) / 2; /* no overflow of lo + hi */
if (sorted[mid] < t)
lo = mid + 1;
else
hi = mid;
}
return lo; /* lo == hi: the count of entries < t */
}

It is O(log⁡n)O(\log n) and touches no memory but sorted. A linear scan is 800x slower at n=218n = 2^{18}, and a defensive copy adds an O(n)O(n) malloc and memcpy per query. The full kata is kernels.c.

The benchmark is one program, built against whichever kernels.c the gate hands it:

/* ol_bench.h comes first: it defines _POSIX_C_SOURCE for clock_gettime, and
* glibc ignores that once any other system header is in. */
#include "ol_bench.h"
#include <stdlib.h>
#include "kernels.h"
#define NM 256 /* matmul: 256^3 = 16.8M multiply-adds */
#define NS (1 << 20) /* sum: 4 MiB of floats */
#define NC (1 << 18) /* count_below: 1 MiB sorted, 256 queries per call */
#define QC 256
// ...
static volatile float sink_f; /* results land here, so the work cannot be deleted */
static volatile int sink_i;
static void run_matmul(void *ctx) { (void)ctx; kata_matmul(A, B, C, NM); sink_f = C[NM * NM - 1]; }
static void run_count(void *ctx) {
(void)ctx;
int acc = 0; /* 256 queries per call: one search alone is too short to time */
for (int q = 0; q < QC; q++) acc += kata_count_below(SORTED, NC, (float)((q * 7919) % NC) + 0.5f);
sink_i = acc;
}
int main(void) {
// ... malloc the buffers; return 2 if any fails
for (int i = 0; i < NM * NM; i++) { /* fixed formulas, never rand() */
A[i] = (float)(i % 7) - 3.0f;
B[i] = (float)(i % 5) - 2.0f;
}
// ... X, W, SORTED likewise
ol_bench_metric("matmul_ns", 1e9 * ol_bench_best(run_matmul, NULL, 5, 0.02)); /* best of 5, each >= 20 ms */
// ... sum_ns, rmsnorm_ns, count_below_ns
return ol_bench_done(); /* last stdout line: one JSON object */
}

The include-order comment records a real portability bug: under -std=c11, a _POSIX_C_SOURCE defined after another system header leaves CLOCK_MONOTONIC undeclared on Linux. The full benchmark is bench.c.

In the gate, the decision is one line; everything else is making the measurement trustworthy.

def regressed(base: float, head: float, max_regression: float) -> bool:
"""True when head is more than max_regression slower than base."""
return head > base * (1.0 + max_regression) # strict >: Delta == budget is ok (worked example, row 3)

Running a subprocess safely is the second piece:

def run(cmd: list[str], timeout: float = 120) -> tuple[int, str]:
"""One subprocess in its own process group, killed whole on timeout."""
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, start_new_session=True) # new session = new process group
try:
out, _ = p.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
os.killpg(p.pid, signal.SIGKILL) # kill the group, not just the child
out, _ = p.communicate()
return 124, out + f"\n(timed out after {timeout:.0f} s)"
return p.returncode, out

build compiles bench.c plus the given kernels.c with cc -std=c11 -O2 -I<gate dir> -I<each include> (and -lm except on macOS) and raises RuntimeError on failure. metrics runs the executable and parses the last line that starts with {, raising if the program exits non-zero or prints no JSON. main ties them together:

def main(argv: list[str] | None = None) -> int:
# ... argparse: --base, --head, --include (repeatable), --runs
try:
runs, budget = load_budget(HERE / "budget.toml")
runs = a.runs or runs
best: dict[str, dict[str, float]] = {"base": {}, "head": {}}
with tempfile.TemporaryDirectory(prefix="gate-") as d: # never write next to the sources
exes = {side: Path(d) / f"bench-{side}" for side in best}
build(a.base.resolve(), a.include, exes["base"])
build(a.head.resolve(), a.include, exes["head"])
for _ in range(runs):
for side in ("base", "head"): # alternate: drift hits both sides alike
for k, v in metrics(exes[side]).items():
best[side][k] = min(v, best[side].get(k, float("inf"))) # noise only adds
missing = [k for k in budget if k not in best["base"] or k not in best["head"]]
if missing:
raise RuntimeError(f"the benchmark reports no {missing}") # a budgeted metric must exist
except (RuntimeError, ValueError, OSError) as e:
print(f"gate: {e}", file=sys.stderr)
return 2 # cannot decide: neither pass nor fail
change, bad = {}, []
for k, limit in budget.items(): # the budget drives the loop, not bench.c
b, h = best["base"][k], best["head"][k]
change[k] = round(h / b - 1.0, 4)
slow = regressed(b, h, limit)
bad += [k] if slow else []
# ... print one table row
print(json.dumps({"ok": not bad, "change": change})) # machine-readable last line
return 1 if bad else 0

Iterating over the budget rather than over what the benchmark printed means a metric nobody budgeted is ignored, and a budgeted metric the benchmark dropped is an error (exit 2), not a silent pass. The full gate is gate.py.

The gate is cheap. It does two builds and 2⋅runs2 \cdot \text{runs} benchmark executions; each execution is 4×54 \times 5 repetitions of at least 20 ms, so about 0.4 s plus the warm-ups. Three runs per side is a few seconds: cheap enough for every pull request.

The minimum beats the mean or median. The mean absorbs outliers, as the worked example showed. The median is robust but still biased upward by sustained noise; with one-sided noise the minimum is the estimator closest to the work. Its weakness is that it hides variance a user would feel (tail latency), which is why the serving benchmarks of Open-loop load generator report percentiles, not minima.

A paired comparison beats a stored baseline. A stored baseline needs a pinned machine and recalibration; the paired run needs neither, at the cost of building and running base on every check.

Times beat instruction counts for this job. Counting instructions (Cachegrind) is deterministic but blind to the cache and vectorization effects that most of these slowdowns are about.

A fixed budget replaces a significance test. benchstat tests whether the two samples differ, but with three runs per side a test has little power; a fixed budget between the noise floor and 2x is simpler and, for these gross regressions, just as reliable. The accepted tradeoff is that slowdowns smaller than 30% pass.

The first eight rows are the planted slowdowns; the last four are mistakes in the benchmark or gate:

PitfallSymptomHow the solution avoids it
1. the matmul loop order i,j,ki, j, k14x slower, same valuesloop order i,k,ji, k, j: the inner loop is a unit-stride axpy
2. tiles too narrow to vectorize24x slower, same valuesBJ = 256 with a fixed-trip axpy_tile and restrict
3. the work done twiceexactly 2x: a budget of 1.0 or more lets it throughbudgets of 0.30 and 0.50, far below 1.0 (step 5)
4. one accumulator4x to 7x slower sumseight partial sums in kata_sum and kata_rmsnorm
5. volatile where it is not neededevery add goes through memoryvolatile only on the benchmark’s sinks, never in the kernels
6. a quantity recomputed inside the loop that uses itO(d2)O(d^2)inv computed once per row before the output loop
7. a linear search800x slower at n=218n = 2^{18}binary search with the [0, lo) < t <= [hi, n) invariant
8. a defensive copyan O(n)O(n) copy per O(log⁡n)O(\log n) querykata_count_below reads sorted in place
a benchmark result nobody readsthe compiler deletes the work; every version is “fast”every result goes to sink_f or sink_i (volatile)
the mean of noisy runsone background job fails a correct changemin over repetitions and over alternating runs
a budget below the noisethe gate fails a kata against itself0.30, three times the observed noise
comparing with a number stored on another machinethe gate passes or fails by machine, not by changebase and head are built and run here, now, alternately

Warning: A gate that fails a change against itself will be switched off within a week. Always run the kata as both base and head, twice, before trusting the budget.

This section reused grading by planted faults from TDD, unit tests, and mutation testing, the values-first rule from Oracles, golden and differential tests, gradcheck as a test, and loop order and tiling from Cache-blocked, packed, batch-invariant matmul. The same gate carries forward. The kernels chapter’s backend tests guard speed as well as values; the Rust runner of Candle model runner has criterion benchmarks behind the same kind of gate; and A performance regression bisect in Systems Engineering plants a slowdown and finds it by bisecting with this gate.

Each piece has a production equivalent that adds more:

  • ol_bench_best corresponds to Google Benchmark, criterion.rs, and Go’s testing.B, which add automatic iteration counts and statistics such as confidence intervals and outlier classes (criterion.rs user guide, “Analysis Process”).
  • The base-versus-head gate.py corresponds to benchstat, Bencher, and CodSpeed, which add significance tests over many runs and history dashboards (golang.org/x/perf/cmd/benchstat).
  • Fixed-machine noise control has an alternative in instruction counting with perf stat or Cachegrind: deterministic counts instead of times, but blind to cache effects (CodSpeed’s instrumentation mode; the Valgrind Cachegrind manual).
  • Planted slowdowns correspond to performance mutation testing (Delgado-Pérez et al., 2021) and regression bisection: mutation operators designed to cost time rather than change values, and bisection to find which commit made it slow (git bisect run with the gate as the test).
  • Brendan Gregg, Systems Performance, 2nd ed., ch. 12, “Benchmarking.”
  • Georges, Buytaert, Eeckhout, “Statistically Rigorous Java Performance Evaluation” (OOPSLA 2007).
  • Chen and Revels, “Robust benchmarking in noisy environments” (2016).
  • Benchmark: a measurement of time per call with fixed inputs, a warm-up, repetitions, and a stated estimator.
  • Minimum estimator: the smallest repetition’s time, the best estimate of the work when noise only adds time.
  • Perf gate: a check that fails a change when head is slower than base by more than the budget.
  • Budget: the largest allowed fractional slowdown β\beta of a metric.
  • Paired comparison: building and running base and head alternately on the same machine, so machine speed cancels in the ratio.
  • Sink: a volatile variable that receives a benchmark’s result so the compiler cannot delete the work.
  • A benchmark is a measurement with a stated error: fixed inputs, a warm-up, repetitions, and the minimum of the repetitions, because noise only ever makes a run slower.
  • A perf gate compares base and head on the same machine, alternately, within seconds, so it needs no stored numbers and no calibration, and a slow laptop and a fast CI runner give the same verdict.
  • The budget is a fraction (head at most 30% slower than base) that must sit above the noise and well below a real regression.
  • The slowdowns that matter keep the answer right: loop order, tiles too small to vectorize, one accumulator, duplicated work, a linear search, a defensive copy. Only a timing test catches them.
  • A good gate fails at least 9 of 10 planted slowdowns, always the gross ones, and never fails a kata against itself.
  1. Why is the bound head > base * (1 + budget) strict, and what would change if it were >=?

    AnswerA change of exactly the budget is allowed by definition (row 3 of the worked example). With `>=`, a head measured at exactly the boundary would fail, which only matters at the boundary, but the contract says "at most 30% slower" and the code should mean that.
  2. Why does the gate alternate base, head, base, head instead of running all base runs first?

    AnswerMachine drift (thermal throttling, a background job starting) is correlated in time. Interleaving spreads it over both sides, so it cancels in the ratio; running base first would charge all of a late slowdown to head.
  3. What happens if a planted slowdown makes bench.c crash or never finish, and why is that the right outcome?

    AnswerA crash raises in `metrics`, a hang is killed by `run`'s timeout (exit 124, then a raise); both make `main` return 2. The gate cannot say "slower" for something it could not measure, and it must not say "ok". CI treats 2 as a failure that needs a human.
  4. Why does kata_sum need eight partial sums in the source when -O2 vectorizes integer sums on its own?

    AnswerFloat addition is not associative, so the compiler may not reorder a single accumulator's chain into vector lanes (without `-ffast-math`). Writing eight independent sums gives it permission, and a fixed combination tree keeps the result deterministic.
  5. Why would a budget of 1.0 miss the “work done twice” slowdown?

    AnswerDoubled work gives $\Delta = +1.0$, and the gate fails only when $\Delta > \beta$. With $\beta = 1.0$, $+1.0$ is not more than $1.0$, so it passes. The budget must be well below 1.0 to catch the smallest real regression.