📊 BenchmarkDotNet = Performance Testing
Performance is critical. BenchmarkDotNet provides accurate, reliable performance benchmarking for .NET applications.
📝 BenchmarkDotNet Usage
[MemoryDiagnoser]
public class StringConcatBenchmark
{
[Benchmark]
public string ConcatWithPlus()
{
string result = "";
for (int i = 0; i < 100; i++)
{
result += i.ToString();
}
return result;
}
[Benchmark]
public string ConcatWithStringBuilder()
{
var sb = new StringBuilder();
for (int i = 0; i < 100; i++)
{
sb.Append(i);
}
return sb.ToString();
}
}
// Run benchmarks
var summary = BenchmarkRunner.Run<StringConcatBenchmark>();
// Attributes
[Benchmark(Baseline = true)]
[Benchmark(Description = "Description")]
[Benchmark(OperationsPerInvoke = 1000)]
// Configuration
public class Config : ManualConfig
{
public Config()
{
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core80)
.WithPlatform(Platform.X64));
AddLogger(ConsoleLogger.Default);
}
}
// Memory Diagnostics
[MemoryDiagnoser]
public class MemoryBenchmark
{
[Benchmark]
public int[] CreateArray()
{
return new int[1000];
}
}
✅ BenchmarkDotNet Benefits
- ✓ Accurate performance measurements
- ✓ Memory allocation tracking
- ✓ Multiple runtime support
- ✓ Statistical analysis
- ✓ Regression detection
BenchmarkDotNet is the gold standard for .NET performance testing. It gives you reliable data to make optimization decisions.
