Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/testlib/qbenchmark.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | #include <QtTest/qbenchmark.h> | - | ||||||||||||
41 | #include <QtTest/private/qbenchmark_p.h> | - | ||||||||||||
42 | #include <QtTest/private/qbenchmarkmetric_p.h> | - | ||||||||||||
43 | - | |||||||||||||
44 | #include <QtCore/qprocess.h> | - | ||||||||||||
45 | #include <QtCore/qdir.h> | - | ||||||||||||
46 | #include <QtCore/qset.h> | - | ||||||||||||
47 | #include <QtCore/qdebug.h> | - | ||||||||||||
48 | - | |||||||||||||
49 | QT_BEGIN_NAMESPACE | - | ||||||||||||
50 | - | |||||||||||||
51 | QBenchmarkGlobalData *QBenchmarkGlobalData::current; | - | ||||||||||||
52 | - | |||||||||||||
53 | QBenchmarkGlobalData::QBenchmarkGlobalData() | - | ||||||||||||
54 | : measurer(0) | - | ||||||||||||
55 | , walltimeMinimum(-1) | - | ||||||||||||
56 | , iterationCount(-1) | - | ||||||||||||
57 | , medianIterationCount(-1) | - | ||||||||||||
58 | , createChart(false) | - | ||||||||||||
59 | , verboseOutput(false) | - | ||||||||||||
60 | , minimumTotal(-1) | - | ||||||||||||
61 | , mode_(WallTime) | - | ||||||||||||
62 | { | - | ||||||||||||
63 | setMode(mode_); | - | ||||||||||||
64 | } executed 815 times by 1 test: end of block Executed by:
| 815 | ||||||||||||
65 | - | |||||||||||||
66 | QBenchmarkGlobalData::~QBenchmarkGlobalData() | - | ||||||||||||
67 | { | - | ||||||||||||
68 | delete measurer; | - | ||||||||||||
69 | QBenchmarkGlobalData::current = 0; | - | ||||||||||||
70 | } executed 1238 times by 508 tests: end of block Executed by:
| 1238 | ||||||||||||
71 | - | |||||||||||||
72 | void QBenchmarkGlobalData::setMode(Mode mode) | - | ||||||||||||
73 | { | - | ||||||||||||
74 | mode_ = mode; | - | ||||||||||||
75 | - | |||||||||||||
76 | if (measurer)
| 73-815 | ||||||||||||
77 | delete measurer; executed 73 times by 1 test: delete measurer; Executed by:
| 73 | ||||||||||||
78 | measurer = createMeasurer(); | - | ||||||||||||
79 | } executed 888 times by 1 test: end of block Executed by:
| 888 | ||||||||||||
80 | - | |||||||||||||
81 | QBenchmarkMeasurerBase * QBenchmarkGlobalData::createMeasurer() | - | ||||||||||||
82 | { | - | ||||||||||||
83 | QBenchmarkMeasurerBase *measurer = 0; | - | ||||||||||||
84 | if (0) { dead code: { } | - | ||||||||||||
85 | #ifdef QTESTLIB_USE_VALGRIND dead code: { } | - | ||||||||||||
86 | } else if (mode_ == CallgrindChildProcess || mode_ == CallgrindParentProcess) { dead code: { }
| - | ||||||||||||
87 | measurer = new QBenchmarkCallgrindMeasurer; | - | ||||||||||||
88 | #endif | - | ||||||||||||
89 | #ifdef QTESTLIB_USE_PERF_EVENTS | - | ||||||||||||
90 | } else if (mode_ == PerfCounter) { never executed: end of block
| 0-888 | ||||||||||||
91 | measurer = new QBenchmarkPerfEventsMeasurer; | - | ||||||||||||
92 | #endif | - | ||||||||||||
93 | #ifdef HAVE_TICK_COUNTER | - | ||||||||||||
94 | } else if (mode_ == TickCounter) { never executed: end of block
| 0-888 | ||||||||||||
95 | measurer = new QBenchmarkTickMeasurer; | - | ||||||||||||
96 | #endif | - | ||||||||||||
97 | } else if (mode_ == EventCounter) { never executed: end of block
| 0-815 | ||||||||||||
98 | measurer = new QBenchmarkEvent; | - | ||||||||||||
99 | } else { executed 73 times by 1 test: end of block Executed by:
| 73 | ||||||||||||
100 | measurer = new QBenchmarkTimeMeasurer; | - | ||||||||||||
101 | } executed 815 times by 1 test: end of block Executed by:
| 815 | ||||||||||||
102 | measurer->init(); | - | ||||||||||||
103 | return measurer; executed 888 times by 1 test: return measurer; Executed by:
| 888 | ||||||||||||
104 | } | - | ||||||||||||
105 | - | |||||||||||||
106 | int QBenchmarkGlobalData::adjustMedianIterationCount() | - | ||||||||||||
107 | { | - | ||||||||||||
108 | if (medianIterationCount != -1) {
| 0-408 | ||||||||||||
109 | return medianIterationCount; never executed: return medianIterationCount; | 0 | ||||||||||||
110 | } else { | - | ||||||||||||
111 | return measurer->adjustMedianCount(1); executed 408 times by 2 tests: return measurer->adjustMedianCount(1); Executed by:
| 408 | ||||||||||||
112 | } | - | ||||||||||||
113 | } | - | ||||||||||||
114 | - | |||||||||||||
115 | - | |||||||||||||
116 | QBenchmarkTestMethodData *QBenchmarkTestMethodData::current; | - | ||||||||||||
117 | - | |||||||||||||
118 | QBenchmarkTestMethodData::QBenchmarkTestMethodData() | - | ||||||||||||
119 | :resultAccepted(false), runOnce(false), iterationCount(-1) | - | ||||||||||||
120 | { | - | ||||||||||||
121 | } executed 13256 times by 502 tests: end of block Executed by:
| 13256 | ||||||||||||
122 | - | |||||||||||||
123 | QBenchmarkTestMethodData::~QBenchmarkTestMethodData() | - | ||||||||||||
124 | { | - | ||||||||||||
125 | QBenchmarkTestMethodData::current = 0; | - | ||||||||||||
126 | } executed 13199 times by 502 tests: end of block Executed by:
| 13199 | ||||||||||||
127 | - | |||||||||||||
128 | void QBenchmarkTestMethodData::beginDataRun() | - | ||||||||||||
129 | { | - | ||||||||||||
130 | iterationCount = adjustIterationCount(1); | - | ||||||||||||
131 | } executed 126618 times by 502 tests: end of block Executed by:
| 126618 | ||||||||||||
132 | - | |||||||||||||
133 | void QBenchmarkTestMethodData::endDataRun() | - | ||||||||||||
134 | { | - | ||||||||||||
135 | } | - | ||||||||||||
136 | - | |||||||||||||
137 | int QBenchmarkTestMethodData::adjustIterationCount(int suggestion) | - | ||||||||||||
138 | { | - | ||||||||||||
139 | // Let the -iterations option override the measurer. | - | ||||||||||||
140 | if (QBenchmarkGlobalData::current->iterationCount != -1) {
| 240-126378 | ||||||||||||
141 | iterationCount = QBenchmarkGlobalData::current->iterationCount; | - | ||||||||||||
142 | } else { executed 240 times by 1 test: end of block Executed by:
| 240 | ||||||||||||
143 | iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion); | - | ||||||||||||
144 | } executed 126378 times by 502 tests: end of block Executed by:
| 126378 | ||||||||||||
145 | - | |||||||||||||
146 | return iterationCount; executed 126618 times by 502 tests: return iterationCount; Executed by:
| 126618 | ||||||||||||
147 | } | - | ||||||||||||
148 | - | |||||||||||||
149 | void QBenchmarkTestMethodData::setResult( | - | ||||||||||||
150 | qreal value, QTest::QBenchmarkMetric metric, bool setByMacro) | - | ||||||||||||
151 | { | - | ||||||||||||
152 | bool accepted = false; | - | ||||||||||||
153 | - | |||||||||||||
154 | // Always accept the result if the iteration count has been | - | ||||||||||||
155 | // specified on the command line with -iterations. | - | ||||||||||||
156 | if (QBenchmarkGlobalData::current->iterationCount != -1)
| 138-282 | ||||||||||||
157 | accepted = true; executed 138 times by 1 test: accepted = true; Executed by:
| 138 | ||||||||||||
158 | - | |||||||||||||
159 | else if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
| 0-266 | ||||||||||||
160 | iterationCount = 1; | - | ||||||||||||
161 | accepted = true; | - | ||||||||||||
162 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||
163 | - | |||||||||||||
164 | // Test the result directly without calling the measurer if the minimum time | - | ||||||||||||
165 | // has been specified on the command line with -minimumvalue. | - | ||||||||||||
166 | else if (QBenchmarkGlobalData::current->walltimeMinimum != -1)
| 14-252 | ||||||||||||
167 | accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum); executed 14 times by 1 test: accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum); Executed by:
| 14 | ||||||||||||
168 | else | - | ||||||||||||
169 | accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value); executed 252 times by 1 test: accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value); Executed by:
| 252 | ||||||||||||
170 | - | |||||||||||||
171 | // Accept the result or double the number of iterations. | - | ||||||||||||
172 | if (accepted)
| 12-408 | ||||||||||||
173 | resultAccepted = true; executed 408 times by 2 tests: resultAccepted = true; Executed by:
| 408 | ||||||||||||
174 | else | - | ||||||||||||
175 | iterationCount *= 2; executed 12 times by 1 test: iterationCount *= 2; Executed by:
| 12 | ||||||||||||
176 | - | |||||||||||||
177 | this->result = QBenchmarkResult( | - | ||||||||||||
178 | QBenchmarkGlobalData::current->context, value, iterationCount, metric, setByMacro); | - | ||||||||||||
179 | } executed 420 times by 2 tests: end of block Executed by:
| 420 | ||||||||||||
180 | - | |||||||||||||
181 | /*! | - | ||||||||||||
182 | \class QTest::QBenchmarkIterationController | - | ||||||||||||
183 | \internal | - | ||||||||||||
184 | - | |||||||||||||
185 | The QBenchmarkIterationController class is used by the QBENCHMARK macro to | - | ||||||||||||
186 | drive the benchmarking loop. It is repsonsible for starting and stopping | - | ||||||||||||
187 | the timing measurements as well as calling the result reporting functions. | - | ||||||||||||
188 | */ | - | ||||||||||||
189 | - | |||||||||||||
190 | /*! \internal | - | ||||||||||||
191 | */ | - | ||||||||||||
192 | QTest::QBenchmarkIterationController::QBenchmarkIterationController(RunMode runMode) | - | ||||||||||||
193 | { | - | ||||||||||||
194 | i = 0; | - | ||||||||||||
195 | if (runMode == RunOnce)
| 0-16 | ||||||||||||
196 | QBenchmarkTestMethodData::current->runOnce = true; executed 16 times by 1 test: QBenchmarkTestMethodData::current->runOnce = true; Executed by:
| 16 | ||||||||||||
197 | QTest::beginBenchmarkMeasurement(); | - | ||||||||||||
198 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||
199 | - | |||||||||||||
200 | QTest::QBenchmarkIterationController::QBenchmarkIterationController() | - | ||||||||||||
201 | { | - | ||||||||||||
202 | i = 0; | - | ||||||||||||
203 | QTest::beginBenchmarkMeasurement(); | - | ||||||||||||
204 | } executed 404 times by 1 test: end of block Executed by:
| 404 | ||||||||||||
205 | - | |||||||||||||
206 | /*! \internal | - | ||||||||||||
207 | */ | - | ||||||||||||
208 | QTest::QBenchmarkIterationController::~QBenchmarkIterationController() | - | ||||||||||||
209 | { | - | ||||||||||||
210 | const qreal result = QTest::endBenchmarkMeasurement(); | - | ||||||||||||
211 | QBenchmarkTestMethodData::current->setResult(result, QBenchmarkGlobalData::current->measurer->metricType()); | - | ||||||||||||
212 | } executed 420 times by 2 tests: end of block Executed by:
| 420 | ||||||||||||
213 | - | |||||||||||||
214 | /*! \internal | - | ||||||||||||
215 | */ | - | ||||||||||||
216 | bool QTest::QBenchmarkIterationController::isDone() | - | ||||||||||||
217 | { | - | ||||||||||||
218 | if (QBenchmarkTestMethodData::current->runOnce)
| 32-1026 | ||||||||||||
219 | return i > 0; executed 32 times by 1 test: return i > 0; Executed by:
| 32 | ||||||||||||
220 | return i >= QTest::iterationCount(); executed 1026 times by 1 test: return i >= QTest::iterationCount(); Executed by:
| 1026 | ||||||||||||
221 | } | - | ||||||||||||
222 | - | |||||||||||||
223 | /*! \internal | - | ||||||||||||
224 | */ | - | ||||||||||||
225 | void QTest::QBenchmarkIterationController::next() | - | ||||||||||||
226 | { | - | ||||||||||||
227 | ++i; | - | ||||||||||||
228 | } executed 638 times by 2 tests: end of block Executed by:
| 638 | ||||||||||||
229 | - | |||||||||||||
230 | /*! \internal | - | ||||||||||||
231 | */ | - | ||||||||||||
232 | int QTest::iterationCount() | - | ||||||||||||
233 | { | - | ||||||||||||
234 | return QBenchmarkTestMethodData::current->iterationCount; executed 1026 times by 1 test: return QBenchmarkTestMethodData::current->iterationCount; Executed by:
| 1026 | ||||||||||||
235 | } | - | ||||||||||||
236 | - | |||||||||||||
237 | /*! \internal | - | ||||||||||||
238 | */ | - | ||||||||||||
239 | void QTest::setIterationCountHint(int count) | - | ||||||||||||
240 | { | - | ||||||||||||
241 | QBenchmarkTestMethodData::current->adjustIterationCount(count); | - | ||||||||||||
242 | } never executed: end of block | 0 | ||||||||||||
243 | - | |||||||||||||
244 | /*! \internal | - | ||||||||||||
245 | */ | - | ||||||||||||
246 | void QTest::setIterationCount(int count) | - | ||||||||||||
247 | { | - | ||||||||||||
248 | QBenchmarkTestMethodData::current->iterationCount = count; | - | ||||||||||||
249 | QBenchmarkTestMethodData::current->resultAccepted = true; | - | ||||||||||||
250 | } never executed: end of block | 0 | ||||||||||||
251 | - | |||||||||||||
252 | /*! \internal | - | ||||||||||||
253 | */ | - | ||||||||||||
254 | void QTest::beginBenchmarkMeasurement() | - | ||||||||||||
255 | { | - | ||||||||||||
256 | QBenchmarkGlobalData::current->measurer->start(); | - | ||||||||||||
257 | // the clock is ticking after the line above, don't add code here. | - | ||||||||||||
258 | } executed 420 times by 2 tests: end of block Executed by:
| 420 | ||||||||||||
259 | - | |||||||||||||
260 | /*! \internal | - | ||||||||||||
261 | */ | - | ||||||||||||
262 | quint64 QTest::endBenchmarkMeasurement() | - | ||||||||||||
263 | { | - | ||||||||||||
264 | // the clock is ticking before the line below, don't add code here. | - | ||||||||||||
265 | return QBenchmarkGlobalData::current->measurer->stop(); executed 420 times by 2 tests: return QBenchmarkGlobalData::current->measurer->stop(); Executed by:
| 420 | ||||||||||||
266 | } | - | ||||||||||||
267 | - | |||||||||||||
268 | /*! | - | ||||||||||||
269 | Sets the benchmark result for this test function to \a result. | - | ||||||||||||
270 | - | |||||||||||||
271 | Use this function if you want to report benchmark results without | - | ||||||||||||
272 | using the QBENCHMARK macro. Use \a metric to specify how Qt Test | - | ||||||||||||
273 | should interpret the results. | - | ||||||||||||
274 | - | |||||||||||||
275 | The context for the result will be the test function name and any | - | ||||||||||||
276 | data tag from the _data function. This function can only be called | - | ||||||||||||
277 | once in each test function, subsequent calls will replace the | - | ||||||||||||
278 | earlier reported results. | - | ||||||||||||
279 | - | |||||||||||||
280 | Note that the -iterations command line argument has no effect | - | ||||||||||||
281 | on test functions without the QBENCHMARK macro. | - | ||||||||||||
282 | - | |||||||||||||
283 | \since 4.7 | - | ||||||||||||
284 | */ | - | ||||||||||||
285 | void QTest::setBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric) | - | ||||||||||||
286 | { | - | ||||||||||||
287 | QBenchmarkTestMethodData::current->setResult(result, metric, false); | - | ||||||||||||
288 | } never executed: end of block | 0 | ||||||||||||
289 | - | |||||||||||||
290 | template <typename T> | - | ||||||||||||
291 | typename T::value_type qAverage(const T &container) | - | ||||||||||||
292 | { | - | ||||||||||||
293 | typename T::const_iterator it = container.constBegin(); | - | ||||||||||||
294 | typename T::const_iterator end = container.constEnd(); | - | ||||||||||||
295 | typename T::value_type acc = typename T::value_type(); | - | ||||||||||||
296 | int count = 0; | - | ||||||||||||
297 | while (it != end) {
| 0 | ||||||||||||
298 | acc += *it; | - | ||||||||||||
299 | ++it; | - | ||||||||||||
300 | ++count; | - | ||||||||||||
301 | } never executed: end of block | 0 | ||||||||||||
302 | return acc / count; never executed: return acc / count; | 0 | ||||||||||||
303 | } | - | ||||||||||||
304 | - | |||||||||||||
305 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |