5

I was wondering if it's a good practice to insert performance test and measurment in the test phase of the build for a c++ project.

In my project I have some methods for which I have some execution time constraint, and my current implementation is good enough to respect them. I have some unit test implemented with gtest for those methods. I would like to take advantage of them to test that in future updates of my implementation I won't introduce some unintentional drops in performance.

Is something like this the right way to go?

auto start = std::chrono::high_resolution_clock::now().time_since_epoch().count();
some_task();
auto end = std::chrono::high_resolution_clock::now().time_since_epoch().count();
EXPECT_LT((end-start), SOME_TASK_TIME_TRHESHOLD)

Or there are some well known alternatives/libraries to implement these kind of performance checks?

Federico
  • 743
  • 9
  • 22
  • 1
    Side note: Careful with `high_resolution_clock` for timing. It is too weakly specified and you may, in the name of higher resolution, get a non-monotonic clock. More information: [What are the uses of std::chrono::high_resolution_clock?](https://stackoverflow.com/questions/37426832/what-are-the-uses-of-stdchronohigh-resolution-clock). – user4581301 Apr 26 '22 at 18:35
  • Benchmarking and unit testing are orthogonal tasks. I'd not expect to find benchmarks among the test cases. – ixSci Apr 26 '22 at 18:42
  • @user4581301 thanks for having pointed that out. It's good to know. I used that in this example because it was the fastest way to make it understandable. The real application is under windows and uses PerformanceCounters – Federico Apr 26 '22 at 18:48
  • 2
    It sounds like you just want to do performance test just to do performance tests (not actual requirements specified somewhere)? But if it IS part of the requirements, and if you are developing a library that says foo() will do some algorithm with worst case timing of 1.2xn nanoseconds, then yes, add that to foo() test. – franji1 Apr 26 '22 at 18:49
  • 1
    This question feels opinion based. Generally, for unit tests you often have optimizations disabled (to allow debugging), which would make this test pointless, unless you compile performance tests to separate binary with optimizations enabled. And I'd probably consider testing performance of the whole appplication in an environement close to real one, rather than testing single element. – Yksisarvinen Apr 26 '22 at 18:50

0 Answers0