Take advantage of the lightweight, open source BenchmarkDotNet library to benchmark your methods and track their performance.
BenchmarkDotNet is a lightweight, open source, powerful .NET library that can transform your methods into benchmarks, track those methods, and then provide insights into the performance data captured. It is easy to write BenchmarkDotNet benchmarks and the results of the benchmarking process are user friendly as well.
You can take advantage of BenchmarkDotNet to benchmark both .NET Framework and .NET Core applications. In this article we’ll explore how we can work with BenchmarkDotNet in .NET Core. You can find BenchmarkDotNet on GitHub.
Create a console application project in Visual Studio
First off, let’s create a .NET Core console application project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core console application project in Visual Studio.
- Launch the Visual Studio IDE.
- Click on “Create new project.”
- In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
- Click Next.
- In the “Configure your new project” window shown next, specify the name and location for the new project.
- Click Create.
Install the BenchmarkDotNet NuGet package:
You can do this either via the NuGet Package Manager inside the Visual Studio 2019 IDE, or by executing the following command at the NuGet Package Manager Console:
Install-Package BenchmarkDotNet
Steps for benchmarking code using BenchmarkDotNet
To run BenchmarkDotNet in your .NET Framework or .NET Core application you must follow these steps:
- Add the necessary NuGet package
- Add Benchmark attributes to your methods
- Create a BenchmarkRunner instance
- Run the application in Release mode
Create a benchmarking class in .NET Core
Open the Program.cs file and write the following code in there.
[MemoryDiagnoser]
public class MemoryBenchmarkerDemo
{
int NumberOfItems = 100000;
[Benchmark]
public string ConcatStringsUsingStringBuilder()
{
var sb = new StringBuilder();
for (int i = 0; i < NumberOfItems; i++)
{
sb.Append("Hello World!" + i);
}
return sb.ToString();
}
[Benchmark]
public string ConcatStringsUsingGenericList()
{
var list = new List<string>(NumberOfItems);
for (int i = 0; i < NumberOfItems; i++)
{
list.Add("Hello World!" + i);
}
return list.ToString();
}
}
The above program illustrates how you can write methods for benchmarking. Note the usage of the Benchmark attribute on top of each of the methods that are to be benchmarked.
In the Main method of the Program.cs file you must specify the initial starting point — the BenchmarkRunner class. This is a way of informing BenchmarkDotNet to run benchmarks on the class specified. So, replace the default code of the Main method in the Program.cs file using the following code snippet.
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<MemoryBenchmarkerDemo>();
}
Run the benchmark in your .NET Core application
If you run the application in debug mode, here’s the error message you’ll see:
When benchmarking you should always ensure that you run your project in release mode. The reason is that during compilation the code is optimized differently for both debug and release modes. The C# compiler does a few optimizations in release mode that are not available in debug mode.
Hence you should run your project in the release mode only. To run benchmarking, specify the following command at the Visual Studio command prompt.
dotnet run -p BenchmarkDotNetDemo.csproj -c Release
For best results, you should make sure that all applications are closed and all unnecessary processes stopped prior to running benchmarks.
0 Comments