Section 14.36

Benchmark harness

Definition

A benchmark harness is the code and procedure that surround the operation a benchmark measures. It prepares the system’s state, generates the inputs, starts and stops the timer or counters, checks whether each run produced a valid result, and records and summarizes the measurements. The workload is the work measured; the harness makes and judges the measurement. Because the harness runs on or beside the measured system, it can add its own cost to the result, change the state being measured, and accept runs that should have been rejected.

The idea

A developer wants the time of one function call that takes about 0.2 microseconds. The first harness reads a timer, calls the function, reads the timer again and prints the difference. Three things go wrong at once. On this system, reading the timer twice costs about 1 microsecond, five times the operation. Printing takes far longer than the call and can push the function’s data out of the processor’s cache, so the next call starts with a cold cache. And the compiler sees that the result is never used and removes the call, a compiler optimization §3.39 called dead-code elimination §12.11, so the harness times nothing at all. A second harness, for a messaging service, counts a run as a success whenever the program ends with exit status §13.24 0, the value that by convention means success, including runs in which the message never arrived.

A harness is a measuring instrument, and an instrument must be checked before its readings are trusted. A kitchen scale is a fair picture. It must be zeroed with the empty bowl on it, so that the bowl’s weight is not reported as flour, and a reading taken while the bag slid off must be thrown away. The correctness argument for a benchmark therefore covers the harness as well as the formula that summarizes the data. The picture stops at one point: a scale does not change the flour, whereas a harness shares the processor, the memory and the caches with the code it measures and can change how that code behaves.

How it works

A harness performs these steps for each run, and each step has its own way to fail.

  1. Fix the estimand §11.2, the exact quantity the benchmark is meant to estimate, including its start event and its end event: “time from calling send until the receiver has the message” differs from “time until send returns”.

  2. Prepare the state that the estimand names: a cold start, or a warm system after a warm-up §11.27 period; an open or a closed connection; a full or an empty buffer. Each run starts from the declared state, and the harness carries no state from one run into the next unless that is declared.

  3. Generate the inputs from a declared distribution, outside the timed region, and record the random seed, the starting value from which the pseudorandom inputs can be generated again.

  4. Time the operation. The timed region is the code between the start and stop readings; it contains the operation and as little else as possible. Use a monotonic clock §9.35, a clock that never jumps backwards. If the operation is shorter than the timer’s resolution or than the cost of reading the timer, time a batch of many repetitions and divide. Make the result observable, for example by adding it to a checksum §4.32 that is printed after the timed region, so that the compiler cannot remove the work.

  5. Capture every outcome: success, error, timeout §9.34 or crash, with a time for each attempt, failed ones included.

  6. Validate each run against acceptance checks written before the results are seen: the output is correct according to a test oracle §10.5, an independent way of deciding the right output; no records were lost; the environment was as declared.

  7. Aggregate with the right denominator, so that every attempt appears in the counts.

Two calibration §11.42 runs test the harness itself. A null benchmark runs the harness with an empty operation and so measures the harness’s own cost. A positive control §10.30 runs an operation whose cost is known to differ, for example one with an added delay of known length, and confirms that the harness reports the difference.

Worked example

Cost domination. The function takes 0.2 microseconds and a pair of timer readings takes 1.0 microsecond. Timing one call reports 0.2+1.0=1.20.2 + 1.0 = 1.2 microseconds, of which 1.0/1.2≈0.831.0 / 1.2 \approx 0.83, or 83%, is harness. Timing a batch of 10,000 calls instead gives 10,000×0.2=2,00010{,}000 \times 0.2 = 2{,}000 microseconds of work plus 1.0 microsecond of timer cost, 2,001 microseconds in total, and 2,001/10,000=0.20012{,}001 / 10{,}000 = 0.2001 microseconds per call. The timer’s share is now 1.0/2,001≈0.00051.0 / 2{,}001 \approx 0.0005, or 0.05%. The loop that repeats the call adds a small cost of its own to every repetition; a null benchmark with an empty loop measures that cost.

Wrong endpoint. A request starts at time 0, the send call returns at 2 ms, and the receiver has the message at 100 ms. A harness that stops the clock when send returns reports 2 ms. The quantity a user experiences is 100 ms, and 100−2=98100 - 2 = 98 ms of it was never observed. The 2 ms is a correct measurement of a different estimand.

