How to benchmark methods from different classes that require arguments?

I want to use BenchmarkDotNet to run benchmarks on all three Run() methods of the following classes. But it’s not clear what the syntax should be.

class Class1
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

class Class2
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

class Class3
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

I tried syntax like this.

BenchmarkRunner.Run<Class1>();

But this gives me an error.

Benchmark method ReadRow has incorrect signature.
Method shouldn’t have any arguments.

Questions:
To compare the performance of these three methods:

  • How do I satisfy the argument requirements and eliminate this error?
  • How do I compare the performance of the methods? Do I simply call BenchmarkRunner.Run<Class1>() for each class, sequentially?

>Solution :

How do I satisfy the argument requirements and eliminate this error?

Create class containing parameterless benchmark method and invoke the becnhmarked method there passing parameters. Either create the param in-place or use some precreated ones:

class MyBenchmark
{
    [Benchmark]
    public bool RunClass1()
    {    
        var columns = // create string[]; maybe use field to store it
        new Class1().BenchmarkedMethod(ref columns);
    }
}

Also check the docs for option for:

How do I compare the performance of the methods

Move all method inside one benchmark class.

class MyBenchmark
{
    [Benchmark]
    public bool RunClass1()
    {    
        var columns = // create string[];
        return new Class1().BenchmarkedMethod(ref columns);
    }

    [Benchmark]
    public bool RunClass2()
    {    
        var columns = // create string[];
        return new Class2().BenchmarkedMethod(ref columns);
    }
}

Leave a Reply