qbenchmark.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/testlib/qbenchmark.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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-
43QT_BEGIN_NAMESPACE-
44-
45QBenchmarkGlobalData *QBenchmarkGlobalData::current;-
46-
47QBenchmarkGlobalData::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:
  • tst_selftests - unknown status
699
59-
60QBenchmarkGlobalData::~QBenchmarkGlobalData()-
61{-
62 delete measurer;-
63 QBenchmarkGlobalData::current = 0;-
64}
executed 1167 times by 541 tests: end of block
Executed by:
  • tst_collections - unknown status
  • tst_compiler - unknown status
  • tst_gestures - unknown status
  • tst_lancelot - unknown status
  • tst_languagechange - unknown status
  • tst_largefile - unknown status
  • tst_modeltest - unknown status
  • tst_networkselftest - unknown status
  • tst_platformsocketengine - unknown status
  • tst_q_func_info - unknown status
  • tst_qabstractanimation - unknown status
  • tst_qabstractbutton - unknown status
  • tst_qabstractfileengine - unknown status
  • tst_qabstractitemmodel - unknown status
  • tst_qabstractitemview - unknown status
  • tst_qabstractnetworkcache - unknown status
  • tst_qabstractprintdialog - unknown status
  • tst_qabstractproxymodel - unknown status
  • tst_qabstractscrollarea - unknown status
  • tst_qabstractslider - unknown status
  • tst_qabstractsocket - unknown status
  • tst_qabstractspinbox - unknown status
  • tst_qabstracttextdocumentlayout - unknown status
  • tst_qaccessibility - unknown status
  • tst_qaction - unknown status
  • ...
1167
65-
66void QBenchmarkGlobalData::setMode(Mode mode)-
67{-
68 mode_ = mode;-
69-
70 if (measurer)
measurerDescription
TRUEevaluated 69 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
FALSEevaluated 699 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
69-699
71 delete measurer;
executed 69 times by 1 test: delete measurer;
Executed by:
  • tst_selftests - unknown status
69
72 measurer = createMeasurer();-
73}
executed 768 times by 1 test: end of block
Executed by:
  • tst_selftests - unknown status
768
74-
75QBenchmarkMeasurerBase * 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: { }
mode_ == CallgrindChildProcessDescription
TRUEnever evaluated
FALSEevaluated 768 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
mode_ == Callg...dParentProcessDescription
TRUEnever evaluated
FALSEevaluated 768 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
-
81 measurer = new QBenchmarkCallgrindMeasurer;-
82#endif-
83#ifdef QTESTLIB_USE_PERF_EVENTS-
84 } else if (mode_ == PerfCounter) {
never executed: end of block
mode_ == PerfCounterDescription
TRUEnever evaluated
FALSEevaluated 768 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
0-768
85 measurer = new QBenchmarkPerfEventsMeasurer;-
86#endif-
87#ifdef HAVE_TICK_COUNTER-
88 } else if (mode_ == TickCounter) {
never executed: end of block
mode_ == TickCounterDescription
TRUEnever evaluated
FALSEevaluated 768 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
0-768
89 measurer = new QBenchmarkTickMeasurer;-
90#endif-
91 } else if (mode_ == EventCounter) {
never executed: end of block
mode_ == EventCounterDescription
TRUEevaluated 69 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
FALSEevaluated 699 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
0-699
92 measurer = new QBenchmarkEvent;-
93 } else {
executed 69 times by 1 test: end of block
Executed by:
  • tst_selftests - unknown status
69
94 measurer = new QBenchmarkTimeMeasurer;-
95 }
executed 699 times by 1 test: end of block
Executed by:
  • tst_selftests - unknown status
699
96 measurer->init();-
97 return measurer;
executed 768 times by 1 test: return measurer;
Executed by:
  • tst_selftests - unknown status
768
98}-
99-
100int QBenchmarkGlobalData::adjustMedianIterationCount()-
101{-
102 if (medianIterationCount != -1) {
medianIterationCount != -1Description
TRUEnever evaluated
FALSEevaluated 376 times by 2 tests
Evaluated by:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
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:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
376
106 }-
107}-
108-
109-
110QBenchmarkTestMethodData *QBenchmarkTestMethodData::current;-
111-
112QBenchmarkTestMethodData::QBenchmarkTestMethodData()-
113:resultAccepted(false), runOnce(false), iterationCount(-1)-
114{-
115}
executed 13112 times by 535 tests: end of block
Executed by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
13112
116-
117QBenchmarkTestMethodData::~QBenchmarkTestMethodData()-
118{-
119 QBenchmarkTestMethodData::current = 0;-
120}
executed 13063 times by 535 tests: end of block
Executed by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
13063
121-
122void QBenchmarkTestMethodData::beginDataRun()-
123{-
124 iterationCount = adjustIterationCount(1);-
125}
executed 130489 times by 535 tests: end of block
Executed by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
130489
126-
127void QBenchmarkTestMethodData::endDataRun()-
128{-
129}-
130-
131int QBenchmarkTestMethodData::adjustIterationCount(int suggestion)-
132{-
133 // Let the -iterations option override the measurer.-
134 if (QBenchmarkGlobalData::current->iterationCount != -1) {
QBenchmarkGlob...ionCount != -1Description
TRUEevaluated 184 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
FALSEevaluated 130305 times by 535 tests
Evaluated by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
184-130305
135 iterationCount = QBenchmarkGlobalData::current->iterationCount;-
136 } else {
executed 184 times by 1 test: end of block
Executed by:
  • tst_selftests - unknown status
184
137 iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion);-
138 }
executed 130305 times by 535 tests: end of block
Executed by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
130305
139-
140 return iterationCount;
executed 130489 times by 535 tests: return iterationCount;
Executed by:
  • tst_Collections
  • tst_Compiler
  • tst_Gestures
  • tst_LargeFile
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_PlatformSocketEngine
  • tst_QAbstractAnimation
  • tst_QAbstractButton
  • tst_QAbstractFileEngine
  • tst_QAbstractItemModel
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractProxyModel
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSocket
  • tst_QAbstractSpinBox
  • tst_QAbstractTextDocumentLayout
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QAlgorithms
  • tst_QAnimationGroup
  • ...
