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) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtTest module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include <QtTest/qbenchmark.h> | - | ||||||||||||
35 | #include <QtTest/private/qbenchmark_p.h> | - | ||||||||||||
36 | #include <QtTest/private/qbenchmarkmetric_p.h> | - | ||||||||||||
37 | - | |||||||||||||
38 | #include <QtCore/qprocess.h> | - | ||||||||||||
39 | #include <QtCore/qdir.h> | - | ||||||||||||
40 | #include <QtCore/qset.h> | - | ||||||||||||
41 | #include <QtCore/qdebug.h> | - | ||||||||||||
42 | - | |||||||||||||
43 | QT_BEGIN_NAMESPACE | - | ||||||||||||
44 | - | |||||||||||||
45 | QBenchmarkGlobalData *QBenchmarkGlobalData::current; | - | ||||||||||||
46 | - | |||||||||||||
47 | QBenchmarkGlobalData::QBenchmarkGlobalData() | - | ||||||||||||
48 | : measurer(0) | - | ||||||||||||
49 | , walltimeMinimum(-1) | - | ||||||||||||
50 | , iterationCount(-1) | - | ||||||||||||
51 | , medianIterationCount(-1) | - | ||||||||||||
52 | , createChart(false) | - | ||||||||||||
53 | , verboseOutput(false) | - | ||||||||||||
54 | , minimumTotal(-1) | - | ||||||||||||
55 | , mode_(WallTime) | - | ||||||||||||
56 | { | - | ||||||||||||
57 | setMode(mode_); | - | ||||||||||||
58 | } executed 699 times by 1 test: end of block Executed by:
| 699 | ||||||||||||
59 | - | |||||||||||||
60 | QBenchmarkGlobalData::~QBenchmarkGlobalData() | - | ||||||||||||
61 | { | - | ||||||||||||
62 | delete measurer; | - | ||||||||||||
63 | QBenchmarkGlobalData::current = 0; | - | ||||||||||||
64 | } executed 1167 times by 541 tests: end of block Executed by:
| 1167 | ||||||||||||
65 | - | |||||||||||||
66 | void QBenchmarkGlobalData::setMode(Mode mode) | - | ||||||||||||
67 | { | - | ||||||||||||
68 | mode_ = mode; | - | ||||||||||||
69 | - | |||||||||||||
70 | if (measurer)
| 69-699 | ||||||||||||
71 | delete measurer; executed 69 times by 1 test: delete measurer; Executed by:
| 69 | ||||||||||||
72 | measurer = createMeasurer(); | - | ||||||||||||
73 | } executed 768 times by 1 test: end of block Executed by:
| 768 | ||||||||||||
74 | - | |||||||||||||
75 | QBenchmarkMeasurerBase * QBenchmarkGlobalData::createMeasurer() | - | ||||||||||||
76 | { | - | ||||||||||||
77 | QBenchmarkMeasurerBase *measurer = 0; | - | ||||||||||||
78 | if (0) { dead code: { } | - | ||||||||||||
79 | #ifdef QTESTLIB_USE_VALGRIND dead code: { } | - | ||||||||||||
80 | } else if (mode_ == CallgrindChildProcess || mode_ == CallgrindParentProcess) { dead code: { }
| - | ||||||||||||
81 | measurer = new QBenchmarkCallgrindMeasurer; | - | ||||||||||||
82 | #endif | - | ||||||||||||
83 | #ifdef QTESTLIB_USE_PERF_EVENTS | - | ||||||||||||
84 | } else if (mode_ == PerfCounter) { never executed: end of block
| 0-768 | ||||||||||||
85 | measurer = new QBenchmarkPerfEventsMeasurer; | - | ||||||||||||
86 | #endif | - | ||||||||||||
87 | #ifdef HAVE_TICK_COUNTER | - | ||||||||||||
88 | } else if (mode_ == TickCounter) { never executed: end of block
| 0-768 | ||||||||||||
89 | measurer = new QBenchmarkTickMeasurer; | - | ||||||||||||
90 | #endif | - | ||||||||||||
91 | } else if (mode_ == EventCounter) { never executed: end of block
| 0-699 | ||||||||||||
92 | measurer = new QBenchmarkEvent; | - | ||||||||||||
93 | } else { executed 69 times by 1 test: end of block Executed by:
| 69 | ||||||||||||
94 | measurer = new QBenchmarkTimeMeasurer; | - | ||||||||||||
95 | } executed 699 times by 1 test: end of block Executed by:
| 699 | ||||||||||||
96 | measurer->init(); | - | ||||||||||||
97 | return measurer; executed 768 times by 1 test: return measurer; Executed by:
| 768 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | int QBenchmarkGlobalData::adjustMedianIterationCount() | - | ||||||||||||
101 | { | - | ||||||||||||
102 | if (medianIterationCount != -1) {
| 0-376 | ||||||||||||
103 | return medianIterationCount; never executed: return medianIterationCount; | 0 | ||||||||||||
104 | } else { | - | ||||||||||||
105 | return measurer->adjustMedianCount(1); executed 376 times by 2 tests: return measurer->adjustMedianCount(1); Executed by:
| 376 | ||||||||||||
106 | } | - | ||||||||||||
107 | } | - | ||||||||||||
108 | - | |||||||||||||
109 | - | |||||||||||||
110 | QBenchmarkTestMethodData *QBenchmarkTestMethodData::current; | - | ||||||||||||
111 | - | |||||||||||||
112 | QBenchmarkTestMethodData::QBenchmarkTestMethodData() | - | ||||||||||||
113 | :resultAccepted(false), runOnce(false), iterationCount(-1) | - | ||||||||||||
114 | { | - | ||||||||||||
115 | } executed 13112 times by 535 tests: end of block Executed by:
| 13112 | ||||||||||||
116 | - | |||||||||||||
117 | QBenchmarkTestMethodData::~QBenchmarkTestMethodData() | - | ||||||||||||
118 | { | - | ||||||||||||
119 | QBenchmarkTestMethodData::current = 0; | - | ||||||||||||
120 | } executed 13063 times by 535 tests: end of block Executed by:
| 13063 | ||||||||||||
121 | - | |||||||||||||
122 | void QBenchmarkTestMethodData::beginDataRun() | - | ||||||||||||
123 | { | - | ||||||||||||
124 | iterationCount = adjustIterationCount(1); | - | ||||||||||||
125 | } executed 130489 times by 535 tests: end of block Executed by:
| 130489 | ||||||||||||
126 | - | |||||||||||||
127 | void QBenchmarkTestMethodData::endDataRun() | - | ||||||||||||
128 | { | - | ||||||||||||
129 | } | - | ||||||||||||
130 | - | |||||||||||||
131 | int QBenchmarkTestMethodData::adjustIterationCount(int suggestion) | - | ||||||||||||
132 | { | - | ||||||||||||
133 | // Let the -iterations option override the measurer. | - | ||||||||||||
134 | if (QBenchmarkGlobalData::current->iterationCount != -1) {
| 184-130305 | ||||||||||||
135 | iterationCount = QBenchmarkGlobalData::current->iterationCount; | - | ||||||||||||
136 | } else { executed 184 times by 1 test: end of block Executed by:
| 184 | ||||||||||||
137 | iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion); | - | ||||||||||||
138 | } executed 130305 times by 535 tests: end of block Executed by:
| 130305 | ||||||||||||
139 | - | |||||||||||||
140 | return iterationCount; executed 130489 times by 535 tests: return iterationCount; Executed by:
| 130489 | ||||||||||||
141 | } | - | ||||||||||||
142 | - | |||||||||||||
143 | void QBenchmarkTestMethodData::setResult( | - | ||||||||||||
144 | qreal value, QTest::QBenchmarkMetric metric, bool setByMacro) | - | ||||||||||||
145 | { | - | ||||||||||||
146 | bool accepted = false; | - | ||||||||||||
147 | - | |||||||||||||
148 | // Always accept the result if the iteration count has been | - | ||||||||||||
149 | // specified on the command line with -iterations. | - | ||||||||||||
150 | if (QBenchmarkGlobalData::current->iterationCount != -1)
| 106-282 | ||||||||||||
151 | accepted = true; executed 106 times by 1 test: accepted = true; Executed by:
| 106 | ||||||||||||
152 | - | |||||||||||||
153 | else if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
| 0-266 | ||||||||||||
154 | iterationCount = 1; | - | ||||||||||||
155 | accepted = true; | - | ||||||||||||
156 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||
157 | - | |||||||||||||
158 | // Test the result directly without calling the measurer if the minimum time | - | ||||||||||||
159 | // has been specified on the command line with -minimumvalue. | - | ||||||||||||
160 | else if (QBenchmarkGlobalData::current->walltimeMinimum != -1)
| 14-252 | ||||||||||||
161 | accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum); executed 14 times by 1 test: accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum); Executed by:
| 14 | ||||||||||||
162 | else | - | ||||||||||||
163 | accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value); executed 252 times by 1 test: accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value); Executed by:
| 252 | ||||||||||||
164 | - | |||||||||||||
165 | // Accept the result or double the number of iterations. | - | ||||||||||||
166 | if (accepted)
| 12-376 | ||||||||||||
167 | resultAccepted = true; executed 376 times by 2 tests: resultAccepted = true; Executed by:
| 376 | ||||||||||||
168 | else | - | ||||||||||||
169 | iterationCount *= 2; executed 12 times by 1 test: iterationCount *= 2; Executed by:
| 12 | ||||||||||||
170 | - | |||||||||||||
171 | this->result = QBenchmarkResult( | - | ||||||||||||
172 | QBenchmarkGlobalData::current->context, value, iterationCount, metric, setByMacro); | - | ||||||||||||
173 | } executed 388 times by 2 tests: end of block Executed by:
| 388 | ||||||||||||
174 | - | |||||||||||||
175 | /*! | - | ||||||||||||
176 | \class QTest::QBenchmarkIterationController | - | ||||||||||||
177 | \internal | - | ||||||||||||
178 | - | |||||||||||||
179 | The QBenchmarkIterationController class is used by the QBENCHMARK macro to | - | ||||||||||||
180 | drive the benchmarking loop. It is repsonsible for starting and stopping | - | ||||||||||||
181 | the timing measurements as well as calling the result reporting functions. | - | ||||||||||||
182 | */ | - | ||||||||||||
183 | - | |||||||||||||
184 | /*! \internal | - | ||||||||||||
185 | */ | - | ||||||||||||
186 | QTest::QBenchmarkIterationController::QBenchmarkIterationController(RunMode runMode) | - | ||||||||||||
187 | { | - | ||||||||||||
188 | i = 0; | - | ||||||||||||
189 | if (runMode == RunOnce)
| 0-16 | ||||||||||||
190 | QBenchmarkTestMethodData::current->runOnce = true; executed 16 times by 1 test: QBenchmarkTestMethodData::current->runOnce = true; Executed by:
| 16 | ||||||||||||
191 | QTest::beginBenchmarkMeasurement(); | - | ||||||||||||
192 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||
193 | - | |||||||||||||
194 | QTest::QBenchmarkIterationController::QBenchmarkIterationController() | - | ||||||||||||
195 | { | - | ||||||||||||
196 | i = 0; | - | ||||||||||||
197 | QTest::beginBenchmarkMeasurement(); | - | ||||||||||||
198 | } executed 372 times by 1 test: end of block Executed by:
| 372 | ||||||||||||
199 | - | |||||||||||||
200 | /*! \internal | - | ||||||||||||
201 | */ | - | ||||||||||||
202 | QTest::QBenchmarkIterationController::~QBenchmarkIterationController() | - | ||||||||||||
203 | { | - | ||||||||||||
204 | const qreal result = QTest::endBenchmarkMeasurement(); | - | ||||||||||||
205 | QBenchmarkTestMethodData::current->setResult(result, QBenchmarkGlobalData::current->measurer->metricType()); | - | ||||||||||||
206 | } executed 388 times by 2 tests: end of block Executed by:
| 388 | ||||||||||||
207 | - | |||||||||||||
208 | /*! \internal | - | ||||||||||||
209 | */ | - | ||||||||||||
210 | bool QTest::QBenchmarkIterationController::isDone() | - | ||||||||||||
211 | { | - | ||||||||||||
212 | if (QBenchmarkTestMethodData::current->runOnce)
| 32-962 | ||||||||||||
213 | return i > 0; executed 32 times by 1 test: return i > 0; Executed by:
| 32 | ||||||||||||
214 | return i >= QTest::iterationCount(); executed 962 times by 1 test: return i >= QTest::iterationCount(); Executed by:
| 962 | ||||||||||||
215 | } | - | ||||||||||||
216 | - | |||||||||||||
217 | /*! \internal | - | ||||||||||||
218 | */ | - | ||||||||||||
219 | void QTest::QBenchmarkIterationController::next() | - | ||||||||||||
220 | { | - | ||||||||||||
221 | ++i; | - | ||||||||||||
222 | } executed 606 times by 2 tests: end of block Executed by:
| 606 | ||||||||||||
223 | - | |||||||||||||
224 | /*! \internal | - | ||||||||||||
225 | */ | - | ||||||||||||
226 | int QTest::iterationCount() | - | ||||||||||||
227 | { | - | ||||||||||||
228 | return QBenchmarkTestMethodData::current->iterationCount; executed 962 times by 1 test: return QBenchmarkTestMethodData::current->iterationCount; Executed by:
| 962 | ||||||||||||
229 | } | - | ||||||||||||
230 | - | |||||||||||||
231 | /*! \internal | - | ||||||||||||
232 | */ | - | ||||||||||||
233 | void QTest::setIterationCountHint(int count) | - | ||||||||||||
234 | { | - | ||||||||||||
235 | QBenchmarkTestMethodData::current->adjustIterationCount(count); | - | ||||||||||||
236 | } never executed: end of block | 0 | ||||||||||||
237 | - | |||||||||||||
238 | /*! \internal | - | ||||||||||||
239 | */ | - | ||||||||||||
240 | void QTest::setIterationCount(int count) | - | ||||||||||||
241 | { | - | ||||||||||||
242 | QBenchmarkTestMethodData::current->iterationCount = count; | - | ||||||||||||
243 | QBenchmarkTestMethodData::current->resultAccepted = true; | - | ||||||||||||
244 | } never executed: end of block | 0 | ||||||||||||
245 | - | |||||||||||||
246 | /*! \internal | - | ||||||||||||
247 | */ | - | ||||||||||||
248 | void QTest::beginBenchmarkMeasurement() | - | ||||||||||||
249 | { | - | ||||||||||||
250 | QBenchmarkGlobalData::current->measurer->start(); | - | ||||||||||||
251 | // the clock is ticking after the line above, don't add code here. | - | ||||||||||||
252 | } executed 388 times by 2 tests: end of block Executed by:
| 388 | ||||||||||||
253 | - | |||||||||||||
254 | /*! \internal | - | ||||||||||||
255 | */ | - | ||||||||||||
256 | quint64 QTest::endBenchmarkMeasurement() | - | ||||||||||||
257 | { | - | ||||||||||||
258 | // the clock is ticking before the line below, don't add code here. | - | ||||||||||||
259 | return QBenchmarkGlobalData::current->measurer->stop(); executed 388 times by 2 tests: return QBenchmarkGlobalData::current->measurer->stop(); Executed by:
| 388 | ||||||||||||
260 | } | - | ||||||||||||
261 | - | |||||||||||||
262 | /*! | - | ||||||||||||
263 | Sets the benchmark result for this test function to \a result. | - | ||||||||||||
264 | - | |||||||||||||
265 | Use this function if you want to report benchmark results without | - | ||||||||||||
266 | using the QBENCHMARK macro. Use \a metric to specify how Qt Test | - | ||||||||||||
267 | should interpret the results. | - | ||||||||||||
268 | - | |||||||||||||
269 | The context for the result will be the test function name and any | - | ||||||||||||
270 | data tag from the _data function. This function can only be called | - | ||||||||||||
271 | once in each test function, subsequent calls will replace the | - | ||||||||||||
272 | earlier reported results. | - | ||||||||||||
273 | - | |||||||||||||
274 | Note that the -iterations command line argument has no effect | - | ||||||||||||
275 | on test functions without the QBENCHMARK macro. | - | ||||||||||||
276 | - | |||||||||||||
277 | \since 4.7 | - | ||||||||||||
278 | */ | - | ||||||||||||
279 | void QTest::setBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric) | - | ||||||||||||
280 | { | - | ||||||||||||
281 | QBenchmarkTestMethodData::current->setResult(result, metric, false); | - | ||||||||||||
282 | } never executed: end of block | 0 | ||||||||||||
283 | - | |||||||||||||
284 | template <typename T> | - | ||||||||||||
285 | typename T::value_type qAverage(const T &container) | - | ||||||||||||
286 | { | - | ||||||||||||
287 | typename T::const_iterator it = container.constBegin(); | - | ||||||||||||
288 | typename T::const_iterator end = container.constEnd(); | - | ||||||||||||
289 | typename T::value_type acc = typename T::value_type(); | - | ||||||||||||
290 | int count = 0; | - | ||||||||||||
291 | while (it != end) {
| 0 | ||||||||||||
292 | acc += *it; | - | ||||||||||||
293 | ++it; | - | ||||||||||||
294 | ++count; | - | ||||||||||||
295 | } never executed: end of block | 0 | ||||||||||||
296 | return acc / count; never executed: return acc / count; | 0 | ||||||||||||
297 | } | - | ||||||||||||
298 | - | |||||||||||||
299 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |