$npx -y skills add wshaddix/dotnet-skills --skill dotnet-benchmarkdotnetWriting benchmarks. BenchmarkDotNet setup, memory diagnosers, baselines, result analysis.
| 1 | # dotnet-benchmarkdotnet |
| 2 | |
| 3 | Microbenchmarking guidance for .NET using BenchmarkDotNet v0.14+. Covers benchmark class setup, memory and disassembly diagnosers, exporters for CI artifact collection, baseline comparisons, and common pitfalls that invalidate measurements. |
| 4 | |
| 5 | **Version assumptions:** BenchmarkDotNet v0.14+ on .NET 8.0+ baseline. Examples use current stable APIs. |
| 6 | |
| 7 | **Out of scope:** Performance-oriented architecture patterns (Span\<T\>, ArrayPool\<T\>, sealed class devirtualization) are owned by this epic's companion skill -- see [skill:dotnet-performance-patterns]. C# syntax for modern patterns (records, primary constructors) -- see [skill:dotnet-csharp-modern-patterns]. Coding standards and style conventions -- see [skill:dotnet-csharp-coding-standards]. Native AOT compilation pipeline and performance characteristics -- see [skill:dotnet-native-aot]. Serialization format APIs and round-trip correctness -- see [skill:dotnet-serialization]. Profiling tools (dotnet-counters, dotnet-trace, dotnet-dump) are covered by [skill:dotnet-profiling]. CI benchmark regression detection is covered by [skill:dotnet-ci-benchmarking]. Architecture patterns (caching, resilience) -- see [skill:dotnet-architecture-patterns]. EF Core query optimization -- see [skill:dotnet-efcore-patterns]. |
| 8 | |
| 9 | Cross-references: [skill:dotnet-performance-patterns] for zero-allocation patterns measured by benchmarks, [skill:dotnet-csharp-modern-patterns] for Span/Memory syntax foundation, [skill:dotnet-csharp-coding-standards] for sealed class style conventions, [skill:dotnet-native-aot] for AOT performance characteristics and benchmark considerations, [skill:dotnet-serialization] for serialization format performance tradeoffs. |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## Package Setup |
| 14 | |
| 15 | ```xml |
| 16 | <!-- Benchmarks.csproj --> |
| 17 | <Project Sdk="Microsoft.NET.Sdk"> |
| 18 | <PropertyGroup> |
| 19 | <OutputType>Exe</OutputType> |
| 20 | <TargetFramework>net8.0</TargetFramework> |
| 21 | </PropertyGroup> |
| 22 | <ItemGroup> |
| 23 | <PackageReference Include="BenchmarkDotNet" Version="0.14.*" /> |
| 24 | </ItemGroup> |
| 25 | </Project> |
| 26 | ``` |
| 27 | |
| 28 | Keep benchmark projects separate from production code. Use a `benchmarks/` directory at the solution root. |
| 29 | |
| 30 | --- |
| 31 | |
| 32 | ## Benchmark Class Setup |
| 33 | |
| 34 | ### Basic Benchmark with [Benchmark] Attribute |
| 35 | |
| 36 | ```csharp |
| 37 | using BenchmarkDotNet.Attributes; |
| 38 | using BenchmarkDotNet.Running; |
| 39 | |
| 40 | [MemoryDiagnoser] |
| 41 | public class StringConcatBenchmarks |
| 42 | { |
| 43 | private readonly string[] _items = Enumerable.Range(0, 100) |
| 44 | .Select(i => i.ToString()) |
| 45 | .ToArray(); |
| 46 | |
| 47 | [Benchmark(Baseline = true)] |
| 48 | public string StringConcat() |
| 49 | { |
| 50 | var result = string.Empty; |
| 51 | foreach (var item in _items) |
| 52 | result += item; |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | [Benchmark] |
| 57 | public string StringBuilder() |
| 58 | { |
| 59 | var sb = new System.Text.StringBuilder(); |
| 60 | foreach (var item in _items) |
| 61 | sb.Append(item); |
| 62 | return sb.ToString(); |
| 63 | } |
| 64 | |
| 65 | [Benchmark] |
| 66 | public string StringJoin() => string.Join(string.Empty, _items); |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | ### Running Benchmarks |
| 71 | |
| 72 | ```csharp |
| 73 | // Program.cs |
| 74 | using BenchmarkDotNet.Running; |
| 75 | |
| 76 | BenchmarkRunner.Run<StringConcatBenchmarks>(); |
| 77 | ``` |
| 78 | |
| 79 | Run in Release mode (mandatory for valid results): |
| 80 | |
| 81 | ```bash |
| 82 | dotnet run -c Release |
| 83 | ``` |
| 84 | |
| 85 | ### Parameterized Benchmarks |
| 86 | |
| 87 | ```csharp |
| 88 | [MemoryDiagnoser] |
| 89 | public class CollectionBenchmarks |
| 90 | { |
| 91 | [Params(10, 100, 1000)] |
| 92 | public int Size { get; set; } |
| 93 | |
| 94 | private int[] _data = null!; |
| 95 | |
| 96 | [GlobalSetup] |
| 97 | public void Setup() |
| 98 | { |
| 99 | _data = Enumerable.Range(0, Size).ToArray(); |
| 100 | } |
| 101 | |
| 102 | [Benchmark(Baseline = true)] |
| 103 | public int ForLoop() |
| 104 | { |
| 105 | var sum = 0; |
| 106 | for (var i = 0; i < _data.Length; i++) |
| 107 | sum += _data[i]; |
| 108 | return sum; |
| 109 | } |
| 110 | |
| 111 | [Benchmark] |
| 112 | public int LinqSum() => _data.Sum(); |
| 113 | } |
| 114 | ``` |
| 115 | |
| 116 | --- |
| 117 | |
| 118 | ## Memory Diagnosers |
| 119 | |
| 120 | ### MemoryDiagnoser |
| 121 | |
| 122 | Tracks GC allocations and collection counts per benchmark invocation. Apply at class level to all benchmarks: |
| 123 | |
| 124 | ```csharp |
| 125 | [MemoryDiagnoser] |
| 126 | public class AllocationBenchmarks |
| 127 | { |
| 128 | [Benchmark] |
| 129 | public byte[] AllocateArray() => new byte[1024]; |
| 130 | |
| 131 | [Benchmark] |
| 132 | public int UseStackalloc() |
| 133 | { |
| 134 | Span<byte> buffer = stackalloc byte[1024]; |
| 135 | buffer[0] = 42; |
| 136 | return buffer[0]; |
| 137 | } |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | Output columns: |
| 142 | |
| 143 | | Column | Meaning | |
| 144 | |--------|---------| |
| 145 | | `Allocated` | Bytes allocated per operation | |
| 146 | | `Gen0` | Gen 0 GC collections per 1000 operations | |
| 147 | | `Gen1` | Gen 1 GC collections per 1000 operations | |
| 148 | | `Gen2` | Gen 2 GC collections per 1000 operations | |
| 149 | |
| 150 | Zero in `Allocated` column confirms zero-allocation code paths. |
| 151 | |
| 152 | ### DisassemblyDiagnoser |
| 153 | |
| 154 | Inspects JIT-compiled assembly to verify optimizations (devirtualization, inlining): |
| 155 | |
| 156 | ```csharp |
| 157 | [DisassemblyDiagnoser(maxDepth: 2)] |
| 158 | [MemoryDiagnoser] |
| 159 | public class DevirtualizationBenchmarks |
| 160 | { |
| 161 | // sealed enables JIT devirtualization -- verify in disassembly output |
| 162 | // See [s |