src/testlib/qbenchmark.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "QtTest/qbenchmark.h"
    42 #include "QtTest/qbenchmark.h"
    43 #include "QtTest/private/qbenchmark_p.h"
    43 #include "QtTest/private/qbenchmark_p.h"
       
    44 #include "QtTest/private/qbenchmarkmetric_p.h"
    44 
    45 
    45 #ifdef QT_GUI_LIB
    46 #ifdef QT_GUI_LIB
    46 #include <QtGui/qapplication.h>
    47 #include <QtGui/qapplication.h>
    47 #endif
    48 #endif
    48 
    49 
   136 
   137 
   137 }
   138 }
   138 
   139 
   139 int QBenchmarkTestMethodData::adjustIterationCount(int suggestion)
   140 int QBenchmarkTestMethodData::adjustIterationCount(int suggestion)
   140 {
   141 {
   141     // Let the -iteration-count option override the measurer.
   142     // Let the -iterations option override the measurer.
   142     if (QBenchmarkGlobalData::current->iterationCount != -1) {
   143     if (QBenchmarkGlobalData::current->iterationCount != -1) {
   143         iterationCount = QBenchmarkGlobalData::current->iterationCount;
   144         iterationCount = QBenchmarkGlobalData::current->iterationCount;
   144     } else {
   145     } else {
   145         iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion);
   146         iterationCount = QBenchmarkGlobalData::current->measurer->adjustIterationCount(suggestion);
   146     }
   147     }
   147 
   148 
   148     return iterationCount;
   149     return iterationCount;
   149 }
   150 }
   150 
   151 
   151 void QBenchmarkTestMethodData::setResult(qint64 value)
   152 void QBenchmarkTestMethodData::setResult(
       
   153     qreal value, QTest::QBenchmarkMetric metric, bool setByMacro)
   152 {
   154 {
   153     bool accepted = false;
   155     bool accepted = false;
   154 
   156 
   155     // Always accept the result if the iteration count has been
   157     // Always accept the result if the iteration count has been
   156     // specified on the command line with -iteartion-count.
   158     // specified on the command line with -iterations.
   157     if (QBenchmarkGlobalData::current->iterationCount != -1)
   159     if (QBenchmarkGlobalData::current->iterationCount != -1)
   158         accepted = true;
   160         accepted = true;
   159 
   161 
   160     if (QBenchmarkTestMethodData::current->runOnce) {
   162     if (QBenchmarkTestMethodData::current->runOnce || !setByMacro) {
   161         iterationCount = 1;
   163         iterationCount = 1;
   162         accepted = true;
   164         accepted = true;
   163     }
   165     }
   164     
   166 
   165     // Test the result directly without calling the measurer if the minimum time 
   167     // Test the result directly without calling the measurer if the minimum time 
   166     // has been specifed on the command line with -minimumvalue.
   168     // has been specifed on the command line with -minimumvalue.
   167     else if (QBenchmarkGlobalData::current->walltimeMinimum != -1)
   169     else if (QBenchmarkGlobalData::current->walltimeMinimum != -1)
   168         accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum);
   170         accepted = (value > QBenchmarkGlobalData::current->walltimeMinimum);
   169     else
   171     else
   173     if (accepted)
   175     if (accepted)
   174         resultAccepted = true;
   176         resultAccepted = true;
   175     else
   177     else
   176         iterationCount *= 2;
   178         iterationCount *= 2;
   177 
   179 
   178     this->result = 
   180     this->result = QBenchmarkResult(
   179         QBenchmarkResult(QBenchmarkGlobalData::current->context, value, iterationCount);
   181         QBenchmarkGlobalData::current->context, value, iterationCount, metric, setByMacro);
   180 }
   182 }
   181 
   183 
   182 /*!
   184 /*!
   183     \class QTest::QBenchmarkIterationController
   185     \class QTest::QBenchmarkIterationController
   184     \internal
   186     \internal
   206 
   208 
   207 /*! \internal
   209 /*! \internal
   208 */
   210 */
   209 QTest::QBenchmarkIterationController::~QBenchmarkIterationController()
   211 QTest::QBenchmarkIterationController::~QBenchmarkIterationController()
   210 {
   212 {
   211     QBenchmarkTestMethodData::current->setResult(QTest::endBenchmarkMeasurement());
   213     const qreal result = QTest::endBenchmarkMeasurement();
       
   214     QBenchmarkTestMethodData::current->setResult(result, QBenchmarkGlobalData::current->measurer->metricType());
   212 }
   215 }
   213 
   216 
   214 /*! \internal
   217 /*! \internal
   215 */
   218 */
   216 bool QTest::QBenchmarkIterationController::isDone()
   219 bool QTest::QBenchmarkIterationController::isDone()
   257     // the clock is ticking after the line above, don't add code here.
   260     // the clock is ticking after the line above, don't add code here.
   258 }
   261 }
   259 
   262 
   260 /*! \internal
   263 /*! \internal
   261 */
   264 */
   262 qint64 QTest::endBenchmarkMeasurement()
   265 quint64 QTest::endBenchmarkMeasurement()
   263 {
   266 {
   264     // the clock is ticking before the line below, don't add code here.
   267     // the clock is ticking before the line below, don't add code here.
   265     return QBenchmarkGlobalData::current->measurer->stop();    
   268     return QBenchmarkGlobalData::current->measurer->stop();    
   266 }
   269 }
   267 
   270 
   268 /*! \internal
   271 /*!
   269 */
   272     Sets the benchmark result for this test function to \a result.
   270 void QTest::setResult(qint64 result)
   273  
   271 {
   274     Use this function if you want to report benchmark results without
   272     QBenchmarkTestMethodData::current->setResult(result);
   275     using the QBENCHMARK macro. Use \a metric to specify how QTestLib
   273 }
   276     should interpret the results.
   274 
   277  
   275 /*! \internal
   278     The context for the result will be the test function name and any
   276 */
   279     data tag from the _data function. This function can only be called
   277 void QTest::setResult(const QString &tag, qint64 result)
   280     once in each test function, subsequent calls will replace the
   278 {
   281     earlier reported results.
   279     QBenchmarkContext context = QBenchmarkGlobalData::current->context;
   282 
   280     context.tag = tag;
   283     Note that the -iterations command line argument has no effect
   281     QBenchmarkTestMethodData::current->result = 
   284     on test functions without the QBENCHMARK macro.
   282         QBenchmarkResult( context, result,
   285 
   283             QBenchmarkTestMethodData::current->iterationCount);
   286     \since 4.7
       
   287 */
       
   288 void QTest::setBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric)
       
   289 {
       
   290     QBenchmarkTestMethodData::current->setResult(result, metric, false);
   284 }
   291 }
   285 
   292 
   286 template <typename T>
   293 template <typename T>
   287 Q_TYPENAME T::value_type qAverage(const T &container)
   294 Q_TYPENAME T::value_type qAverage(const T &container)
   288 {
   295 {
   296         ++count;
   303         ++count;
   297     }
   304     }
   298     return acc / count;
   305     return acc / count;
   299 }
   306 }
   300 
   307 
   301 
       
   302 QT_END_NAMESPACE
   308 QT_END_NAMESPACE
   303