1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QBENCHMARK_P_H
41#define QBENCHMARK_P_H
42
43#include <stdlib.h>
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists purely as an
50// implementation detail. This header file may change from version to
51// version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include <QtCore/qglobal.h>
57
58#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) && !defined(Q_OS_ANDROID)
59#define QTESTLIB_USE_PERF_EVENTS
60#else
61#undef QTESTLIB_USE_PERF_EVENTS
62#endif
63
64#include <QtTest/private/qbenchmarkmeasurement_p.h>
65#include <QtCore/QMap>
66#include <QtTest/qttestglobal.h>
67#if QT_CONFIG(valgrind)
68#include <QtTest/private/qbenchmarkvalgrind_p.h>
69#endif
70#ifdef QTESTLIB_USE_PERF_EVENTS
71#include <QtTest/private/qbenchmarkperfevents_p.h>
72#endif
73#include <QtTest/private/qbenchmarkevent_p.h>
74#include <QtTest/private/qbenchmarkmetric_p.h>
75
76QT_BEGIN_NAMESPACE
77
78struct QBenchmarkContext
79{
80 // None of the strings below are assumed to contain commas (see toString() below)
81 QString slotName;
82 QString tag; // from _data() function
83
84 int checkpointIndex = -1;
85
86 QString toString() const
87 {
88 return QString::fromLatin1("%1,%2,%3")
89 .arg(slotName, tag, QString::number(checkpointIndex));
90 }
91
92 QBenchmarkContext() = default;
93};
94Q_DECLARE_TYPEINFO(QBenchmarkContext, Q_RELOCATABLE_TYPE);
95
96class QBenchmarkResult
97{
98public:
99 QBenchmarkContext context;
100 qreal value = -1;
101 int iterations = -1;
102 QTest::QBenchmarkMetric metric = QTest::FramesPerSecond;
103 bool setByMacro = true;
104 bool valid = false;
105
106 QBenchmarkResult() = default;
107
108 QBenchmarkResult(
109 const QBenchmarkContext &context, const qreal value, const int iterations,
110 QTest::QBenchmarkMetric metric, bool setByMacro)
111 : context(context)
112 , value(value)
113 , iterations(iterations)
114 , metric(metric)
115 , setByMacro(setByMacro)
116 , valid(true)
117 { }
118
119 bool operator<(const QBenchmarkResult &other) const
120 {
121 return (value / iterations) < (other.value / other.iterations);
122 }
123};
124Q_DECLARE_TYPEINFO(QBenchmarkResult, Q_RELOCATABLE_TYPE);
125
126/*
127 The QBenchmarkGlobalData class stores global benchmark-related data.
128 QBenchmarkGlobalData:current is created at the beginning of qExec()
129 and cleared at the end.
130*/
131class Q_TESTLIB_EXPORT QBenchmarkGlobalData
132{
133public:
134 static QBenchmarkGlobalData *current;
135
136 QBenchmarkGlobalData();
137 ~QBenchmarkGlobalData();
138 enum Mode { WallTime, CallgrindParentProcess, CallgrindChildProcess, PerfCounter, TickCounter, EventCounter };
139 void setMode(Mode mode);
140 Mode mode() const { return mode_; }
141 QBenchmarkMeasurerBase *createMeasurer();
142 int adjustMedianIterationCount();
143
144 QBenchmarkMeasurerBase *measurer = nullptr;
145 QBenchmarkContext context;
146 int walltimeMinimum = -1;
147 int iterationCount = -1;
148 int medianIterationCount = -1;
149 bool createChart = false;
150 bool verboseOutput = false;
151 QString callgrindOutFileBase;
152 int minimumTotal = -1;
153private:
154 Mode mode_ = WallTime;
155};
156
157/*
158 The QBenchmarkTestMethodData class stores all benchmark-related data
159 for the current test case. QBenchmarkTestMethodData:current is
160 created at the beginning of qInvokeTestMethod() and cleared at
161 the end.
162*/
163class Q_TESTLIB_EXPORT QBenchmarkTestMethodData
164{
165public:
166 static QBenchmarkTestMethodData *current;
167 QBenchmarkTestMethodData();
168 ~QBenchmarkTestMethodData();
169
170 // Called once for each data row created by the _data function,
171 // before and after calling the test function itself.
172 void beginDataRun();
173 void endDataRun();
174
175 bool isBenchmark() const { return result.valid; }
176 bool resultsAccepted() const { return resultAccepted; }
177 int adjustIterationCount(int suggestion);
178 void setResult(qreal value, QTest::QBenchmarkMetric metric, bool setByMacro = true);
179
180 QBenchmarkResult result;
181 bool resultAccepted = false;
182 bool runOnce = false;
183 int iterationCount = -1;
184};
185
186// low-level API:
187namespace QTest
188{
189 int iterationCount();
190 void setIterationCountHint(int count);
191 void setIterationCount(int count);
192
193 Q_TESTLIB_EXPORT void beginBenchmarkMeasurement();
194 Q_TESTLIB_EXPORT quint64 endBenchmarkMeasurement();
195}
196
197QT_END_NAMESPACE
198
199#endif // QBENCHMARK_H
200