7

I'm using BenchmarkDotNet library for performance checks and I want to inject parameters when using the benchmark class. Something like this:

public class Program
{
    public static void Main()
    {
        var benchmark1 = new BenchmarkWithParameter(100);
        BenchmarkRunner.Run(benchmark1);

        var benchmark2 = new BenchmarkWithParameter(200);
        BenchmarkRunner.Run(benchmark2);
    }
}

public class BenchmarkWithParameter
{
    public BenchmarkWithParameter(int waitTime)
    {
        WaitTime = waitTime;
    }

    public int WaitTime { get; }

    [Benchmark]
    public void Run()
    {
        Thread.Sleep(WaitTime);
    }
}

Is there a way to achieve it?

I know that I can use Params, ParamsSource and ArgumentsSource attributes, but it's mean that I need to change the benchmark class for every parameter change instead of injecting different parameters.

My main goal is to write the benchmark class once and using it many times with different parameters.

itaiy
  • 1,152
  • 1
  • 13
  • 22
  • 1
    Not very elegant, but currently I write the parameters to a text file and then read them in `GlobalSetup` where I need them to instantiate a class. The parameters are specified on command line in my case. – dashesy Aug 14 '20 at 21:56

1 Answers1

9

I am sorry, but as of today, it's impossible.

The design of the library is limited by the fact that to avoid side effects of previously executed benchmarks we run every benchmark in a stand-alone process. To be able to do that, we generate and compile a new project for every benchmark. The compilation is what limits us to known ways of providing the parameters/arguments.

All the available options are described here: https://benchmarkdotnet.org/articles/features/parameterization.html

Adam Sitnik
  • 1,256
  • 11
  • 15
  • How can one use the `setter` to change the parameter [here](https://benchmarkdotnet.org/articles/features/parameterization.html#source-code)? Considering that runner only runs on a type like `BenchmarkRunner.Run();` – dashesy Aug 14 '20 at 21:54