简体   繁体   English

如何在Google Test中断言执行时间?

[英]How to assert execution time in Google Test?

I am using Google's C++ testing framework Gtest. 我正在使用Google的C ++测试框架Gtest。 I would like to test a function against its execution time, eg function foo() fails if its execution is longer than 3ms. 我想针对其执行时间测试函数,例如函数foo()如果执行时间超过3ms则失败。 I could not find an ASSERT statement to achieve this. 我找不到ASSERT语句来实现这一目标。 Does gtest not include such a feature ? gtest不包含这样的功能吗?

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);

由于错误仍然存​​在,可能不存在: http//code.google.com/p/googletest/issues/detail?id = 348

I found a way to use the stats that GoogleTest prints out for this. 我找到了一种方法来使用GoogleTest为此打印的统计数据。 This is at the program level, but you could probably do the same in TearDownTestCase() to sanity check speed in a subset. 这是在程序级别,但您可以在TearDownTestCase()执行相同操作以在子集中进行健全性检查速度。 This is because there is a TestCase-level elapsed_time member function. 这是因为有一个TestCase级别的elapsed_time成员函数。

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. [==========]来自18个测试用例的338个测试运行。 (207723 ms total) [ PASSED ] 338 tests. (总共207723 ms)[PASSED] 338次测试。

YOU HAVE 13 DISABLED TESTS 你有13个残疾人测试

FrameworkTest.cpp(39): error: Expected: (elapsed) < (measurePerf ? 190 * 1000 : 170 * 1000), actual: 207723 vs 170000 FrameworkTest.cpp(39):错误:预期:(已过去)<(measurePerf?190 * 1000:170 * 1000),实际:207723 vs 170000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM