Benchmark design
Benchmark design
A useful benchmark compares equivalent work under controlled conditions, not just two stopwatch readings.
Start with a hypothesis and a correctness contract. Here the workload compares two integer parsing overloads using the same supplied sample inputs. The shown empty, signed, malformed and overflowing cases are rejected by both implementations. NumberStyles.None does not by itself guarantee a strict ASCII lexical grammar: the documented parser ignores terminating NUL characters. For an exact digits-only contract, validate each character separately before parsing and add that check to both paths before benchmarking. Compare identical inputs and consume results. Release builds and a fixed SDK/runtime, OS, CPU architecture, power settings and workload size make results interpretable. JIT warmup, tiered compilation, garbage collection, CPU frequency, scheduling and instrumentation can change measurements. Separate cold-start from steady-state questions. BenchmarkDotNet automates much of the warmup and measurement process; its results still need interpretation. Use the companion ShortRun lab only with an approved finite outer timeout and reviewed dependencies. Record the actual sample count, mean, error, standard deviation, allocations and environment. Short runs are exploratory, not proof of a small improvement. Do not equate microbenchmark throughput with production p99 latency. The examples below validate work and analyze synthetic samples; they do not measure performance.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Synthetic calculations are not performance measurements.
Establish equivalent parsing behavior
Check both paths, including empty input and overflow, before benchmarking.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;using System.Globalization;foreach (string text in new[] {"42", "0007", "", "-1", "12x", "2147483648"}) { var a = int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out int x); var b = int.TryParse(text.AsSpan(), NumberStyles.None, CultureInfo.InvariantCulture, out int y); if (a != b || (a && x != y)) throw new Exception("mismatch"); Console.WriteLine(a ? x.ToString(CultureInfo.InvariantCulture) : "rejected");}Expected result (not executed):
427rejectedrejectedrejectedrejectedBoth overloads have the same acceptance rule here. A span view of an existing string does not allocate a substring; this alone does not establish a performance advantage.
Compare normalized work
Use fictional elapsed samples, each covering 1,000 operations. Report median and spread, not a measured result.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;using System.Globalization;long[] elapsedNs = { 120000, 110000, 150000, 115000, 125000 };Array.Sort(elapsedNs);Console.WriteLine("modeled median ns/op=" + (elapsedNs[2] / 1000));Console.WriteLine("modeled range ns/op=" + elapsedNs[0]/1000 + ".." + elapsedNs[^1]/1000);Expected result (not executed):
modeled median ns/op=120modeled range ns/op=110..150Dividing batch duration by operations gives batch-average cost per operation. The range of batch averages is not the distribution of individual requests.
Calculate a tail percentile separately
Nearest-rank p95 for a synthetic ten-request latency sequence. Small samples are not a reliable estimate of a production tail.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;int[] ms = { 5, 6, 8, 8, 10, 11, 12, 14, 20, 90 };Array.Sort(ms);int rank = (int)Math.Ceiling(0.95 * ms.Length);Console.WriteLine($"modeled p95={ms[rank-1]} ms");Console.WriteLine($"max={ms[^1]} ms");Expected result (not executed):
modeled p95=90 msmax=90 msWith ten samples the nearest-rank p95 is the maximum. Use a much larger representative sample for a meaningful tail estimate.
Apply the concept
Extend the parsing equivalence matrix with maximum int, leading whitespace and a Unicode digit. Design a benchmark with two workload sizes, a cold-start experiment and a steady-state experiment.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Benchmark design — knowledge check
Apply the lesson concepts to the following scenarios.
Why check both implementations first?
What does warmup address?
What is a limitation of a short benchmark run?
What does batch ns/op represent?
Which environment should be recorded?