4

I want to measure performance of some methods in my console application using BenchmarkDotNet library.

The question is: should I create a separate project in my solution where I will copy the methods I am interested in measuring and do the measuring there or should I add all the attributes necessary for measuring into the existing project?

What is the convention here?

ShrikeThe
  • 77
  • 6

1 Answers1

6

You can think about it as of adding unit tests for your console app. You don't add the tests to the app itself, but typically create a new project that references (not copies) the logic that you want to test.

In my opinion the best approach would be to:

  1. Add a new console app for benchmarks to your solution.
  2. In the benchmarks app, add a project reference to the existing console app.
  3. Add new benchmarks that have all the BDN annotations to the benchmark project, but implement the benchmarks by referencing public types and methods exposed by your console app. Don't copy the code (over time you might introduce changes to one copy and end up testing outdated version).
Adam Sitnik
  • 1,256
  • 11
  • 15
  • 2
    Thanks for the tips! Especially the last one since I was in the process of copying everything to new project :D Makes more sense to reference it. – ShrikeThe Feb 23 '22 at 15:54
  • Any suggestions on naming conventions? For tests, folder `tests` in the root of the repo is quite common and the `.Tests` suffix in the project name is a standard, too. I have seen many variations when the unit, integration, and E2E tests are differentiated, but separate folders under the `tests` folder make sense to me and keeping the same suffix for all seems OK as only the unit tests will have 1:1 correspondence with the implementation projects. But where to put the benchmarks? For now, I opted for `benches` in the repo root and `.Benchmarks` suffix. – Palec Dec 04 '22 at 22:44
  • @Palec `.Benchmarks` suffix sounds very good to me. When it comes to the subfolder name I am not 100% sure myself. My current approach would be to keep it under `tests` if that is the only benchmark project for given repo solution. If you have more benchmark projects I would move them to dedicated, `benchmarks` subfolder (on the same level as src and tests) – Adam Sitnik Dec 05 '22 at 11:16
  • I went for `benches` to keep the folder name short. Though even about `bench`. The reason for a separate folder from `tests` is separate treatment by the pipelines -- benchmarks may need to run separately from tests, possibly not during all builds, and they have different outputs. – Palec Dec 05 '22 at 12:27