How to assert execution time in Google Test?

Probably doesn't exist as the bug is still open: http://code.google.com/p/googletest/issues/detail?id=348


Why not to use such simple solution?

//pseudo code
clock_t t = clock();
foo();
const double work_time = (clock() - t) / double(CLOCKS_PER_SEC);
ASSERT_TRUE(work_time <= 0.003);

I found a way to use the stats that GoogleTest prints out for this. This is at the program level, but you could probably do the same in TearDownTestCase() to sanity check speed in a subset. This is because there is a TestCase-level elapsed_time member function.

int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    auto result(RUN_ALL_TESTS());
    ::testing::internal::TimeInMillis elapsed(
        ::testing::UnitTest::GetInstance()->elapsed_time());
    ASSERT_LT(elapsed, measurePerf ? 180 * 1000 : 215 * 1000);
    return result;
}

Representative results:

[==========] 338 tests from 18 test cases ran. (207723 ms total) [ PASSED ] 338 tests.

YOU HAVE 13 DISABLED TESTS

FrameworkTest.cpp(39): error: Expected: (elapsed) < (measurePerf ? 190 * 1000 : 170 * 1000), actual: 207723 vs 170000