Qt 4.8
qbenchmark.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 #ifdef QT_GUI_LIB
47 #include <QtGui/qapplication.h>
48 #endif
49 
50 #include <QtCore/qprocess.h>
51 #include <QtCore/qdir.h>
52 #include <QtCore/qset.h>
53 #include <QtCore/qdebug.h>
54 
56 
58 
60  : measurer(0)
61  , walltimeMinimum(-1)
62  , iterationCount(-1)
63  , medianIterationCount(-1)
64  , createChart(false)
65  , verboseOutput(false)
66  , mode_(WallTime)
67 {
68  setMode(mode_);
69 }
70 
72 {
73  delete measurer;
75 }
76 
78 {
79  mode_ = mode;
80 
81  if (measurer)
82  delete measurer;
84 }
85 
87 {
89  if (0) {
90 #ifdef QTESTLIB_USE_VALGRIND
92  measurer = new QBenchmarkCallgrindMeasurer;
93 #endif
94 #ifdef HAVE_TICK_COUNTER
95  } else if (mode_ == TickCounter) {
96  measurer = new QBenchmarkTickMeasurer;
97 #endif
98  } else if (mode_ == EventCounter) {
99  measurer = new QBenchmarkEvent;
100  } else {
101  measurer = new QBenchmarkTimeMeasurer;
102  }
103  measurer->init();
104  return measurer;
105 }
106 
108 {
109  if (medianIterationCount != -1) {
110  return medianIterationCount;
111  } else {
112  return measurer->adjustMedianCount(1);
113  }
114 }
115 
116 
118 
120 :resultAccepted(false), runOnce(false), iterationCount(-1)
121 {
122 
123 }
124 
126 {
127  QBenchmarkTestMethodData::current = 0;
128 }
129 
131 {
133 }
134 
136 {
137 
138 }
139 
141 {
142  // Let the -iterations option override the measurer.
145  } else {
147  }
148 
149  return iterationCount;
150 }
151 
153  qreal value, QTest::QBenchmarkMetric metric, bool setByMacro)
154 {
155  bool accepted = false;
156 
157  // Always accept the result if the iteration count has been
158  // specified on the command line with -iterations.
160  accepted = true;
161 
162  else if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
163  iterationCount = 1;
164  accepted = true;
165  }
166 
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);
171  else
173 
174  // Accept the result or double the number of iterations.
175  if (accepted)
176  resultAccepted = true;
177  else
178  iterationCount *= 2;
179 
180  this->result = QBenchmarkResult(
181  QBenchmarkGlobalData::current->context, value, iterationCount, metric, setByMacro);
182 }
183 
199 {
200  i = 0;
201  if (runMode == RunOnce)
202  QBenchmarkTestMethodData::current->runOnce = true;
204 }
205 
207 {
208  i = 0;
210 }
211 
215 {
217  QBenchmarkTestMethodData::current->setResult(result, QBenchmarkGlobalData::current->measurer->metricType());
218 }
219 
223 {
224  if (QBenchmarkTestMethodData::current->runOnce)
225  return i > 0;
226  return i >= QTest::iterationCount();
227 }
228 
232 {
233  ++i;
234 }
235 
239 {
240  return QBenchmarkTestMethodData::current->iterationCount;
241 }
242 
246 {
247  QBenchmarkTestMethodData::current->adjustIterationCount(count);
248 }
249 
253 {
254  QBenchmarkTestMethodData::current->iterationCount = count;
255  QBenchmarkTestMethodData::current->resultAccepted = true;
256 }
257 
261 {
263  // the clock is ticking after the line above, don't add code here.
264 }
265 
269 {
270  // the clock is ticking before the line below, don't add code here.
272 }
273 
292 {
293  QBenchmarkTestMethodData::current->setResult(result, metric, false);
294 }
295 
296 template <typename T>
297 Q_TYPENAME T::value_type qAverage(const T &container)
298 {
299  Q_TYPENAME T::const_iterator it = container.constBegin();
300  Q_TYPENAME T::const_iterator end = container.constEnd();
301  Q_TYPENAME T::value_type acc = Q_TYPENAME T::value_type();
302  int count = 0;
303  while (it != end) {
304  acc += *it;
305  ++it;
306  ++count;
307  }
308  return acc / count;
309 }
310 
static QBenchmarkGlobalData * current
Definition: qbenchmark_p.h:136
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QBenchmarkResult result
Definition: qbenchmark_p.h:181
#define it(className, varName)
QBenchmarkMetric
This enum lists all the things that can be benchmarked.
virtual void start()=0
QBenchmarkMeasurerBase * measurer
Definition: qbenchmark_p.h:146
virtual int adjustMedianCount(int suggestion)=0
virtual int adjustIterationCount(int suggestion)=0
virtual bool isMeasurementAccepted(qint64 measurement)=0
void setIterationCount(int count)
Definition: qbenchmark.cpp:252
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_TESTLIB_EXPORT quint64 endBenchmarkMeasurement()
Definition: qbenchmark.cpp:268
void Q_TESTLIB_EXPORT setBenchmarkResult(qreal result, QBenchmarkMetric metric)
Sets the benchmark result for this test function to result.
Definition: qbenchmark.cpp:291
unsigned __int64 quint64
Definition: qglobal.h:943
virtual qint64 stop()=0
#define Q_TYPENAME
Definition: qglobal.h:1717
void setIterationCountHint(int count)
Definition: qbenchmark.cpp:245
int adjustIterationCount(int suggestion)
Definition: qbenchmark.cpp:140
void setResult(qreal value, QTest::QBenchmarkMetric metric, bool setByMacro=true)
Definition: qbenchmark.cpp:152
void setMode(Mode mode)
Definition: qbenchmark.cpp:77
QBenchmarkMeasurerBase * createMeasurer()
Definition: qbenchmark.cpp:86
int iterationCount()
Definition: qbenchmark.cpp:238
static QBenchmarkTestMethodData * current
Definition: qbenchmark_p.h:167
Q_TYPENAME T::value_type qAverage(const T &container)
Definition: qbenchmark.cpp:297
Q_TESTLIB_EXPORT void beginBenchmarkMeasurement()
Definition: qbenchmark.cpp:260
static const KeyPair *const end
int adjustMedianIterationCount()
Definition: qbenchmark.cpp:107