performance measurment in unit test
Asked Answered
I

0

6

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?

Injurious answered 26/4, 2022 at 18:24 Comment(5)
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?.Vocalize
Benchmarking and unit testing are orthogonal tasks. I'd not expect to find benchmarks among the test cases.Peppy
@Vocalize 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 PerformanceCountersInjurious
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.Diedrediefenbaker
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.Pigweed

© 2022 - 2024 — McMap. All rights reserved.