BenchmarkDotNet - 基准多目标项目

BenchmarkDotNet - Benchmark Multi-Targeted Project

我有一个针对 net35、net40、net45 和 netstandard2.0 的项目。 (https://weblog.west-wind.com/posts/2017/Jun/22/MultiTargeting-and-Porting-a-NET-Library-to-NET-Core-20)。我想对我目标的每个版本的 dotnet 进行基准测试,以确保我为旧版本的 .Net 中缺少的功能创建的 polyfill 与 dot net 功能相比表现得体。例如 net35 在 System.Collections.Concurrent 命名空间中没有任何类型。是否可以使用 BenchmarkDotNet 进行此类基准测试,如果可以,怎么做?

    new CachingActivatorFactory(
#if NET35
        new ReaderWriterCache<ConstructorInfo, Func<object[], object>>(),
#else
        new ConcurrentCache<ConstructorInfo, Func<object[],object>>(), 
#endif
        new LambdaExpressionActivatorFactory(
#if NET45 || NETSTANDARD
            new IlEmitCompiler()
#else
            new SystemExpressionCompiler()
#endif  
        )
    )

考虑到每个版本的 benchmarkdotnet 支持的内容,您将受到限制。

当前版本0.11.5支持.net standard 2.0将支持.net core > 2.0.net framework > 4.6.1 所以这些将是您可以测试的目标。

因此,例如,我可以将基准项目定位到 net461 和 netcoreapp2.2。您还在列表中包含了 .net 标准,但您不能将其作为目标,因为它不是运行时。在任何情况下,您都必须将项目目标设置为:

<TargetFrameworks>netcoreapp2.2;net461;</TargetFrameworks>

然后你用class装饰你的测试:

[CoreJob, ClrJob]

有一个变通办法,但是要限制在 FAQ.

中显示的 benchmarkdotnet 库支持的内容

Q My source code targets old versions of .NET Framework or .NET Core, but BenchmarkDotNet requires net461 and netcoreapp2.0. How can I run benchmarks in this case?

A It's a good practice to introduce an additional console application (e.g. MyAwesomeLibrary.Benchmarks) which will depend on your code and BenchmarkDotNet. Due to the fact that users usually run benchmarks in a develop environment and don't distribute benchmarks for users, it shouldn't be a problem.