I have a project which targets net35, net40, net45 and netstandard2.0. (https://weblog.west-wind.com/posts/2017/Jun/22/MultiTargeting-and-Porting-a-NET-Library-to-NET-Core-20). I would like to benchmark each version of dotnet I am targeting to ensure the polyfills I have created for missing features in older versions of .Net perform decently in comparison to the dot net feature. For example net35 does not have any of the types in the System.Collections.Concurrent namespace. Is it possible to do this type of benchmarking with BenchmarkDotNet, and if so how?
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
)
)
Well given what each version of benchmarkdotnet supports you will be constrained to that.
The current version 0.11.5 supports .net standard 2.0 which will support .net core > 2.0 and .net framework > 4.6.1 so those would be the target you can test.
So for instance with this I could target the benchmark project to net461 and netcoreapp2.2. You also included .net standard in your list, you cannot target that though as that is not a runtime. In any case you would have to set your project target as:
<TargetFrameworks>netcoreapp2.2;net461;</TargetFrameworks>
Then you decorate your test class with:
[CoreJob, ClrJob]
There is a work around however to being constrained to what the benchmarkdotnet library supports shows in the FAQ.
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.