首页 > 解决方案 > BenchmarkDotNet,跳过特定运行时的基准

问题描述

( https://benchmarkdotnet.org/ )

是否可以跳过特定运行时的单个基准测试部分?

例如,我想测试 4.7.2 和 Core 3.1 的几个功能,但一个只能在 Core31 上进行测试

[Benchmark]
public void CoreOnly()
{
#if NETCOREAPP3_1
    //some stuff i only want to test with 3.1
#endif
}

[Benchmark]
public void General()
{
    //some stuff i want to test on all runtimes
}

这就是我到目前为止所做的。有没有更好的办法?

标签: c#benchmarkdotnet

解决方案


这在设计上是不可能的。

当运行以 XYZ 框架为目标的宿主进程时,BDN 使用反射来获取可用方法(基准)的列表。如果您使用的是#if定义,那么每个主机进程目标框架的基准测试列表都会有所不同。

性能 repo 文档描述了如何在此处比较多个运行时性能: https ://github.com/dotnet/performance/blob/master/docs/benchmarkdotnet.md#multiple-runtimes

主机进程必须是您要比较的运行时的最低通用 API 分母!

您可以在此处阅读有关测试多个运行时的更多信息: https ://benchmarkdotnet.org/articles/configs/toolchains.html#multiple-frameworks-support


推荐阅读