|
| 1 | +--- |
| 2 | +mode: agent |
| 3 | +description: 'Writing Performance Benchmarks' |
| 4 | +--- |
| 5 | + |
| 6 | +# Benchmark Fixture Prompt (Tuning Benchmarks) |
| 7 | + |
| 8 | +This prompt defines how to generate performance tests (βbenchmarksβ) for a project/solution using BenchmarkDotNet. |
| 9 | +Benchmarks are *not* unit tests β they are micro- or component-level performance measurements that belong under the `tuning/` directory and follow strict conventions. |
| 10 | + |
| 11 | +Copilot must follow these guidelines when generating benchmark fixtures. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 1. Naming and Placement |
| 16 | + |
| 17 | +- All benchmark projects live under the `tuning/` folder. |
| 18 | + Examples: |
| 19 | + - `tuning/<ProjectName>.Benchmarks/` |
| 20 | + - `tuning/<ProjectName>.Console.Benchmarks/` |
| 21 | + |
| 22 | +- **Namespaces must NOT end with `.Benchmarks`.** |
| 23 | + They must mirror the production assemblyβs namespace. |
| 24 | + |
| 25 | + Example: |
| 26 | + If benchmarking a type inside `YourProject.Console`, then: |
| 27 | + |
| 28 | + ```csharp |
| 29 | + namespace YourProject.Console |
| 30 | + { |
| 31 | + public class Sha512256Benchmark { β¦ } |
| 32 | + } |
| 33 | + ``` |
| 34 | + |
| 35 | +* **Benchmark class names must end with `Benchmark`.** |
| 36 | + Example: `DateSpanBenchmark`, `FowlerNollVoBenchmark`. |
| 37 | + |
| 38 | +* Benchmark files should be located in the matching benchmark project |
| 39 | + (e.g., benchmarks for `YourProject.Console` go in `YourProject.Console.Benchmarks.csproj`). |
| 40 | + |
| 41 | +* In the `.csproj` for each benchmark project, set the root namespace to the production namespace, for example: |
| 42 | + |
| 43 | + ```xml |
| 44 | + <RootNamespace>YourProject.Console</RootNamespace> |
| 45 | + ``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 2. Attributes and Configuration |
| 50 | + |
| 51 | +Each benchmark class should use: |
| 52 | + |
| 53 | +```csharp |
| 54 | +[MemoryDiagnoser] |
| 55 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 56 | +``` |
| 57 | + |
| 58 | +Optional but strongly recommended where meaningful: |
| 59 | + |
| 60 | +* `[Params(...)]` β define small, medium, large input sizes. |
| 61 | +* `[GlobalSetup]` β deterministic initialization of benchmark data. |
| 62 | +* `[Benchmark(Description = "...")]` β always add descriptions. |
| 63 | +* `[Benchmark(Baseline = true)]` β when comparing two implementations. |
| 64 | + |
| 65 | +Avoid complex global configs; prefer explicit attributes inside the class. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## 3. Structure and Best Practices |
| 70 | + |
| 71 | +A benchmark fixture must: |
| 72 | + |
| 73 | +* Measure a **single logical operation** per benchmark method. |
| 74 | +* Avoid I/O, networking, disk access, logging, or side effects. |
| 75 | +* Avoid expensive setup inside `[Benchmark]` methods. |
| 76 | +* Use deterministic data (e.g., seeded RNG or predefined constants). |
| 77 | +* Use `[GlobalSetup]` to allocate buffers, random payloads, or reusable test data only once. |
| 78 | +* Avoid shared mutable state unless reset per iteration. |
| 79 | + |
| 80 | +Use representative input sizes such as: |
| 81 | + |
| 82 | +```csharp |
| 83 | +[Params(8, 256, 4096)] |
| 84 | +public int Count { get; set; } |
| 85 | +``` |
| 86 | + |
| 87 | +BenchmarkDotNet will run each benchmark for each parameter value. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 4. Method Naming Conventions |
| 92 | + |
| 93 | +Use descriptive names that communicate intent: |
| 94 | + |
| 95 | +* `Parse_Short` |
| 96 | +* `Parse_Long` |
| 97 | +* `ComputeHash_Small` |
| 98 | +* `ComputeHash_Large` |
| 99 | +* `Serialize_Optimized` |
| 100 | +* `Serialize_Baseline` |
| 101 | + |
| 102 | +When comparing approaches, always list them clearly and tag one as the baseline. |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## 5. Example Benchmark Fixture |
| 107 | + |
| 108 | +```csharp |
| 109 | +using BenchmarkDotNet.Attributes; |
| 110 | +using BenchmarkDotNet.Configs; |
| 111 | + |
| 112 | +namespace YourProject |
| 113 | +{ |
| 114 | + [MemoryDiagnoser] |
| 115 | + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 116 | + public class SampleOperationBenchmark |
| 117 | + { |
| 118 | + [Params(8, 256, 4096)] |
| 119 | + public int Count { get; set; } |
| 120 | + |
| 121 | + private byte[] _payload; |
| 122 | + |
| 123 | + [GlobalSetup] |
| 124 | + public void Setup() |
| 125 | + { |
| 126 | + _payload = new byte[Count]; |
| 127 | + // deterministic initialization |
| 128 | + } |
| 129 | + |
| 130 | + [Benchmark(Baseline = true, Description = "Operation - baseline")] |
| 131 | + public int Operation_Baseline() => SampleOperation.Process(_payload); |
| 132 | + |
| 133 | + [Benchmark(Description = "Operation - optimized")] |
| 134 | + public int Operation_Optimized() => SampleOperation.ProcessOptimized(_payload); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## 6. Reporting and CI |
| 142 | + |
| 143 | +* Benchmark projects live exclusively under `tuning/`. They must not affect production builds. |
| 144 | +* Heavy BenchmarkDotNet runs should *not* run in CI unless explicitly configured. |
| 145 | +* Reports are produced by the benchmark runner and stored under the configured artifacts directory. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## 7. Additional Guidelines |
| 150 | + |
| 151 | +* Keep benchmark fixtures focused and readable. |
| 152 | +* Document non-obvious reasoning in short comments. |
| 153 | +* Prefer realistic but deterministic data sets. |
| 154 | +* When benchmarks reveal regressions or improvements, reference the associated PR or issue in a comment. |
| 155 | +* Shared benchmark helpers belong in `tuning/` projects, not in production code. |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## Final Notes |
| 160 | + |
| 161 | +* Benchmarks are performance tests, not unit tests. |
| 162 | +* Use `[Benchmark]` only for pure performance measurement. |
| 163 | +* Avoid `MethodImplOptions.NoInlining` unless absolutely necessary. |
| 164 | +* Use small sets of meaningful benchmark scenarios β avoid combinatorial explosion. |
0 commit comments