Qt:衡量事件处理时间

时间:2013-02-26 19:43:06

标签: qt

我想测量我的应用程序中的哪些事件需要很长时间才能在主线程(阻塞GUI)中执行,或者至少是否有任何超过,比如10毫秒。我显然使用线程和并发性来完成需要很长时间的任务,但是有时很难在其他线程中放置什么以及GUI可以保留什么之间划清界线。特别是对于在多个操作系统上运行的应用程序以及新旧硬件和几年旧硬件。

我查看了QApplication(和QCoreApplication)但它没有任何“processSingleEvent”类型的功能,我可以轻松覆盖并包装时间测量。事件过滤器也不起作用,因为AFAIU在处理事件后无法获得通知。

我认为我可以手动调用QApplication :: processEvents(无需调用exec),但同样它不会提供单事件粒度,并且正如我所读,它不处理destroy事件。

我查看了QCoreApplication :: exec实现,发现它在内部使用QEventLoop,所以如果我想将我的特殊代码添加到原始实现中,我将不得不重新实现QApplication和QEventLoop从Qt源复制大量代码。 ..

编辑:问题显然是:如何以简单和“干净”的方式衡量事件处理时间?

1 个答案:

答案 0 :(得分:5)

覆盖bool QCoreApplication::notify ( QObject * receiver, QEvent * event )

class MyApplication : public QApplication
{
    QElapsedTimer t;
public:
    MyApplication(int& argc, char ** argv) : QApplication(argc, argv) { }
    virtual ~MyApplication() { }

    virtual bool notify(QObject* receiver, QEvent* event)
    {
        t.start();
        bool ret = QApplication::notify(receiver, event);
        if(t.elapsed() > 10)
            qDebug("processing event type %d for object %s took %dms",
                (int)event->type(), receiver->objectName().toLocal8Bit().data(),
                (int)t.elapsed());
        return ret;
    }
};

int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);

    ...

这也恰好是catch all type exception handling的地方。