1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtTest module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtTest/qbenchmark.h"
43 #include "QtTest/private/qbenchmark_p.h"
44 #include "QtTest/private/qbenchmarkmetric_p.h"
47 #include <QtGui/qguiapplication.h>
50 #include <QtCore/qprocess.h>
51 #include <QtCore/qdir.h>
52 #include <QtCore/qset.h>
53 #include <QtCore/qdebug.h>
57 QBenchmarkGlobalData *QBenchmarkGlobalData::current;
59 QBenchmarkGlobalData::QBenchmarkGlobalData()
63 , medianIterationCount(-1)
65 , verboseOutput(false)
71 QBenchmarkGlobalData::~QBenchmarkGlobalData()
74 QBenchmarkGlobalData::current = 0;
77 void QBenchmarkGlobalData::setMode(Mode mode)
83 measurer = createMeasurer();
86 QBenchmarkMeasurerBase * QBenchmarkGlobalData::createMeasurer()
88 QBenchmarkMeasurerBase *measurer = 0;
90 #ifdef QTESTLIB_USE_VALGRIND
91 } else if (mode_ == CallgrindChildProcess || mode_ == CallgrindParentProcess) {
92 measurer = new QBenchmarkCallgrindMeasurer;
94 #ifdef HAVE_TICK_COUNTER
95 } else if (mode_ == TickCounter) {
96 measurer = new QBenchmarkTickMeasurer;
98 } else if (mode_ == EventCounter) {
99 measurer = new QBenchmarkEvent;
101 measurer = new QBenchmarkTimeMeasurer;
107 int QBenchmarkGlobalData::adjustMedianIterationCount()
109 if (medianIterationCount != -1) {
110 return medianIterationCount;
112 return measurer->adjustMedianCount(1);
117 QBenchmarkTestMethodData *QBenchmarkTestMethodData::current;
119 QBenchmarkTestMethodData::QBenchmarkTestMethodData()
120 :resultAccepted(false), runOnce(false), iterationCount(-1)
125 QBenchmarkTestMethodData::~QBenchmarkTestMethodData()
127 QBenchmarkTestMethodData::current = 0;
130 void QBenchmarkTestMethodData::beginDataRun()
132 iterationCount = adjustIterationCount(1);
135 void QBenchmarkTestMethodData::endDataRun()
140 int QBenchmarkTestMethodData::adjustIterationCount(int suggestion)
142 // Let the -iterations option override the measurer.
143 if (QBenchmarkGlobalData::current->iterationCount != -1) {
144 iterationCount = QBenchmarkGlobalData::current->iterationCount;
146 iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion);
149 return iterationCount;
152 void QBenchmarkTestMethodData::setResult(
153 qreal value, QTest::QBenchmarkMetric metric, bool setByMacro)
155 bool accepted = false;
157 // Always accept the result if the iteration count has been
158 // specified on the command line with -iterations.
159 if (QBenchmarkGlobalData::current->iterationCount != -1)
162 else if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
167 // Test the result directly without calling the measurer if the minimum time
168 // has been specified on the command line with -minimumvalue.
169 else if (QBenchmarkGlobalData::current->walltimeMinimum != -1)
170 accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum);
172 accepted = QBenchmarkGlobalData::current->measurer->isMeasurementAccepted(value);
174 // Accept the result or double the number of iterations.
176 resultAccepted = true;
180 this->result = QBenchmarkResult(
181 QBenchmarkGlobalData::current->context, value, iterationCount, metric, setByMacro);
185 \class QTest::QBenchmarkIterationController
188 The QBenchmarkIterationController class is used by the QBENCHMARK macro to
189 drive the benchmarking loop. It is repsonsible for starting and stopping
190 the timing measurements as well as calling the result reporting functions.
195 QTest::QBenchmarkIterationController::QBenchmarkIterationController(RunMode runMode)
198 if (runMode == RunOnce)
199 QBenchmarkTestMethodData::current->runOnce = true;
200 QTest::beginBenchmarkMeasurement();
203 QTest::QBenchmarkIterationController::QBenchmarkIterationController()
206 QTest::beginBenchmarkMeasurement();
211 QTest::QBenchmarkIterationController::~QBenchmarkIterationController()
213 const qreal result = QTest::endBenchmarkMeasurement();
214 QBenchmarkTestMethodData::current->setResult(result, QBenchmarkGlobalData::current->measurer->metricType());
219 bool QTest::QBenchmarkIterationController::isDone()
221 if (QBenchmarkTestMethodData::current->runOnce)
223 return i >= QTest::iterationCount();
228 void QTest::QBenchmarkIterationController::next()
235 int QTest::iterationCount()
237 return QBenchmarkTestMethodData::current->iterationCount;
242 void QTest::setIterationCountHint(int count)
244 QBenchmarkTestMethodData::current->adjustIterationCount(count);
249 void QTest::setIterationCount(int count)
251 QBenchmarkTestMethodData::current->iterationCount = count;
252 QBenchmarkTestMethodData::current->resultAccepted = true;
257 void QTest::beginBenchmarkMeasurement()
259 QBenchmarkGlobalData::current->measurer->start();
260 // the clock is ticking after the line above, don't add code here.
265 quint64 QTest::endBenchmarkMeasurement()
267 // the clock is ticking before the line below, don't add code here.
268 return QBenchmarkGlobalData::current->measurer->stop();
272 Sets the benchmark result for this test function to \a result.
274 Use this function if you want to report benchmark results without
275 using the QBENCHMARK macro. Use \a metric to specify how QTestLib
276 should interpret the results.
278 The context for the result will be the test function name and any
279 data tag from the _data function. This function can only be called
280 once in each test function, subsequent calls will replace the
281 earlier reported results.
283 Note that the -iterations command line argument has no effect
284 on test functions without the QBENCHMARK macro.
288 void QTest::setBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric)
290 QBenchmarkTestMethodData::current->setResult(result, metric, false);
293 template <typename T>
294 Q_TYPENAME T::value_type qAverage(const T &container)
296 Q_TYPENAME T::const_iterator it = container.constBegin();
297 Q_TYPENAME T::const_iterator end = container.constEnd();
298 Q_TYPENAME T::value_type acc = Q_TYPENAME T::value_type();