Sample: Visual Studio Profiler
Using the Microsoft.VisualStudio.BenchmarkDotNetDiagnosers NuGet package you can capture performance profiles of your benchmarks that can be opened in Visual Studio.
Source code
using System;
using BenchmarkDotNet.Attributes;
using Microsoft.VSDiagnostics;
namespace BenchmarkDotNet.Samples
{
// Enables profiling with the CPU Usage tool
// See: https://learn.microsoft.com/visualstudio/profiling/profiling-with-benchmark-dotnet
[CPUUsageDiagnoser]
public class IntroVisualStudioProfiler
{
private readonly Random rand = new Random(42);
[Benchmark]
public void BurnCPU()
{
for (int i = 0; i < 100000; ++i)
{
rand.Next(1, 100);
}
}
}
}
Output
The output will contain a path to the collected diagsession and automatically open in Visual Studio when launched from it.
// * Diagnostic Output - VSDiagnosticsDiagnoser *
Collection result moved to 'C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession'.
Session : {d54ebddb-2d6d-404f-b1da-10acbc89635f}
Stopped
Exported diagsession file: C:\Work\BenchmarkDotNet\samples\BenchmarkDotNet.Samples\bin\Release\net8.0\BenchmarkDotNet.Artifacts\BenchmarkDotNet_IntroVisualStudioProfiler_20241205_192056.diagsession.
Opening diagsession in VisualStudio: 15296