using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running;
// Entry point, tell application to find the benchmark class - Tests - in the current assembly and run the defined benchmarks. BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);
// Instruct BenchmarkDotNet to generate the assembly language output(disassembly) for the benchmarked method. This is highly useful for low-level performance analysis to see exactly what instructions the JIT compiler produces. [DisassemblyDiagnoser]
// This attribute enables memory usage tracking, reporting metrics like allocated memory per operation. // "displayGenColumns: false" simplifies the output by not showing columns for specific garbage collector generations. [MemoryDiagnoser(displayGenColumns: false)]
// Format output, hid several default columns from the results table to make output cleaner and focus on the most relevant metrics(like Mean, Allocated, etc.). [HideColumns("Job", "Error", "StdDev", "Median", "RatioSD", "y")]
publicpartialclassTests// Class that contains method to be benchmarked. { [Benchmark] // Marks method as the specific code block that BenchmarkDotNet should run and measure repeatedly. [Arguments(42)] // Benchmark specific argument publicintSum(int y) { Func<int, int> addY = x => x + y; return DoubleResult(addY, y); }
privateintDoubleResult(Func<int, int> func, int arg) { int result = func(arg); return result + result; } }
Phases of benchmark
OverheadJitting
WorkloadJitting
WorkloadPilot
OverheadActual (IdleTarget) includes OverheadWarmup and OverheadWorkload
WorkloadWarmup (MainWarmup)
WorkloadActual (MainTarget)
WorkloadResult = WorkloadActual - OverheadActual
pilot: The best operation count will be chosen.
OverheadWarmup, OverheadWorkload: BenchmarkDotNet overhead will be evaluated.