qbenchmark.cpp

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

Generated by Squish Coco Non-Commercial