BenchmarkDotNet - Reliable And Efficient .NET Code Performance Measure

The performance of written code has always been a crucial and reliable measurement and getting them accurate is a big challenge. We have always needed a tool that can provide us all the necessary matrices and then help us decide what is the best approach. In my initial experience with BenchmarkDotNet, I found it easiest to use among all free or paid code performance monitor tools.

What is this?

BenchmarkDotNet is a performance monitor tool that provides high-quality performance research. It helps to analyze the execution of a program and getting many useful statistics.

What is wrong with TimeSpan and Stopwatch?

Using TimeSpan

I have seen many developers using TimeSpan to measure the time elapsed in program execution. But it is very immature to use TimeSpan as it doesn’t give accurate time.

Using Stopwatch

Stopwatch is more accurate than TimeSpan since it is very easy to use but it gives very limited information as elapsed time. In addition to this, it has few other problems as,

  1. Sometimes Stopwatch results are inconsistent.
  2. First execution takes a longer time.
  3. It gives a negative value as well as a weird result.

Despite these many issues with Stopwatch, I had used them in my many previous articles and analysis. In all of them, I had tried to run them in many iterations. From now on,  I will be using this to compare the performance of different code snippets/features.

How to use BenchmarkDotNet?

Go to your solution and click on Manage Nuget Packages for Solution.

BenchmarkDotNet : Reliable and Efficient .NET Code Performance Measure

And you will see this screen,

  1. Search the Term “Benchmark”
  2. Click on the BenchmarkDotNet
  3. Select the project, where it will be used.
  4. Install this to the project(s)

This is applicable to the .NET Framework and .NET Core application.

BenchmarkDotNet : Reliable and Efficient .NET Code Performance Measure

Now, you will see the packages, which will be installed for your selected project.

BenchmarkDotNet : Reliable and Efficient .NET Code Performance Measure

After that, it will do the rest of the work and you will be ready to start a performance monitor for your application.

  • Now add the [Benchmark] attribute to the method(s) you want to monitor or compare. Make sure to add namespace BenchmarkDotNet.Attributes;
  • Now use this code in the main entry program BenchmarkRunner.Run<Test>(); and the needed namespace for this is BenchmarkDotNet.Running;
    • The test is the name of the class where method(s) for benchmarking are available.

Code Example

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace BenchmarkSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BenchmarkRunner.Run<Test>();
            Console.ReadLine();
        }
   }
 
    public class Test
    {
        [Benchmark]
        public void Method1()
        {
            Thread.Sleep(3000);
        }
        [Benchmark]
        public void Method2()
        {
            Thread.Sleep(2000);
        }
    }
}

Must have,

  1. All methods should be public.
  2. Methods or classes should be instances not static.
  3. Code should be in release mode.

Output

It generates a very detailed report about all the execution environment which you can see in the execution and this is the summary about the execution time,

BenchmarkDotNet : Reliable and Efficient .NET Code Performance Measure

Features

Other than execution time, Benchmark provides extensive detail of much other information as well, a few important features are below,

  1. We can get the performance of different .NET Environments.
  2. We can add additional information to the report.
  3. Very extensive statistics are available for code execution.
  4. We can baseline a method and then other methods with coming in relative execution.
  5. It allows us to log the report.

System and Environment Requirement

  • IDE: Visual Studio 2015 or higher/ Rider
  • .NET Core 2.0 or higher
  • .NET Framework 4.6 or higher
  • NUGET 3.X+
  • OS: Windows, Linus, MacOS
  • Languages: C#, F#, VB

Happy Benchmarking !!!