Introduction

Performance is one of the crucial parts of the development. To improve performance, we need first to measure the code execution time and then optimize the code to improve code quality and performance.

In this article, we are going to discuss how to measure your code performance using Benchmark.Net.

We will cover

  1. What is Benchmark?
  2. Introduction BenchmarkDotNet library.
  3. A step-by-step guide to benchmarking .Net application.
  4. Analysis of Benchmarking Result

Before we start with Benchmarking, first let's discuss the performance of your code.

This article can be used by beginners, Intermediate, and professionals.

Prerequisites

  1. Visual Studio 2022
  2. .Net Core 6.0/7.0
  3. BenchmarkDotNet

What is Benchmark?

The software can be measured with its behavior while having a massive workload. Good software should work smoothly while having a heavy workload in your system.

Benchmarking is a technique to determine the performance of the pieces of code in the application. It helps you to identify the area of the code in your application that needs code optimizations.

Introduction of BenchmarkDotNet Library

BenchmarkDotNet is a lightweight, Open-source, and powerful library to track performance and shared tracked data.

Below are the details supported by the BenchmarkDotNet library.

Runtimes Languges OS Architectures
.NET 5+, .NET Framework 4.6.1+, .NET Core 2.0+, Mono, NativeAOT C#, F#, Visual Basic Windows, Linux, macOS x86, x64, ARM, ARM64, Wasm and LoongArch64

​Measured data by BenchmarkDotNet can be exported to different formats (md, html, csv, xml, json, etc.).

Below are a few important attributes you used.

[MemoryDiagnoser]
public class SampleMemoryDemo
{
//Code removed.
}

Sample code of the [MemoryDiagnoser] attributed.

Let's create a sample application to understand BenchmarkDotNet.

A step-by-step guide to benchmarking .Net application

We will create a simple console application to compare the performance of String and StringBuilder.

​Please follow the below steps to create a .Net project.

Step 1. Create a new console application named “BenchmarkingDemo.

Configure PRoject

Click on the next button to select .Net framework on the next screen.

Step 2. Select .Net framework. We are going to use .Net core 7.0 in this project.

Additional Info

Click on the Next button. A console application will be created.

Step 3. Install BenchMarkingDotNet nugget package.

Install Benchmark

Step 4. Create a new class called the “StringCompareBenchmarkPerformance” class.

CreateClass

Click on Add to create a new class.

Step 5. Add the code below to the class.

using System.Text;
using BenchmarkDotNet.Attributes;

namespace BenchmarkingDemo
{
    
    [MemoryDiagnoser]
    public class StringCompareBenchmarkPerformance
    {
        public int number = 5;

        [Benchmark]
        public string WithStringBUilder()
        {
            // Create StringBuilder object
            StringBuilder output = new StringBuilder();

            for (int i = 0; i < number; i++)
            {
                output.Append("Kirtesh").Append(i);
            }
            return output.ToString();
        }

        [Benchmark]
        public string WithString()
        {
            string strOutput = string.Empty;
            for (int i = 0; i < number; i++)
            {
                strOutput = $"{strOutput}Kirtesh{i}";
            }
            return strOutput;
        }
    }
}

In the above code.

  1. The class has been decorated with [MemoryDiagnoser] to take benefit of Memory tracking/usage.
  2. We have taken the int variable globally and the assign value 5.
  3. We have added two methods, “WithStringBuilder” and “WithString”.
  4. Both methods return the string as the output.
  5. We have decorated both methods with the [Benchmark] attribute to add these methods to the monitor.

Step 6. Let’s add the below code to the Program.cs file.

// See https://aka.ms/new-console-template for more information
using BenchmarkDotNet.Running;
using BenchmarkingDemo;

var summary = BenchmarkRunner.Run<StringCompareBenchmarkPerformance>();

In the above code.

Step 7. Let's execute the program and see the result in the output console.

ExecuteProgram

Analysed Benchmarking Result

Let's discuss the result in detail to get a better understanding.

Benchmarking Results will be saved in the newly created folder called “Benchmark. Artifacts”. Let's move to the artifacts folder. Here we can see the result folder and output in the .txt file.

Result

Let's click on the Result folder. We can have results in different formats like “.html”,”.csv” etc.

ResultDirectory

Let's click on the html file to see the result.

Output

That’s all for this article. I hope you learned something new and enjoyed this article.