BenchmarkDotNet is a light-weight, open source, powerful .Net library that can transform your approaches into benchmarks, track those approaches, and then deliver insights into the general performance details captured. It is quick to create BenchmarkDotNet benchmarks and the final results of the benchmarking system are user friendly as well.
You can get advantage of BenchmarkDotNet to benchmark the two .Net Framework and .Net Core applications. In this short article we’ll take a look at how we can do the job with BenchmarkDotNet in .Net Core. You can find BenchmarkDotNet on GitHub.
To do the job with the code illustrations provided in this short article, you ought to have Visual Studio 2019 put in in your program. If you do not presently have a duplicate, you can obtain Visual Studio 2019 right here.
Build a console software venture in Visual Studio
First off, let’s generate a .Net Core console software venture in Visual Studio. Assuming Visual Studio 2019 is put in in your program, adhere to the ways outlined down below to generate a new .Net Core console software venture in Visual Studio.
- Launch the Visual Studio IDE.
- Click on “Create new venture.”
- In the “Create new project” window, select “Console Application (.Net Core)” from the checklist of templates exhibited.
- Click Following.
- In the “Configure your new project” window shown following, specify the title and place for the new venture.
- Click Build.
This will generate a new .Net Core console software venture in Visual Studio 2019.
Notice that when you generate the console software venture, the ensuing System class (created routinely in the System.cs file) will glimpse like this:
class System
static void Primary(string[] args)
Console.WriteLine("Hi Entire world!")
We’ll use this venture and System class to do the job with BenchmarkDotNet in the subsequent sections of this short article.
Set up the BenchmarkDotNet NuGet bundle
To do the job with BenchmarkDotNet you have to install the BenchmarkDotNet bundle. You can do this either by way of the NuGet Deal Supervisor within the Visual Studio 2019 IDE, or by executing the adhering to command at the NuGet Deal Supervisor Console:
Set up-Deal BenchmarkDotNet
Why benchmark code?
A benchmark is a measurement or a set of measurements related to the general performance of a piece of code in an software. Benchmarking code is critical to being familiar with the general performance metrics of the approaches in your software. It is constantly a superior tactic to have the metrics at hand when you’re optimizing code. It is extremely vital for us to know if the alterations manufactured in the code has enhanced or worsened the general performance. Benchmarking also allows you to slim in on the parts of the code in the software that will need refactoring.
Methods for benchmarking code employing BenchmarkDotNet
To run BenchmarkDotNet in your .Net Framework or .Net Core software you have to adhere to these ways:
- Increase the important NuGet bundle
- Increase Benchmark characteristics to your approaches
- Build a BenchmarkRunner occasion
- Operate the software in Release method
Build a benchmarking class in .Net Core
Open the System.cs file and create the adhering to code in there.
[MemoryDiagnoser]
community class MemoryBenchmarkerDemo
int NumberOfItems = a hundred thousand
[Benchmark]
community string ConcatStringsUsingStringBuilder()
var sb = new StringBuilder()
for (int i = i < NumberOfItems i++)
sb.Append("Hi Entire world!" + i)
return sb.ToString()
[Benchmark]
community string ConcatStringsUsingGenericList()
var checklist = new Record(NumberOfItems)
for (int i = i < NumberOfItems i++)
checklist.Increase("Hi Entire world!" + i)
return checklist.ToString()
The over plan illustrates how you can create approaches for benchmarking. Notice the utilization of the Benchmark attribute on major of each and every of the approaches that are to be benchmarked.
In the Primary process of the System.cs file you have to specify the preliminary beginning point — the BenchmarkRunner class. This is a way of informing BenchmarkDotNet to run benchmarks on the class specified. So, switch the default code of the Primary process in the System.cs file employing the adhering to code snippet.
static void Primary(string[] args)
var summary = BenchmarkRunner.Operate()
Operate the benchmark in your .Net Core software
If you run the software in debug method, here’s the mistake message you’ll see:
When benchmarking you ought to constantly be certain that you run your venture in launch method. The reason is that during compilation the code is optimized in a different way for the two debug and launch modes. The C# compiler does a couple optimizations in launch method that are not available in debug method.
As a result you ought to run your venture in the launch method only. To run benchmarking, specify the adhering to command at the Visual Studio command prompt.
dotnet run -p BenchmarkDotNetDemo.csproj -c Release
For most effective final results, you ought to make certain that all applications are closed and all avoidable procedures stopped prior to jogging benchmarks.
Notice that if you do not specify the configuration parameter then the runtime will try to do benchmarking on non-optimized, debug-method code. And you’ll be introduced with the same mistake shown in Figure one.
Assess the benchmarking final results
When the execution of the benchmarking system is entire, a summary of the final results will be exhibited at the console window. The summary portion consists of facts related to the environment in which the benchmarks ended up executed, these as the BenchmarkDotNet model, working program, pc components, .Net model, compiler facts, and facts related to the general performance of the software.
A couple information will also be made in the BenchmarkDotNet.Artifacts folder underneath the application’s root folder. Here is a summary of the final results.
As apparent from the summary shown in Figure two, for each and every benchmarked process, you will see a row of details that specifies the general performance metrics these as signify execution time, Gen , Gen one, Gen two collections, etcetera.
On analyzing the final results shown in Figure 3, you can see that the ConcatStringUsingGenericList is a great deal more quickly than the ConcatStringUsingStringBuilder process. You can also see that there are many much more allocations soon after jogging the ConcatStringUsingStringBuilder process.
Now add the RankColumn attribute on major of the MemoryBenchmarkerDemo class. This will add an additional column to the output indicating which process was more quickly. Operate the benchmarking system once more employing the adhering to command.
dotnet run -p BenchmarkDotNetDemo.csproj -c Release
When you run this command, the benchmarking system kicks off and displays the output soon after the benchmarking system has been executed properly. Figure four down below reveals the output with RankColumn added.
BenchmarkDotNet is a nice software that presents a very simple way to make an knowledgeable determination about the general performance metrics of your software. In BenchmarkDotNet, invocation of a process that has the Benchmark attribute set is known as an procedure. An iteration is the title specified to a selection of several functions.
You can take a look at a demo ASP.Net Core software that illustrates several methods to benchmark the code. You can get the software from the ASP.Net repo on GitHub.
How to do much more in C#:
Copyright © 2020 IDG Communications, Inc.