我们如何在 C# 中为 BenchmarkDotNet 的 [Arguments] 标记传递动态参数?
How can we pass dynamic arguments in [Arguments] tag for BenchmarkDotNet in C#?
我正在尝试使用参数对方法进行基准测试。
[Benchmark]
public void ViewPlan(int x)
{
//code here
}
在执行带有 [Benchmark] 注释的代码时,出现如下错误
"Benchmark method ViewPlan has incorrect signature. Method shouldn't have any arguments"。
所以我也尝试向该方法添加 [Arguments] 注释。
参考 link: https://benchmarkdotnet.org/articles/samples/IntroArguments.html
[Benchmark]
[Arguments]
public void ViewPlan(int x)
{
//code here
}
在此[Arguments]中,我们还需要指定方法的参数值。但是,调用该功能时会动态设置 x 的值。
有没有办法在 [Arguments] 中动态传递参数值?
我们也可以对静态方法进行基准测试吗?如果可以,那么如何?
我已经为你做了一个例子。看看它是否符合您的需求。
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
}
}
我不知道你是如何设置你的数据的。对于每次迭代或每次迭代一次,所以我包括两个设置。
我正在尝试使用参数对方法进行基准测试。
[Benchmark]
public void ViewPlan(int x)
{
//code here
}
在执行带有 [Benchmark] 注释的代码时,出现如下错误 "Benchmark method ViewPlan has incorrect signature. Method shouldn't have any arguments"。 所以我也尝试向该方法添加 [Arguments] 注释。 参考 link: https://benchmarkdotnet.org/articles/samples/IntroArguments.html
[Benchmark]
[Arguments]
public void ViewPlan(int x)
{
//code here
}
在此[Arguments]中,我们还需要指定方法的参数值。但是,调用该功能时会动态设置 x 的值。 有没有办法在 [Arguments] 中动态传递参数值? 我们也可以对静态方法进行基准测试吗?如果可以,那么如何?
我已经为你做了一个例子。看看它是否符合您的需求。
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
}
}
我不知道你是如何设置你的数据的。对于每次迭代或每次迭代一次,所以我包括两个设置。