Benchmark Using BenchmarkDotNet

Introduction

In this blog, we will learn how to test the performance of the C# code. For the performance, we will use benchmarking.

Benchmark

Benchmarking is relative to the performance of a code, the benchmark is a test of code and helps us to improve the performance. To perform the benchmarking in c# we have used the Benchmarkdotnet library.

BenchmarkDotNet

BenchmarkDotNet is a lightweight, open-source, powerful .NET library that can transform your methods into benchmarks, track their performance, and share reproducible measurement experiments. It's no harder than writing unit tests.

In this project we have to call an API using two types; one is using rest sharp and another is Httpwebrequest.

So using the benchmark we have compared the performance of these.

Firstly we create an Asp.net Core MVC Project, 

After creating the project you go to the Package Manager Console.

In the console, you add the benchmark NuGet package

Install-Package BenchmarkDotNet -Version 0.13.1

After that, you have to create a class name d BenchmarkLogic.

In this class,

public class BenchmarkLogic {
    public string restrequest() {
        var client = new RestClient("http://api.ipapi.com/api/103.129.97.63?access_key=ba22e041405f597bf03914c669f00a4a");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        IRestResponse response = client.Execute(request);
        return null;
    }
    public string webrequest() {
        var url = "http://api.ipapi.com/api/103.129.97.63?access_key=ba22e041405f597bf03914c669f00a4a";
        HttpWebRequest WebRequestObject = (HttpWebRequest) HttpWebRequest.Create(url);
        WebResponse Response = WebRequestObject.GetResponse();
        Stream WebStream = Response.GetResponseStream();
        StreamReader Reader = new StreamReader(WebStream);
        string webcontent = Reader.ReadToEnd();
        return null;
    }
}

After creating this class we have created another class named BenchmarkLogicLookup,

In this class,

[MemoryDiagnoser]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
public class BenchmarkLogicLookup {
    BenchmarkLogic obj = new BenchmarkLogic();
    [Benchmark]
    public void restrequest() {
            obj.restrequest();
        }
        [Benchmark]
    public void webrequest() {
        obj.webrequest();
    }
}

Now we go to Program.cs class 

In Program.cs class in the Main method, we have added

BenchmarkRunner.Run<BenchmarkLogicLookup>();

Nowin the package manager console we run a command.

To execute our benchmarks, we are going to run the project in Release configuration:

dotnet run -c Release

After hitting  enter, you'll see the benchmark result.

Benchmark using BenchmarkDotNet

Webrequest method tasks 329.3ms and restrequest take 373.3ms,

Let's take a closer look at the result.

Method

In this, you have seen the method name which you created.

Mean

The mean column gives us the average (or the arithmetic mean) of all measurements for the corresponding method. In the above example, webrequest takes 329.3ms and restrequest take 373.3 ms,(ms means millisecond)

Error

The error column refers to half of the 99.9% confidence interval. In other words, we can be 99.9% sure that the actual mean is within 6.73 milliseconds and 7.42 milliseconds of the sampled mean.

StdDev

The standard deviation (StdDev) column tells us how much the executions varied from the mean. A small standard deviation generally means that the sample mean is a good representation of the actual mean. Because we’re dealing in the world of nanoseconds, we can certainly trust these results.

Rank

The relative position of the current benchmark means among all benchmarks.

Allocated

Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)

So using the benchmark you can check the performance of your code. 

Conclusion

In this blog, we have learned how to use benchmarks in your project, and we can check the performance of our code. We have also compared the same work in different implementations.