6

I am writing some unit tests using QTest in Qt. I also encountered the QBENCHMARK macro, which benchmarks the code it encapsulates.

I am running my unit tests and benchmarks some of the code. The QBENCHMARK reports how long it took to execute some method and that is fine. I want to use the execution time in a unit test with for example QVERIFY2(). How can I do this?

EDIT:

What I am currently doing is:

void UnitTest::benchmark()
{
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");

    QCOMPARE(str1.localeAwareCompare(str2), 0);

    QBENCHMARK {
        str1.localeAwareCompare(str2);
    }
}
uniquenamehere
  • 1,869
  • 3
  • 31
  • 60

1 Answers1

2

From the documentation:

void QTest::setBenchmarkResult(qreal result, QBenchmarkMetric metric)

Sets the benchmark result for this test function to result.

Use this function if you want to report benchmark results without using the QBENCHMARK macro. Use metric to specify how Qt Test should interpret the results.

The context for the result will be the test function name and any data tag from the _data function. This function can only be called once in each test function, subsequent calls will replace the earlier reported results.

Note that the -iterations command line argument has no effect on test functions without the QBENCHMARK macro.

You could also of course just use an elapsed timer for such a thing.

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • I updated the question with the code I am currently running. I don't quite understand how I can get it to work similar with setBenchmarkResult? It says its context is the test function and eventually the test function _data, but I tried with the WallTimeNano and its just 0.00000000. The macro averages the run time over many measurements, not sure if this one does? I am trying to write equally functional code as the one I have in my question. – uniquenamehere Jun 04 '14 at 19:38
  • 1
    @Phataas: you have not replied to my initial comment. It is unclear what you are trying to achieve. – László Papp Jun 04 '14 at 19:46