Search code examples
c#benchmarkdotnet

BenchmarkDotNet - How to inject parameters outside from the class


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.


Solution

  • 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