2

I intend to use BenchmarkDotNet to test some methods inside various project.

As it may be as easy as adding the Benchmark attribute to the methods, I have considerable doubt about using it in the project I work on.

The project is consisting of 2 parts:

  • native core
  • .Net features

The native part initializes the .Net part and calls its methods via interop.

So using a test project and adding references to the .Net projects and starting the benchmark application will not work as the native code should be started first.

Any idea, if I can somehow skip the native part and make BenchmarkDotNet test only the methods? Or should I look for another benchmarking approach?

Nestor
  • 8,194
  • 7
  • 77
  • 156

2 Answers2

2
  • Create a new public class
  • Create benchmark method, call the thing that you want to measure. Mark it with [Benchmark] attribute.

If it's enough to run the initialization code once (for many benchmark iterations):

  • Create a new method, implement the initialization and mark it with [GlobalSetup] attribute
  • BenchmarkDotNet is going to create a new instance of your class, call the setup method once and afterward start the benchmarking of your code.

If it's not enough to run the initialization once and it's required to be called every time before benchmarked method call:

  • Create a new method, implement the initialization and mark it with [IterationSetup] attribute. Set the run strategy to RunStrategy.Monitoring.
  • BenchmarkDotNet is going to create a new instance of your class, call the setup method once before calling the benchmark and repeat it many times.

You can read more about setups and cleanups in our official docs

Adam Sitnik
  • 1,256
  • 11
  • 15
  • Unfortunately, I can't properly set up the initialization without the native part as it handles authorization, parameter init and so on. So I should just use the attributes and when the .Net code starts, it should log the performance. I wonder if it is possible using BenchmarkDotNet. – Nestor Aug 29 '17 at 09:35
  • @Nestor Unfortunately, it's not possible. You need a profiler to measure that. – Adam Sitnik Aug 30 '17 at 10:11
0

Looking at the specification for BenchmarkDotNet it seems you can mark any method as benchmark, it doesn't have to be the actual methods that your native app calls. Similar to unit tests, you can write benchmarks that call methods in your .NET code with valid parameters that would otherwise come from the native core.

I would suggest creating a separate Benchmark project similar to how you'd have a separate Test project.

Simmetric
  • 1,443
  • 2
  • 12
  • 20
  • It is crucial to measure the valid usage of interopcode and the initialization overheads of .Net, so I can't simulate the native part by creating a .Net project. That is why I intend to make BenchmarkDotNet skip the native parts and start measuring when the .Net part kicks in. – Nestor Aug 29 '17 at 05:04