【发布时间】:2016-04-28 13:59:51
【问题描述】:
我在 QTest 中编写了一个小型基准测试,虽然我使用了 QBENCHMARK_ONCE。
这里有一些复制问题的示例代码:
标题:
#ifndef MY_TEST_H
#define MY_TEST_H
#include <QtTest>
class MyTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void test1();
};
#endif // MY_TEST_H
cpp 文件:
#include "mytest.h"
void MyTest::initTestCase() {
qDebug() << "init";
}
void MyTest::test1() {
QBENCHMARK_ONCE {
qDebug() << "bench";
}
qDebug() << "test1";
}
QTEST_MAIN(MyTest)
运行“mytest”我得到:
********* Start testing of MyTest *********
Config: Using QtTest library 5.5.1, Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160407)
QDEBUG : MyTest::initTestCase() init
PASS : MyTest::initTestCase()
QDEBUG : MyTest::test1() bench
QDEBUG : MyTest::test1() test1
QDEBUG : MyTest::test1() bench
QDEBUG : MyTest::test1() test1
PASS : MyTest::test1()
RESULT : MyTest::test1():
0 msecs per iteration (total: 0, iterations: 1)
PASS : MyTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of MyTest *********
我希望它只运行一次。一些基准测试每次迭代需要一分钟...
我在 Linux 上使用 CMake 和 make 后端。他们测试类被编译成单独的可执行文件。由于 ctest 没有给我有用的输出,我直接运行它们,即 "# ./mytest"
//加法: CMakeLists.txt
include(CTest)
enable_testing()
set(CMAKE_AUTOMOC ON)
find_package(Qt5Test REQUIRED)
add_executable(mytest "test/mytest.cpp")
add_test(mytest mytest)
target_link_libraries(mytest Qt5::Test)
【问题讨论】:
-
您能尝试创建一个可重现的小示例吗?例如,分配给 NUMBER_ITERATIONS 的值在您的示例中是不可见的。
-
我会尝试创建一个。 NUMBER_ITERATIONS 并不重要,它只是计算函数的一个参数,它定义了内部循环(迭代算法)运行的频率。这是10,000 btw。 ;)
标签: c++ qt benchmarking qtestlib