Expose the metric trap in an averaged benchmark
Average two unequal workloads into one latency and confidence interval, then identify what the headline hides.
A benchmark headline can be mathematically correct and still mislead. An average over two workloads answers “what is the mean of these trials?” It does not answer “how fast is a short request?”, “how fast is a long request?”, or “what happens at this arrival rate?”. If the workloads have different sizes and the denominator is not stated, the mean is not a service-level result.
This example builds six trials from two workloads. The “short prompt” has 12
characters; the longer prompt has 32. Latency is 0.1 + 0.01 * length, so the
short trials are 0.22 and the long ones 0.42. The mean is 0.32, and the 95%
interval is about [0.232, 0.408]. The interval spans both workloads because the
trials are pooled. The tokens field is the string length, not a model token
count, and the three repeats are deterministic copies, not independent samples.
Run it
From the canonical repository at commit
f958967d70992d7b09d9c75000bd8c543f1644d4:
python examples/ch40/benchmark.py --jsonPython 3.11 or later, standard library only. No model, network, GPU or
credentials. It finishes immediately. The chapter’s bench_lab.py is the
measured suite and is not the default path.
Implementation
run_trials repeats each workload and assigns latency from its length.
confidence_interval uses the pooled sample standard deviation and a normal
1.96 multiplier. build_report returns the trials, the pooled mean and the
interval.
def run_trials(workload: List[str], repeat: int = 3) -> List[Trial]:
trials = []
for item in workload:
for _ in range(repeat):
trials.append(Trial(workload=item, latency=0.1 + 0.01 * len(item), tokens=len(item)))
return trials
def confidence_interval(values: List[float]) -> tuple[float, float]:
if not values:
return (0.0, 0.0)
mean = statistics.mean(values)
if len(values) == 1:
return (mean, mean)
stdev = statistics.stdev(values)
margin = 1.96 * stdev / (len(values) ** 0.5)
return (mean - margin, mean + margin)
def build_report(workload: List[str], repeat: int = 3) -> BenchmarkReport:
trials = run_trials(workload, repeat)
latencies = [t.latency for t in trials]
ci = confidence_interval(latencies)
return BenchmarkReport(
trials=trials,
mean_latency=statistics.mean(latencies) if latencies else 0.0,
ci_low=ci[0],
ci_high=ci[1],
metadata={"repeat": repeat, "workload_size": len(workload)},
)Expected outcome
Observed with the command above (exit 0):
{
"trials": [
{
"workload": "short prompt",
"latency": 0.22,
"tokens": 12
},
{
"workload": "short prompt",
"latency": 0.22,
"tokens": 12
},
{
"workload": "short prompt",
"latency": 0.22,
"tokens": 12
},
{
"workload": "a longer prompt with more tokens",
"latency": 0.42000000000000004,
"tokens": 32
},
{
"workload": "a longer prompt with more tokens",
"latency": 0.42000000000000004,
"tokens": 32
},
{
"workload": "a longer prompt with more tokens",
"latency": 0.42000000000000004,
"tokens": 32
}
],
"mean_latency": 0.32,
"ci_low": 0.23234613528200823,
"ci_high": 0.4076538647179918,
"metadata": {
"repeat": 3,
"workload_size": 2
}
}The pooled mean is exactly halfway between the two workloads because each has
three trials. The interval is wide because the pooled sample includes both
levels. Neither workload’s latency is 0.32; the number describes the mixture, not
a request class. The tokens values are characters, not tokeniser tokens.
Mechanism and limitations
Documented: run_trials assigns latency deterministically from the workload
string length and records the length as tokens. confidence_interval returns
zero for an empty list and uses the pooled standard deviation for more than one
value. build_report records repeat and workload count. The tests check trial
counts, the interval, report metadata, the demo, JSON serialisation and the empty
interval.
Observed: the command exited 0 with the six trials, mean 0.32 and interval
above. The targeted tests.test_ch40_benchmark is part of the canonical suite;
no independent test count is claimed here.
Not shown: a real model, tokeniser, arrival process, concurrency, warm-up, A/A comparison, quality gate, or statistical independence. The repeats are identical copies, so the confidence interval is not a sampling interval from independent runs. The example is a deliberately weak benchmark used to expose the traps, not a recommended report format.
Challenge this result
Change the second workload to a different sentence of exactly 12 characters. Predict the pooled mean and interval; the code’s latency depends only on length, so both should match the first workload. Then change the latency model to depend on a separate semantic difficulty and explain why the same token count can still have different latency. Finally, rewrite the report to keep each workload separate and add a denominator for tokens per second.
Source and verification
Canonical source:
examples/ch40/benchmark.py
at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests:
tests/test_ch40_benchmark.py
.
Chapter: Designing Meaningful Inference Benchmarks
.
The run above is a fresh local execution on Windows 11 with Python 3.13.15; it
uses a synthetic trace, not a model or a serving benchmark.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.