Wrong denominator. Ten requests are attempted. Eight succeed, with a mean time of 40 ms, and two hit a timeout set at 500 ms. A harness that drops the timed-out runs reports “8 of 8 succeeded, mean 40 ms”. The correct report is “8 of 10 succeeded, a success rate of 8/10=0.88 / 10 = 0.8; the other 2 took longer than 500 ms”. This is right censoring §11.28: for a timed-out run only a lower bound on its time, 500 ms, is known. The mean over all ten attempts is therefore at least (8×40+2×500)/10=(320+1000)/10=132(8 \times 40 + 2 \times 500) / 10 = (320 + 1000) / 10 = 132 ms, which is 132/40=3.3132 / 40 = 3.3 times the reported mean.

Stated precisely

Let topt_{\mathrm{op}} be the true cost of one operation and tht_{\mathrm{h}} the harness’s cost inside the timed region per operation. The harness reports top+tht_{\mathrm{op}} + t_{\mathrm{h}}, so reporting that as the operation’s cost overstates it by the fraction th/topt_{\mathrm{h}} / t_{\mathrm{op}}. When one pair of timer readings, costing trt_{\mathrm{r}}, times a batch of kk operations, the timer’s cost per operation falls to tr/kt_{\mathrm{r}} / k. A cost that the harness adds to every repetition, such as loop control, does not fall with kk and must be measured with a null benchmark.

A benchmark result is valid for its estimand only if five conditions hold.

  1. Endpoints: the start and stop readings are taken at the events that the estimand names.

  2. Overhead: th/topt_{\mathrm{h}} / t_{\mathrm{op}} is small compared with the differences the benchmark is meant to detect, or tht_{\mathrm{h}} is measured with a null benchmark and subtracted, with its own variation reported.

  3. State: every run starts in the declared state, and nothing the harness does between runs changes that state unless declared.

  4. Accounting: every attempt is recorded with its outcome, so that Natt=Nok+Nfail+NexclN_{\mathrm{att}} = N_{\mathrm{ok}} + N_{\mathrm{fail}} + N_{\mathrm{excl}}, where NattN_{\mathrm{att}} counts attempts, NokN_{\mathrm{ok}} valid runs, NfailN_{\mathrm{fail}} failed or timed-out runs, and NexclN_{\mathrm{excl}} runs excluded for a recorded reason.

  5. Acceptance: the rule that decides whether a run is valid is fixed before the results are seen and is checked by code that can reject a run.

Limits and common mistakes

An exit status of 0 shows only that the program ended normally. It does not show that the work was done or that the result was right; a benchmark program can print an error and still exit with 0.

Repeating a harness reproduces its bias. Good repeatability §11.43, the same result on every rerun, says nothing about whether the harness measures the right thing.

Subtracting a null benchmark assumes that the harness’s cost adds to the operation’s cost unchanged. Caches and the processor’s instruction pipeline, which overlaps the execution of neighboring instructions, can make the two interact, so for very short operations the subtraction is only approximate.

A tight loop that repeats one operation warms the caches and trains the processor’s branch predictor, the circuit that guesses which way a branch will go. The result describes the warm case. An operation that runs rarely in real use meets cold caches and runs slower.

A load generator that waits for each response before it sends the next request sends fewer requests exactly when the system is slow. The slow periods then contribute few samples, and the measured latency distribution §11.36 looks better than the one users experience at the intended request rate. This error is called coordinated omission. To measure latency at a target rate, send requests on a fixed schedule and measure each one from its scheduled send time.

A harness that runs on a separate host and takes its timestamps there includes the host’s scheduling delays and the delay of the connection to the device in every measurement.

Recording where the harness code came from, and which version ran, makes an audit possible. It does not show that the harness is correct.

Questions answered

Can the harness dominate cost, alter state, or accept an invalid run?

Yes: a benchmark harness, the code around a measured operation that prepares, times, checks and records each run, can do all three, and each needs its own check.

Finally, test the harness itself: a null benchmark shows its cost, and a positive control, an operation with a known extra cost, shows that it can detect a real difference.

Related entries: Estimand and estimator (§11.2), Missing data and attrition (§11.29), Instrumentation overhead (§11.32), Finite-window rate (§11.41), Censoring (§11.28), Survivorship bias (§11.30), Positive control (§10.30).