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.