Edit

Share via


Analyze BenchmarkDotNet data in Visual Studio

You can use the profiling tools to collect and view BenchmarkDotNet data in Visual Studio.

When you add a BenchmarkDotNet diagnoser to your benchmark classes as an attribute, a .diagsession file is generated after the benchmark runs. You can then open the .diagsession in Visual Studio and view profiling data for the benchmarks.

The following diagnosers are supported:

  • CPUUsageDiagnoser
  • DatabaseDiagnoser
  • DotNetCountersDiagnoser
  • EventsDiagnoser
  • FileIODiagnoser

Each diagnoser generates performance data related to that diagnoser. For example, the CPUUsageDiagnoser generates a .diagsession file with CPU data in it, and the DatabaseDiagnoser generates a .diagsession file with data on database operations. Limitations correspond to the associated profiling tool. For example, the profiler's Database tool works on ADO.NET or Entity Framework Core.

Prerequisites

Collect Benchmark.NET data

  1. Create a console project.

    The benchmark functions must be added to a .NET console application. These functions can be wrapper functions that reference other project types.

  2. Set your build to a Release build instead of a Debug build.

  3. Attribute your code for diagnosers and benchmarks, and include code to run the benchmarks (BenchmarkRunner.Run).

    Add the diagnoser name as an attribute to the class that contains the benchmarks for which you want to generate data.

    For example, you can use the following code for the CPUUsageDiagnoser.

    using System;
    using System.Security.Cryptography;
    using BenchmarkDotNet.Attributes;
    using BenchmarkDotNet.Running;
    using Microsoft.VSDiagnostics;
    
    namespace MyBenchmarks
    {
        [CPUUsageDiagnoser]
        public class Md5VsSha256
        {
            private const int N = 10000;
            private readonly byte[] data;
    
            private readonly SHA256 sha256 = SHA256.Create();
            private readonly MD5 md5 = MD5.Create();
    
            public Md5VsSha256()
            {
                data = new byte[N];
                new Random(42).NextBytes(data);
            }
    
            [Benchmark]
            public byte[] Sha256() => sha256.ComputeHash(data);
    
            [Benchmark]
            public byte[] Md5() => md5.ComputeHash(data);
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
            }
        }
    }
    
  4. Run the application to generate the .diagsession file.

    Check the console output to get the ___location of the file. For example:

    // * Diagnostic Output - VSDiagnosticsDiagnoser * 
    Collection result moved to 'BenchmarkDotNet_Md5VsSha256_20231218_123326.diagsession'.
    Session : {7f38bcc2-c692-4266-aa24-b12bc5325ea4}
      Stopped
    Exported diagsession file: *.diagsession
    

View BenchmarkDotNet data

  1. In Visual Studio, select File > Open > File and navigate to the ___location of the .diagsession file, and then select and open the file.

  2. Select the Benchmarks tab to view data for the BenchmarkDotNet benchmarks.

    Screenshot of BenchmarkDotNet data in Visual Studio.

    For more information about the results in the Benchmarks tab, see BenchmarkDotNet documentation.

  3. Right-click a row in the results and choose Select time range to sync the timeline graph with the benchmark.

  4. Select one of the available tabs such as CPU Usage or Allocations.

    Depending on the diagnoser you used to collect data, you can gain insights related to memory allocation, CPU usage, counters, and other performance data. To analyze memory allocations, use the built-in MemoryDiagnoser by adding the [MemoryDiagnoser] attribute. For more information, see Diagnosers.

    Note

    The profiler supports only the [MemoryDiagnoser] and the diagnosers listed previously in this article.

    For an example of using the profiler to analyze memory allocations, see the blog post Benchmarking with Visual Studio Profiler.

    To analyze data related to other tabs such as CPU Usage, see the corresponding articles in the profiling documentation.