4. INTRODUCTION
A powerful .NET library for benchmarking
Standard benchmarking routine: generating an isolated
project per each benchmark method; auto-selection of
iteration amount; warmup; overhead evaluation; statistics
calculation; and so on.
Supported runtimes: Full .NET Framework, .NET Core (RTM),
Mono
Supported languages: C#, F#, and Visual Basic
Supported OS: Windows, Linux, MacOS
Easy way to compare different environments
(x86 vs x64, LegacyJit vs RyuJit, and so on; see: Jobs)
Reports: markdown, csv, html, plain text, png plots.
Advanced features: Baseline, Params
Powerful diagnostics based on ETW events
(see BenchmarkDotNet.Diagnostics.Windows) 4
11. WRITE CODE TO BENCHMARK
using BenchmarkDotNet.Attributes;
…
public class ProgramBenchmarker {
protected Program m_Program { get; set; } = new
Program();
[Benchmark] public void Test() {
m_Program.Test();
}
}
…
11
12. RUN THE BENCHMARK
using BenchmarkDotNet.Running;
…
public class Program {
static void Main(string[] args) {
var summary =
BenchmarkRunner.Run<ProgramBenchmarker>();
}
public void Test() {… }
}
…
}
12
25. COLUMNS
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
…
public class HashCodeColumn : IColumn {
public string ColumnName { get; } = "HashCode";
public HashCodeColumn() { }
public bool IsDefault(Summary summary, Benchmark benchmark) =>
false;
public string GetValue(Summary summary, Benchmark benchmark) =>
benchmark.Target.Method.Name.GetHashCode().ToString();
public bool IsAvailable(Summary summary) => true;
public bool AlwaysShow => true;
public ColumnCategory Category => ColumnCategory.Custom;
public string Id { get; } = "1";
public int PriorityInCategory { get; } = 0;
public override string ToString() => ColumnName;
}
25
33. EXPORTERS
Allows you to export results of your benchmark in
different formats.By default, files with results will be
located
in .BenchmarkDotNet.Artifactsresults directory.
Available Exporters
HtmlExporter
CsvExporter
MarkdownExporter
AsciiDocExporter
CsvMeasurementsExporter
PlainExporter
JsonExporter
33
35. CONFIG
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Exporters.Csv;
using BenchmarkDotNet.Exporters.Json;
…
[Config(typeof(Config))]
public class ProgramBenchmarker {
private class Config : ManualConfig {
public Config() {
Add(AsciiDocExporter.Default);
Add(CsvMeasurementsExporter.Default);
Add(PlainExporter.Default);
Add(JsonExporter.Default);
}
}
…
}
35
44. PARAMS
using BenchmarkDotNet.Attributes;
...
public class ProgramBenchmarker {
[Params(100, 200)]
public int Parameter { get; set; }
protected Program m_Program { get; set; } = new
Program();
[Benchmark] public void Test() {
m_Program.Test();
}
}
44
47. SETUP
using BenchmarkDotNet.Attributes;
...
public class ProgramBenchmarker {
protected Program m_Program { get; set; }
[Setup]
public void Setup() {
m_Program = new Program();
}
[Benchmark]
public void Test() {
m_Program.Test();
}
} 47