本页内容

第 5 章:编写基准测试

本章演示了如何使用Qt Test 编写基准测试。

编写基准测试

要创建基准测试,我们需通过 QBENCHMARK 宏扩展一个测试函数。因此,基准测试函数通常由初始化代码和一个包含待测量代码的 QBENCHMARK 宏组成。本测试函数用于对QString::localeAwareCompare() 进行基准测试。

void TestBenchmark::simple()
{
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");

    QCOMPARE(str1.localeAwareCompare(str2), 0);

    QBENCHMARK {
        str1.localeAwareCompare(str2);
    }
}

初始化工作可在函数开头进行。此时,计时器尚未开始运行。QBENCHMARK 宏内的代码将被计时,并可能重复执行数次以获得精确的测量结果。

提供多种后端,可在命令行中进行选择。

数据函数

数据函数适用于创建用于比较多种数据输入的基准测试,例如将“支持区域设置的比较”与“标准比较”进行对比。

void TestBenchmark::multiple_data()
{
    QTest::addColumn<bool>("useLocaleCompare");
    QTest::newRow("locale-aware-compare") << true;
    QTest::newRow("standard-compare") << false;
}

然后,测试函数使用这些数据来确定要进行哪些基准测试。

void TestBenchmark::multiple()
{
    QFETCH(bool, useLocaleCompare);
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");

    int result;
    if (useLocaleCompare) {
        QBENCHMARK {
            result = str1.localeAwareCompare(str2);
        }
    } else {
        QBENCHMARK {
            result = (str1 == str2);
        }
    }
    Q_UNUSED(result);
}

if (useLocaleCompare) 开关应置于QBENCHMARK宏之外,以避免测量其开销。每个基准测试函数只能包含一个有效的QBENCHMARK宏。

构建可执行文件

您可以使用 CMake 或 qmake 构建测试用例可执行文件。

使用 CMake 构建

在 CMakeLists.txt 文件中配置构建设置:

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

cmake_minimum_required(VERSION 3.16)
project(tutorial5 LANGUAGES CXX)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_standard_project_setup()

qt_add_executable(tutorial5
    benchmarking.cpp
)

set_target_properties(tutorial5 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

target_link_libraries(tutorial5 PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Test
    Qt6::Widgets
)

install(TARGETS tutorial5
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET tutorial5
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})

接下来,在命令行中运行cmake ,或使用位于Qt-prefix/<version>/<platform>/bin/qt-cmake 目录下的qt-cmake 便捷脚本:

<Qt-prefix>/<version>/<platform>/bin/qt-cmake <source-dir> <build-dir> -G Ninja

然后,运行您首选的生成器工具来构建可执行文件。这里我们使用 Ninja:

ninja

使用 qmake 构建

在.pro 文件中配置您的构建设置:

QT += widgets testlib

SOURCES = benchmarking.cpp

# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial5
INSTALLS += target

接下来,运行qmake ,最后运行make 来构建可执行文件:

qmake
make

运行可执行文件

运行生成的可执行文件应显示以下输出:

********* Start testing of TestBenchmark *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestBenchmark::initTestCase()
PASS   : TestBenchmark::simple()
RESULT : TestBenchmark::simple():
     0.00030 msecs per iteration (total: 79, iterations: 262144)
PASS   : TestBenchmark::multiple(locale-aware-compare)
RESULT : TestBenchmark::multiple():"locale-aware-compare":
     0.00029 msecs per iteration (total: 78, iterations: 262144)
.....
PASS   : TestBenchmark::series(locale-aware-compare:8001)
RESULT : TestBenchmark::series():"locale-aware-compare:8001":
     0.039 msecs per iteration (total: 81, iterations: 2048)
Totals: 15 passed, 0 failed, 0 skipped, 0 blacklisted, 3971ms
********* Finished testing of TestBenchmark *********

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.