1 /****************************************************************************
3 ** Copyright (C) 2009 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "QtTest/qbenchmark.h"
43 #include "QtTest/private/qbenchmark_p.h"
46 #include <QtGui/qapplication.h>
49 #include <QtCore/qprocess.h>
50 #include <QtCore/qdir.h>
51 #include <QtCore/qset.h>
52 #include <QtCore/qdebug.h>
56 QBenchmarkGlobalData
*QBenchmarkGlobalData::current
;
58 QBenchmarkGlobalData::QBenchmarkGlobalData()
62 , medianIterationCount(-1)
64 , verboseOutput(false)
70 QBenchmarkGlobalData::~QBenchmarkGlobalData()
73 QBenchmarkGlobalData::current
= 0;
76 void QBenchmarkGlobalData::setMode(Mode mode
)
82 measurer
= createMeasurer();
85 QBenchmarkMeasurerBase
* QBenchmarkGlobalData::createMeasurer()
87 QBenchmarkMeasurerBase
*measurer
= 0;
89 #ifdef QTESTLIB_USE_VALGRIND
90 } else if (mode_
== CallgrindChildProcess
|| mode_
== CallgrindParentProcess
) {
91 measurer
= new QBenchmarkCallgrindMeasurer
;
93 #ifdef HAVE_TICK_COUNTER
94 } else if (mode_
== TickCounter
) {
95 measurer
= new QBenchmarkTickMeasurer
;
97 } else if (mode_
== EventCounter
) {
98 measurer
= new QBenchmarkEvent
;
100 measurer
= new QBenchmarkTimeMeasurer
;
106 int QBenchmarkGlobalData::adjustMedianIterationCount()
108 if (medianIterationCount
!= -1) {
109 return medianIterationCount
;
111 return measurer
->adjustMedianCount(1);
116 QBenchmarkTestMethodData
*QBenchmarkTestMethodData::current
;
118 QBenchmarkTestMethodData::QBenchmarkTestMethodData()
119 :resultAccepted(false), runOnce(false), iterationCount(-1)
124 QBenchmarkTestMethodData::~QBenchmarkTestMethodData()
126 QBenchmarkTestMethodData::current
= 0;
129 void QBenchmarkTestMethodData::beginDataRun()
131 iterationCount
= adjustIterationCount(1);
134 void QBenchmarkTestMethodData::endDataRun()
139 int QBenchmarkTestMethodData::adjustIterationCount(int suggestion
)
141 // Let the -iteration-count option override the measurer.
142 if (QBenchmarkGlobalData::current
->iterationCount
!= -1) {
143 iterationCount
= QBenchmarkGlobalData::current
->iterationCount
;
145 iterationCount
= QBenchmarkGlobalData::current
->measurer
->adjustIterationCount(suggestion
);
148 return iterationCount
;
151 void QBenchmarkTestMethodData::setResult(qint64 value
)
153 bool accepted
= false;
155 // Always accept the result if the iteration count has been
156 // specified on the command line with -iteartion-count.
157 if (QBenchmarkGlobalData::current
->iterationCount
!= -1)
160 if (QBenchmarkTestMethodData::current
->runOnce
) {
165 // Test the result directly without calling the measurer if the minimum time
166 // has been specifed on the command line with -minimumvalue.
167 else if (QBenchmarkGlobalData::current
->walltimeMinimum
!= -1)
168 accepted
= (value
> QBenchmarkGlobalData::current
->walltimeMinimum
);
170 accepted
= QBenchmarkGlobalData::current
->measurer
->isMeasurementAccepted(value
);
172 // Accept the result or double the number of iterations.
174 resultAccepted
= true;
179 QBenchmarkResult(QBenchmarkGlobalData::current
->context
, value
, iterationCount
);
183 \class QTest::QBenchmarkIterationController
186 The QBenchmarkIterationController class is used by the QBENCHMARK macro to
187 drive the benchmarking loop. It is repsonsible for starting and stopping
188 the timing measurements as well as calling the result reporting functions.
193 QTest::QBenchmarkIterationController::QBenchmarkIterationController(RunMode runMode
)
196 if (runMode
== RunOnce
)
197 QBenchmarkTestMethodData::current
->runOnce
= true;
198 QTest::beginBenchmarkMeasurement();
201 QTest::QBenchmarkIterationController::QBenchmarkIterationController()
204 QTest::beginBenchmarkMeasurement();
209 QTest::QBenchmarkIterationController::~QBenchmarkIterationController()
211 QBenchmarkTestMethodData::current
->setResult(QTest::endBenchmarkMeasurement());
216 bool QTest::QBenchmarkIterationController::isDone()
218 if (QBenchmarkTestMethodData::current
->runOnce
)
220 return i
>= QTest::iterationCount();
225 void QTest::QBenchmarkIterationController::next()
232 int QTest::iterationCount()
234 return QBenchmarkTestMethodData::current
->iterationCount
;
239 void QTest::setIterationCountHint(int count
)
241 QBenchmarkTestMethodData::current
->adjustIterationCount(count
);
246 void QTest::setIterationCount(int count
)
248 QBenchmarkTestMethodData::current
->iterationCount
= count
;
249 QBenchmarkTestMethodData::current
->resultAccepted
= true;
254 void QTest::beginBenchmarkMeasurement()
256 QBenchmarkGlobalData::current
->measurer
->start();
257 // the clock is ticking after the line above, don't add code here.
262 qint64
QTest::endBenchmarkMeasurement()
264 // the clock is ticking before the line below, don't add code here.
265 return QBenchmarkGlobalData::current
->measurer
->stop();
270 void QTest::setResult(qint64 result
)
272 QBenchmarkTestMethodData::current
->setResult(result
);
277 void QTest::setResult(const QString
&tag
, qint64 result
)
279 QBenchmarkContext context
= QBenchmarkGlobalData::current
->context
;
281 QBenchmarkTestMethodData::current
->result
=
282 QBenchmarkResult( context
, result
,
283 QBenchmarkTestMethodData::current
->iterationCount
);
286 template <typename T
>
287 Q_TYPENAME
T::value_type
qAverage(const T
&container
)
289 Q_TYPENAME
T::const_iterator it
= container
.constBegin();
290 Q_TYPENAME
T::const_iterator end
= container
.constEnd();
291 Q_TYPENAME
T::value_type acc
= Q_TYPENAME
T::value_type();