Question

Is there a simply way to knowing how much time consume each test case in QT Test Framework for C++?

It will be incredible helpful to getting some metrics.

Was it helpful?

Solution

You could write a custom Timer class which starts a usually monotonic elapsed timer on creation and prints the elapsed time on deletion (see QElapsedTimer::elapsed()):

timer.h:

#include <QElapsedTimer>
class Timer {
public:
    Timer() {
        timer.start();
    }
    ~Timer() {
        qint64 ms = timer.elapsed();
        qDebug("Time needed: %s ms%s", ms,
               timer.isMontonic() ? "" : " (WARNING: non-monotonic)");
    }
private:
    QElapsedTimer timer;
}

The benefit of using QElapsedTimer over QTime is that Qt will try to use the best available monotonic timer on each platform. QTime is not guaranteed to be montonic: it will decrease when time synchronization daemon/service adjusts the clock, etc.

Now insert the line Timer t; at the beginning of each test case you want to measure the time of; no more code. This simply creates a Timer object, which starts the internal timer, and deletes the object when it gets out of scope (which is at the end of the method) and thus prints the elapsed time:

Your test case (.cpp):

#include "timer.h"
...
void TestClass::myTestCase1()
{
    Timer t; //the only line needed to add to your test case

    ... //the code for test case
}
void TestClass::myTestCase2()
{
    Timer t; //the only line needed to add to your test case

    ... //the code for test case
}
...

OTHER TIPS

time_t t = time(0); will define a time variable in milliseconds since the epoch, and set it to the current moment. Fire one of these off before testing, one after testing, and compare the two to get how long the test took. As cbamber85 noted, it varies based on a number of things, so if you want your metrics to mean anything, you need to keep your platform stable (and even then they'll only be significant as a relative thing) but it's at least somethign you can work with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top