Adding configurations

Benchmark configuration can be defined by creating a custom class and inheriting it from the ManualConfig class. Here is an example of the TestBenchmark class that we created earlier containing some benchmark methods:

[Config(typeof(Config))] 
public class TestBenchmark 
{ 
  private class Config : ManualConfig 
  { 
    // We will benchmark ONLY method with names with names (which 
// contains "A" OR "1") AND (have length < 3) public Config() { Add(new DisjunctionFilter( new NameFilter(name => name.Contains("Recursive")) )); } } [Params(10,20,30)] public int Len { get; set; } [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; } } [Benchmark] public void FibonacciRecursive() { 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); } } }

In the preceding code, we defined the Config class that inherits the ManualConfig class provided in the benchmark framework. Rules can be defined inside the Config constructor. In the preceding example, there is a rule that stipulates that only those benchmark methods that contain Recursive should be executed. In our case, we have only one method, FibonacciRecursive, that will be executed and whose performance we will measure.

Another way of doing this is through the fluent API, where we can skip creating a Config class and implement the following: 

static void Main(string[] args) 
{ 
  var config = ManualConfig.Create(DefaultConfig.Instance); 
  config.Add(new DisjunctionFilter(new NameFilter(
name => name.Contains("Recursive")))); BenchmarkRunner.Run<TestBenchmark>(config); }

To learn more about BenchmarkDotNet, refer to http://benchmarkdotnet.org/Configs.htm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.3