130489
141}-
142-
143void 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)
QBenchmarkGlob...ionCount != -1Description
TRUEevaluated 106 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
FALSEevaluated 282 times by 2 tests
Evaluated by:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
106-282
151 accepted = true;
executed 106 times by 1 test: accepted = true;
Executed by:
  • tst_selftests - unknown status
106
152-
153 else if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
QBenchmarkTest...rrent->runOnceDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • tst_QByteDataBuffer
FALSEevaluated 266 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
!setByMacroDescription
TRUEnever evaluated
FALSEevaluated 266 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
0-266
154 iterationCount = 1;-
155 accepted = true;-
156 }
executed 16 times by 1 test: end of block
Executed by:
  • tst_QByteDataBuffer
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)
QBenchmarkGlob...eMinimum != -1Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
FALSEevaluated 252 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
14-252
161 accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum);
executed 14 times by 1 test: accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum);
Executed by:
  • tst_selftests - unknown status
14
162 else-
163 accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value);
executed 252 times by 1 test: accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value);
Executed by:
  • tst_selftests - unknown status
252
164-
165 // Accept the result or double the number of iterations.-
166 if (accepted)
acceptedDescription
TRUEevaluated 376 times by 2 tests
Evaluated by:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
FALSEevaluated 12 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
12-376
167 resultAccepted = true;
executed 376 times by 2 tests: resultAccepted = true;
Executed by:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
376
168 else-
169 iterationCount *= 2;
executed 12 times by 1 test: iterationCount *= 2;
Executed by:
  • tst_selftests - unknown status
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:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
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*/-
186QTest::QBenchmarkIterationController::QBenchmarkIterationController(RunMode runMode)-
187{-
188 i = 0;-
189 if (runMode == RunOnce)
runMode == RunOnceDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • tst_QByteDataBuffer
FALSEnever evaluated
0-16
190 QBenchmarkTestMethodData::current->runOnce = true;
executed 16 times by 1 test: QBenchmarkTestMethodData::current->runOnce = true;
Executed by:
  • tst_QByteDataBuffer
16
191 QTest::beginBenchmarkMeasurement();-
192}
executed 16 times by 1 test: end of block
Executed by:
  • tst_QByteDataBuffer
16
193-
194QTest::QBenchmarkIterationController::QBenchmarkIterationController()-
195{-
196 i = 0;-
197 QTest::beginBenchmarkMeasurement();-
198}
executed 372 times by 1 test: end of block
Executed by:
  • tst_selftests - unknown status
372
199-
200/*! \internal-
201*/-
202QTest::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:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
388
207-
208/*! \internal-
209*/-
210bool QTest::QBenchmarkIterationController::isDone()-
211{-
212 if (QBenchmarkTestMethodData::current->runOnce)
QBenchmarkTest...rrent->runOnceDescription
TRUEevaluated 32 times by 1 test
Evaluated by:
  • tst_QByteDataBuffer
FALSEevaluated 962 times by 1 test
Evaluated by:
  • tst_selftests - unknown status
32-962
213 return i > 0;
executed 32 times by 1 test: return i > 0;
Executed by:
  • tst_QByteDataBuffer
32
214 return i >= QTest::iterationCount();
executed 962 times by 1 test: return i >= QTest::iterationCount();
Executed by:
  • tst_selftests - unknown status
962
215}-
216-
217/*! \internal-
218*/-
219void QTest::QBenchmarkIterationController::next()-
220{-
221 ++i;-
222}
executed 606 times by 2 tests: end of block
Executed by:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
606
223-
224/*! \internal-
225*/-
226int QTest::iterationCount()-
227{-
228 return QBenchmarkTestMethodData::current->iterationCount;
executed 962 times by 1 test: return QBenchmarkTestMethodData::current->iterationCount;
Executed by:
  • tst_selftests - unknown status
962
229}-
230-
231/*! \internal-
232*/-
233void QTest::setIterationCountHint(int count)-
234{-
235 QBenchmarkTestMethodData::current->adjustIterationCount(count);-
236}
never executed: end of block
0
237-
238/*! \internal-
239*/-
240void 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*/-
248void 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:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
388
253-
254/*! \internal-
255*/-
256quint64 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:
  • tst_QByteDataBuffer
  • tst_selftests - unknown status
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*/-
279void QTest::setBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric)-
280{-
281 QBenchmarkTestMethodData::current->setResult(result, metric, false);-
282}
never executed: end of block
0
283-
284template <typename T>-
285typename 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) {
it != endDescription
TRUEnever evaluated
FALSEnever evaluated
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-
299QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9