I am trying to benchmark a method with parameters.
[Benchmark]
public void ViewPlan(int x)
{
//code here
}
While executing the code with [Benchmark] annotation, I got an error as "Benchmark method ViewPlan has incorrect signature. Method shouldn't have any arguments". So I tried to add [Arguments] annotation to the method as well. Refer link: https://benchmarkdotnet.org/articles/samples/IntroArguments.html
[Benchmark]
[Arguments]
public void ViewPlan(int x)
{
//code here
}
In this [Arguments] we need to specify the value of the parameter for the method as well. However, value of x is set dynamically when the functionality is called. Is there any way to pass the parameter value dynamically in [Arguments] ? Also can we benchmark static methods?If yes then how?
I have made an example for you. See if it fits your needs.
public class IntroSetupCleanupIteration
{
private int rowCount;
private IEnumrable<object> innerSource;
public IEnumerable<object> Source => this.innerSource;
[IterationSetup]
public void IterationSetup()
{
// retrieve data or setup your grid row count for each iteration
this.InitSource(42);
}
[GlobalSetup]
public void GlobalSetup()
{
// retrieve data or setup your grid row count for every iteration
this.InitSource(42);
}
[Benchmark]
[ArgumentsSource(nameof(Source))]
public void ViewPlan(int x)
{
// code here
}
private void InitSource(int rowCount)
{
this.innerSource = Enumerable.Range(0,rowCount).Select(t=> (object)t).ToArray(); // you can also shuffle it
}
}
I don't know how you set up your data. For each iteration or once for every iteration so i include both setups.