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 *********

© 2025 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.