Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qttestlib-tutorial5.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5/*!
6 \page qttestlib-tutorial5-example.html
7 \previouspage {Chapter 4: Replaying GUI Events}{Chapter 4}
8 \nextpage {Chapter 6: Skipping Tests with QSKIP}{Chapter 6}
9
10 \title Chapter 5: Writing a Benchmark
11 \brief How to write a benchmark.
12
13 This chapter demonstrates how to write benchmarks using Qt Test.
14
15 \section1 Writing a Benchmark
16 To create a benchmark we extend a test function with a QBENCHMARK macro.
17 A benchmark test function will then typically consist of setup code and
18 a QBENCHMARK macro that contains the code to be measured. This test
19 function benchmarks QString::localeAwareCompare().
20
21 \snippet tutorial5/benchmarking.cpp 0
22
23 Setup can be done at the beginning of the function. At this point, the clock
24 is not running. The code inside the QBENCHMARK macro will be
25 measured, and possibly repeated several times in order to get an
26 accurate measurement.
27
28 Several \l {testlib-benchmarking-measurement}{back-ends} are available
29 and can be selected on the command line.
30
31 \section1 Data Functions
32
33 Data functions are useful for creating benchmarks that compare
34 multiple data inputs, for example locale aware compare against standard
35 compare.
36
37 \snippet tutorial5/benchmarking.cpp 1
38
39 The test function then uses the data to determine what to benchmark.
40
41 \snippet tutorial5/benchmarking.cpp 2
42
43 The \c{if (useLocaleCompare)} switch is placed outside the QBENCHMARK
44 macro to avoid measuring its overhead. Each benchmark test function
45 can have one active QBENCHMARK macro.
46
47 \section1 Building the Executable
48
49 \include {building-examples.qdocinc} {building the executable} {tutorial5}
50
51 \section1 Running the Executable
52
53 Running the resulting executable should give you the following
54 output:
55
56 \snippet code/doc_src_qtestlib.qdoc 14
57*/