Vai*_*wis 3 c# performance-testing benchmarkdotnet
我正在尝试使用参数对方法进行基准测试。
[Benchmark]
public void ViewPlan(int x)
{
//code here
}
Run Code Online (Sandbox Code Playgroud)
在使用 [Benchmark] 注释执行代码时,出现错误“基准方法 ViewPlan 的签名不正确。方法不应有任何参数”。所以我也尝试向该方法添加 [Arguments] 注释。参考链接: https: //benchmarkdotnet.org/articles/samples/IntroArguments.html
[Benchmark]
[Arguments]
public void ViewPlan(int x)
{
//code here
}
Run Code Online (Sandbox Code Playgroud)
在此 [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
}
}
Run Code Online (Sandbox Code Playgroud)
不知道你的数据是怎么设置的。对于每次迭代或每次迭代一次,因此我包括这两种设置。