How to benchmark C# code using BenchmarkDotNet
BenchmarkDotNet is a lightweight, open resource, effective .Net library that can completely transform your solutions into benchmarks, monitor all those solutions, and then present insights into the general performance information captured. It is quick to write BenchmarkDotNet benchmarks and the effects of the benchmarking approach are consumer welcoming as perfectly.
You can take edge of BenchmarkDotNet to benchmark equally .Net Framework and .Net Core programs. In this write-up we’ll investigate how we can do the job with BenchmarkDotNet in .Net Core. You can find BenchmarkDotNet on GitHub.
To do the job with the code examples provided in this write-up, you really should have Visible Studio 2019 set up in your procedure. If you really do not now have a duplicate, you can down load Visible Studio 2019 listed here.
Generate a console software venture in Visible Studio
Very first off, let us build a .Net Core console software venture in Visible Studio. Assuming Visible Studio 2019 is set up in your procedure, comply with the techniques outlined under to build a new .Net Core console software venture in Visible Studio.
- Start the Visible Studio IDE.
- Click on on “Create new venture.”
- In the “Create new project” window, decide on “Console Application (.Net Core)” from the record of templates exhibited.
- Click on Following.
- In the “Configure your new project” window proven subsequent, specify the identify and area for the new venture.
- Click on Generate.
This will build a new .Net Core console software venture in Visible Studio 2019.
Take note that when you build the console software venture, the ensuing Program course (created automatically in the Program.cs file) will appear like this:
course Program
static void Most important(string[] args)
Console.WriteLine("Good day Earth!")
We’ll use this venture and Program course to do the job with BenchmarkDotNet in the subsequent sections of this write-up.
Set up the BenchmarkDotNet NuGet package deal
To do the job with BenchmarkDotNet you have to put in the BenchmarkDotNet package deal. You can do this possibly by using the NuGet Offer Manager within the Visible Studio 2019 IDE, or by executing the subsequent command at the NuGet Offer Manager Console:
Set up-Offer BenchmarkDotNet
Why benchmark code?
A benchmark is a measurement or a set of measurements similar 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 solutions in your software. It is often a great approach to have the metrics at hand when you’re optimizing code. It is very essential for us to know if the variations made in the code has improved or worsened the general performance. Benchmarking also will help you to narrow in on the portions of the code in the software that need to have refactoring.
Actions for benchmarking code making use of BenchmarkDotNet
To operate BenchmarkDotNet in your .Net Framework or .Net Core software you have to comply with these techniques:
- Insert the vital NuGet package deal
- Insert Benchmark characteristics to your solutions
- Generate a BenchmarkRunner occasion
- Run the software in Launch mode
Generate a benchmarking course in .Net Core
Open up the Program.cs file and write the subsequent code in there.
[MemoryDiagnoser]
community course MemoryBenchmarkerDemo
int NumberOfItems = one hundred thousand
[Benchmark]
community string ConcatStringsUsingStringBuilder()
var sb = new StringBuilder()
for (int i = i < NumberOfItems i++)
sb.Append("Good day Earth!" + i)
return sb.ToString()
[Benchmark]
community string ConcatStringsUsingGenericList()
var record = new Record(NumberOfItems)
for (int i = i < NumberOfItems i++)
record.Insert("Good day Earth!" + i)
return record.ToString()
The previously mentioned system illustrates how you can write solutions for benchmarking. Take note the use of the Benchmark attribute on top rated of each of the solutions that are to be benchmarked.
In the Most important process of the Program.cs file you have to specify the preliminary commencing stage — the BenchmarkRunner course. This is a way of informing BenchmarkDotNet to operate benchmarks on the course specified. So, switch the default code of the Most important process in the Program.cs file making use of the subsequent code snippet.
static void Most important(string[] args)
var summary = BenchmarkRunner.Run()
Run the benchmark in your .Net Core software
If you operate the software in debug mode, here’s the error concept you will see:
Figure 1. Jogging benchmark code in debug mode will end result in an error.
When benchmarking you really should often make sure that you operate your venture in launch mode. The rationale is that for the duration of compilation the code is optimized in a different way for equally debug and launch modes. The C# compiler does a couple of optimizations in launch mode that are not readily available in debug mode.
Hence you really should operate your venture in the launch mode only. To operate benchmarking, specify the subsequent command at the Visible Studio command prompt.
dotnet operate -p BenchmarkDotNetDemo.csproj -c Launch
For very best effects, you really should make sure that all programs are shut and all unneeded procedures stopped prior to functioning benchmarks.
Take note that if you really do not specify the configuration parameter then the runtime will attempt to do benchmarking on non-optimized, debug-mode code. And you will be presented with the exact same error proven in Figure 1.
Evaluate the benchmarking effects
At the time the execution of the benchmarking approach is total, a summary of the effects will be exhibited at the console window. The summary segment includes information similar to the natural environment in which the benchmarks ended up executed, these as the BenchmarkDotNet version, running procedure, laptop or computer hardware, .Net version, compiler information, and information similar to the general performance of the software.
A couple of data files will also be developed in the BenchmarkDotNet.Artifacts folder less than the application’s root folder. Below is a summary of the effects.
Figure two. Summary of BenchmarkDotNet benchmark effects.
As evident from the summary proven in Figure two, for each benchmarked process, you will see a row of information that specifies the general performance metrics these as signify execution time, Gen , Gen 1, Gen two collections, etc.
On examining the effects proven in Figure 3, you can see that the ConcatStringUsingGenericList is considerably more quickly than the ConcatStringUsingStringBuilder process. You can also see that there are quite a few a lot more allocations soon after functioning the ConcatStringUsingStringBuilder process.
Figure 3. Yet another watch of BenchmarkDotNet benchmark effects.
Now include the RankColumn attribute on top rated of the MemoryBenchmarkerDemo course. This will include an extra column to the output indicating which process was more quickly. Run the benchmarking approach all over again making use of the subsequent command.
dotnet operate -p BenchmarkDotNetDemo.csproj -c Launch
When you operate this command, the benchmarking approach kicks off and shows the output soon after the benchmarking approach has been executed correctly. Figure 4 under reveals the output with RankColumn extra.
Figure 4. BenchmarkDotNet benchmark effects which include the RankColumn attribute.
BenchmarkDotNet is a nice device that delivers a simple way to make an educated choice about the general performance metrics of your software. In BenchmarkDotNet, invocation of a process that has the Benchmark attribute set is acknowledged as an operation. An iteration is the identify given to a collection of various operations.
You can investigate a demo ASP.Net Core software that illustrates various strategies to benchmark the code. You can get the software from the ASP.Net repo on GitHub.
How to do a lot more in C#:
Copyright © 2020 IDG Communications, Inc.