Improving your C# Skills
上QQ阅读APP看书,第一时间看更新

Exploring BenchmarkDotNet

In this section, we will explore BenchmarkDotNet and learn how effectively it can be used to measure application performance.

It can simply be installed using a NuGet package manager console window or through the Project References section of your project. To install BenchmarkDotNet, execute the following command:

Install-Package BenchmarkDotNet 

The preceding command adds a BenchmarkDotNet package from NuGet.org.

To test the BenchmarkDotNet tool, we will create a simple class that contains two methods to generate a Fibonacci series for a sequence of 10 numbers. The Fibonacci series can be implemented in multiple ways, which is why we are using it to measure which code snippet is faster and more performance efficient.

Here is the first method that generates the Fibonacci sequence iteratively:

public class TestBenchmark 
{ 
  int len= 10; 
  [Benchmark] 
  public  void Fibonacci() 
  { 
    int a = 0, b = 1, c = 0; 
    Console.Write("{0} {1}", a, b); 
 
    for (int i = 2; i < len; i++) 
    { 
      c = a + b; 
      Console.Write(" {0}", c); 
      a = b; 
      b = c; 
    } 
  } 
} 

Here is another method that uses the recursive approach to generate the Fibonacci series:

 
[Benchmark] 
public  void FibonacciRecursive() 
{ 
  int len= 10; 
  Fibonacci_Recursive(0, 1, 1, len); 
} 
 
private void Fibonacci_Recursive(int a, int b, int counter, int len) 
{ 
  if (counter <= len) 
  { 
    Console.Write("{0} ", a); 
    Fibonacci_Recursive(b, a + b, counter + 1, len); 
  } 
}  

Note that both of the main methods of the Fibonacci series contain a Benchmark attribute. This actually tells the BenchmarkRunner to measure methods that contain this attribute. Finally, we can call the BenchmarkRunner from the main entry point of the application that measures the performance and generates a report, as shown in the following code:

static void Main(string[] args)
{
BenchmarkRunner.Run<TestBenchmark>();
Console.Read();
}

Once the benchmark is run, we will get the report as follows:

As well as this, it also generates files in the root folder of an application that runs the BenchmarkRunner. Here is the .html file that contains the information about the version of BenchmarkDotNet and the OS, the processor, frequency, resolution, and timer details, the .NET version (in our case, .NET Core SDK 2.0.0), host, and so on:

The table contains four columns. However, we can add more columns, which are optional by default. We can also add custom columns as well. The Method is the name of the method that contains the benchmark attribute, the Mean is the average time it takes for all the measurements to be taken (where us is microseconds), Error is the time taken to process errors, and StdDev is the standard deviation of the measurements.

After comparing both the methods, the FibonacciRecursive method is more efficient as the Mean, Error, and StdDev values are smaller than the Fibonacci method.

Other than the HTML, two more files are created, a Comma Separated Value (CSV) file and a Markdown Documentation (MD) file which contains the same information.