Search code examples
c++qttestingsignalsbenchmarking

How can I benchmark signals in the Qt test framework?


I'm implementing the Connection Scan Algorithm in Qt C++ and I added some tests to the project using the Qt test framework which is fine. I looked in the docs of the Qt test framework but I can't find a way to benchmark a called asynchronous method and wait for it's signal.

You have QSignalSpy available to test signals but it waits a certain time before it checks if the signal was fired. QBENCHMARK can measure the time to execute a piece of code. I would like to measure the time between the call of the asynchronous method and the signal fired by the thread which the method calls.

QString str1 = "foo";
QString str2 = "foo";

QBENCHMARK {
    str1.localeAwareCompare(str2);
}

The code above is fine for synchronous operations but not for asynchronous operations with signals.


Solution

  • Qt doesn't have anything in their test framework to accomplish this. However, with the help of a QEventLoop you can wait for the signal to fire (which makes your code synchronous again).

    If you apply a QBENCHMARK macro around the event loop you can benchmark the asynchronous method. There's one downside to this: You still have some overhead for creating a QEventLoop and connecting the signal to the quit() method of the event loop.

    QBENCHMARK_ONCE {
        planner->getConnections(
                    QUrl("http://irail.be/stations/NMBS/008811189"), // From: Vilvoorde
                    QUrl("http://irail.be/stations/NMBS/008891009"), // To: Brugge
                    QDateTime::fromString("2018-08-02T13:00:00.000Z", Qt::ISODate), // Departure time (UTC)
                    4 // Max transfers
                    );
    
        QEventLoop loop;
        connect(planner, SIGNAL(routesFound(QList<CSA::Route*>)), &loop, SLOT(quit()));
        loop.exec();
    }