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