Profiling and Benchmarking .NET Code made simple
| What | Badge |
|---|---|
| Build | |
| Nuget |
MeasureMap allows profiling and benchmarking from simple code fragments to full applications.
Visit 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.
The Builder for profiling is initiated with the ProfilerSession when running the StartSession method.
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.
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
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();Each benchmark creates an instance of the class that is benchmarked and executes the methods marked with the [Benchmark] attribute.
Each method marked with the [Benchmark] attribute is executed as a separate benchmark.
OnStartPipeline can return a IExecutionContext that is then injected into the benchmark methods.
The returned IExecutionContext and the Parameter ProfilerSettings are both optional.
[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()
{
}
[OnStartPipeline]
public IExecutionContext Setup(ProfilerSettings settings)
{
settings.CreateContext();
}
[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);
}
}[Test]
public void WorkflowTest_Benchmark()
{
var runner = new BenchmarkRunner();
var result = runner.RunSession<WorkflowBenchmark>();
result.Trace();
}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 |
The Workflow of MeasureMap is setup in three stacks or pipelines. The stack is made up of an execution chain following the Chain of Responsibility pattern. Each item in the pipeline is connected to the next in the chain.
The items in each stack are processed one after another. Handlers have logic that can be executed before or after the next handler in the chain is processed.
The SessionStack is run once per Session
- ElapsedTimeSessionHandler
- PreExecutionSessionHandler
- WarmupSessionHandler
- BasicSessionHandler / MainThreadSessionHandler / MultyThreadSessionHandler
The ContextStack is run once per Thread. The DefaultContextStackBuilder creates a new instance of the stack for each Thread.
- OnStartPipelineContextHandler
- OnEndPipelineContextHandler
- ProcessDataContextHandler
- WorkerContextHandler
The IterationStack is run once for every Iteration.
- ProcessDataTaskHandler (ITaskMiddleware)
- MemoryCollectionTaskHandler (ITaskMiddleware)
- ElapsedTimeTaskHandler (ITaskMiddleware)
- _task (ITask)