Sample: IntroDotMemoryDiagnoser
If you want to get a memory allocation profile of your benchmarks, just add the [DotMemoryDiagnoser]
attribute, as shown below.
As a result, BenchmarkDotNet performs bonus benchmark runs using attached
dotMemory Command-Line Profiler.
The obtained dotMemory workspaces are saved to the artifacts
folder.
These dotMemory workspaces can be opened using the standalone dotMemory,
or dotMemory in Rider.
Source code
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnostics.dotMemory;
using System.Collections.Generic;
namespace BenchmarkDotNet.Samples
{
// Profile benchmarks via dotMemory SelfApi profiling for all jobs
[DotMemoryDiagnoser]
[SimpleJob] // external-process execution
[InProcess] // in-process execution
public class IntroDotMemoryDiagnoser
{
[Params(1024)]
public int Size;
private byte[] dataArray;
private IEnumerable<byte> dataEnumerable;
[GlobalSetup]
public void Setup()
{
dataArray = new byte[Size];
dataEnumerable = dataArray;
}
[Benchmark]
public int IterateArray()
{
var count = 0;
foreach (var _ in dataArray)
count++;
return count;
}
[Benchmark]
public int IterateEnumerable()
{
var count = 0;
foreach (var _ in dataEnumerable)
count++;
return count;
}
}
}
Links
- The permanent link to this sample: BenchmarkDotNet.Samples.IntroDotMemoryDiagnoser