https://github.com/wickedflame/measuremap
Profiling and Benchmarking .NET Code made simple
https://github.com/wickedflame/measuremap
benchmark benchmark-testing benchmarking dotnet performance performance-testing profiler profiling
Last synced: 3 months ago
JSON representation
Profiling and Benchmarking .NET Code made simple
- Host: GitHub
- URL: https://github.com/wickedflame/measuremap
- Owner: WickedFlame
- License: mit
- Created: 2015-11-03T19:59:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2025-10-16T06:44:23.000Z (6 months ago)
- Last Synced: 2025-10-17T08:51:24.265Z (6 months ago)
- Topics: benchmark, benchmark-testing, benchmarking, dotnet, performance, performance-testing, profiler, profiling
- Language: C#
- Homepage:
- Size: 1010 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# MeasureMap
Profiling and Benchmarking .NET Code made simple
[](https://ci.appveyor.com/project/chriswalpen/measuremap/branch/master)
[](https://ci.appveyor.com/project/chriswalpen/measuremap/branch/dev)
[](https://www.nuget.org/packages/measuremap/)
[](https://www.nuget.org/packages/measuremap/)
MeasureMap allows profiling and benchmarking from simple code fragments to full applications.
Visit [https://wickedflame.github.io/MeasureMap/](https://wickedflame.github.io/MeasureMap/) for the full documentation.
MeasureMap uses the builder pattern and a fluent API to make profiling or benchmarking as simple as possible.
## Profiling
The Builder for profiling is initiated with the ProfilerSession when running the StartSession method.
```csharp
var result = ProfilerSession.StartSession()
.Task(() =>
{
// This represents the Task that needs testint
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.001));
})
.SetIterations(200)
.Assert(pr => pr.Iterations.Count() == 1)
.RunSession();
result.Trace();
Assert.IsTrue(result.AverageMilliseconds < 20);
```
RunSession executes the profiler for the registered Task.
## Benchmarking
Benchmarks are basically just multiple Profiler-Sessions that are run in one Session.
The Benchmarks are registered to and executed by the BenchmarkRunner.
Benchmarks can be run in two ways
- FluentAPI
- Attributes
### FluentAPI
```csharp
var sha256 = SHA256.Create();
var md5 = MD5.Create();
var data = new byte[10000];
new Random(42).NextBytes(data);
var runner = new BenchmarkRunner();
runner.SetIterations(10);
runner.Task("sha256", () => sha256.ComputeHash(data));
runner.Task("md5", () => md5.ComputeHash(data));
var result = runner.RunSessions();
result.Trace();
```
### Attributes
```csharp
[Iterations(10)]
[Threads(10)]
//[Duration(10)]
[RunWarmup(false)]
public class WorkflowBenchmark
{
private readonly SHA256 _sha256;
private readonly MD5 _md5;
private readonly byte[] _data;
public WorkflowBenchmark()
{
_sha256 = SHA256.Create();
_md5 = MD5.Create();
_data = new byte[10000];
new Random(42).NextBytes(_data);
}
[OnStartPipeline]
public void Setup()
{
}
[OnEndPipeline]
public void End()
{
}
[Benchmark]
public void sha256()
{
// Simulate some work
_sha256.ComputeHash(_data);
}
[Benchmark]
public void md5(IExecutionContext ctx)
{
// Simulate some work
_md5.ComputeHash(_data);
}
}
```
```csharp
[Test]
public void WorkflowTest_Benchmark()
{
var runner = new BenchmarkRunner();
var result = runner.RunSession();
result.Trace();
}
```
## Tracing the Result
The result is by default traced as Markdown
```
### MeasureMap Benchmark
Iterations: 10
#### Summary
| Name | Avg Time | Avg Ticks | Total | Fastest | Slowest | Memory Increase |
|------- |----------------: |---------: |----------------: |-------: |-------: |---------------: |
| sha256 | 00:00:00.0000924 | 924 | 00:00:00.0009243 | 776 | 1471 | 1392 |
| md5 | 00:00:00.0000485 | 485 | 00:00:00.0004858 | 409 | 534 | 1392 |
```