Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict a BenchmarkDotNet job to run only on specific platforms?

I am writing an F# port of a program I wrote in native code in the past. I used BenchmarkDotNet to measure its performance. I also placed a native EXE in the application's output directory.

I set my native program as the baseline benchmark and saw it was 5x faster than my F# program. Just as I expected!

However, the native program is posted on GitHub and distributed as a Win64 binary only. In case somebody using another OS tries to run it, it will crash.

So, how to specify that this benchmark will only run on 64-bit Windows?

like image 640
Theodore Tsirpanis Avatar asked Sep 15 '25 08:09

Theodore Tsirpanis


1 Answers

In BenchmarkDotNet, there is a concept of Jobs. Jobs define how the benchmark should be executed. So, you can express your "x64 only" condition as a job. Note that there are several different 64x jit-compilers depends on runtime (LegacyJIT-x64 and RyuJIT-x64 for the full .NET Framework, RyuJIT-x64 for .NET Core, and Mono JIT compiler). You can request not only a specific platform but also a specific JIT-compiler (it can significantly affect performance), e.g.:

[<RyuJitX64Job>]
member this.MyAwesomeBenchmark () = // ...

In this case, a user will be notified that it's impossible to compile the benchmark for required platform.

Unfortunately, there is no way to require a specific OS for now (there is only one option: current OS). So, in your case, it's probably better to check System.Environment.Is64BitOperatingSystem and System.Environment.OSVersion at the start and don't run benchmarks on invalid operation systems.

like image 93
AndreyAkinshin Avatar answered Sep 17 '25 19:09

AndreyAkinshin