Benchmarking .NET 6 applications using BenchmarkDotNet

Benchmarking is essential to the software development process, enabling developers to measure their applications’ performance and identify improvement areas. To accurately gauge the performance of .NET 6 applications, developers can rely on BenchmarkDotNet, a popular open-source benchmarking library.

Setting up BenchmarkDotNet

To install the BenchmarkDotNet NuGet package in our project, we run the following command in the project directory:

dotnet add package BenchmarkDotNet

Creating a benchmark

We will define a benchmark class. That contains the methods we want to benchmark. Check out the following example.

public class MathBenchmark
{
[Benchmark]
public int SquareRootBenchmark()
{
return (int)Math.Sqrt(42);
}
[Benchmark]
public int PowerOfTwoBenchmark()
{
return (int)Math.Pow(2, 10);
}
}

In the code above, we have defined a class named MathBenchmark, which contains two benchmark methods for measuring the performance of mathematical operations: SquareRootBenchmark() and PowerOfTwoBenchmark() methods. The [Benchmark] attribute is applied to these two methods. These methods perform square root and power-of-two calculations, respectively, and are intended to be used with the BenchmarkDotNet library to measure their execution times.

Running the benchmark

To execute the benchmark, our main function is as follows:

using BenchmarkDotNet.Attributes;

public class MathBenchmark
{
    [Benchmark]
    public  float SquareRootBenchmark()
    {
        // return squareroot of first 1000 integers
        float sum = 0;
        for (int i = 0; i < 1000; i++)
        {
            sum += (int)Math.Sqrt(i);
        }
        return sum;
    }
    
    [Benchmark]
    public int PowerOfTwoBenchmark()
    {
        // Return the sum of powers of two for the first 1000 integers
        int sum = 0;
        for (int i = 0; i < 1000; i++)
        {
            sum += (int)Math.Pow(2, i);
        }
        return sum;
    }
}
Benchmark execution for math operations using BenchmarkDotNet

Note: When we run the application, BenchmarkDotNet will automatically detect the benchmark class and execute the defined benchmark methods.

In the MathBenchmark.cs file:

  • Line 1: We use the BenchmarkDotNet.Attributes namespace for benchmarking annotations and attributes.

  • Line 3: We define a class named MathBenchmark with benchmarking methods.

  • Lines 5 and 17: We apply the [Benchmark] attribute to SquareRootBenchmark() and PowerOfTwoBenchmark() methods.

  • Lines 6–15: The SquareRootBenchmark() method calculates the sum of square roots for the first 1000 integers.

  • Lines 18–27: The PowerOfTwoBenchmark() method calculates the sum of powers of two for the first 1000 integers.

In the Program.cs file:

  • Line 1: We use the BenchmarkDotNet.Running namespace for benchmark execution.

  • Line 3: We define an internal class named Program.

  • Lines 5–8: The entry point of the program is the Main() method. It invokes BenchmarkRunner.Run<MathBenchmark>() and executes and runs the benchmarks defined in the MathBenchmark class. It captures and stores the benchmarking results in the summary variable.

Analyzing benchmark results

BenchmarkDotNet generates detailed reports and statistical data to help us analyze the benchmark results effectively. By default, BenchmarkDotNet outputs its results in the console. However, we can also generate HTML or CSV reports for further analysis.

| Method | Mean | Error | StdDev |
|-------------------- |----------:|----------:|----------:|
| SquareRootBenchmark | 2.270 us | 0.0114 us | 0.0095 us |
| PowerOfTwoBenchmark | 24.471 us | 0.0541 us | 0.0422 us |
Benchmark results

By following the steps above, we can have detailed performance measurements and analysis of our methods.

Copyright ©2024 Educative, Inc. All rights reserved