tests/benchmarks/events/main.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    45 
    45 
    46 class PingPong : public QObject
    46 class PingPong : public QObject
    47 {
    47 {
    48 public:
    48 public:
    49     void setPeer(QObject *peer);
    49     void setPeer(QObject *peer);
       
    50     void resetCounter() {m_counter = 100;}
    50 
    51 
    51 protected:
    52 protected:
    52     bool event(QEvent *e);
    53     bool event(QEvent *e);
    53 
    54 
    54 private:
    55 private:
    67     --m_counter;
    68     --m_counter;
    68     if (m_counter > 0) {
    69     if (m_counter > 0) {
    69         QEvent *e = new QEvent(QEvent::User);
    70         QEvent *e = new QEvent(QEvent::User);
    70         QCoreApplication::postEvent(m_peer, e);
    71         QCoreApplication::postEvent(m_peer, e);
    71     } else {
    72     } else {
    72         QCoreApplication::quit();
    73         QTestEventLoop::instance().exitLoop();
    73     }
    74     }
    74     return true;
    75     return true;
    75 }
    76 }
    76 
    77 
    77 class EventTester : public QObject
    78 class EventTester : public QObject
   147 }
   148 }
   148 
   149 
   149 void EventsBench::postEvent_data()
   150 void EventsBench::postEvent_data()
   150 {
   151 {
   151     QTest::addColumn<bool>("filterEvents");
   152     QTest::addColumn<bool>("filterEvents");
       
   153     // The first time an eventloop is executed, the case runs radically slower at least
       
   154     // on some platforms, so test the "no eventfilter" case to get a comparable results
       
   155     // with the "eventfilter" case.
       
   156     QTest::newRow("first time, no eventfilter") << false;
   152     QTest::newRow("no eventfilter") << false;
   157     QTest::newRow("no eventfilter") << false;
   153     QTest::newRow("eventfilter") << true;
   158     QTest::newRow("eventfilter") << true;
   154 }
   159 }
   155 
   160 
   156 void EventsBench::postEvent()
   161 void EventsBench::postEvent()
   162     pong.setPeer(&ping);
   167     pong.setPeer(&ping);
   163     if (filterEvents) {
   168     if (filterEvents) {
   164         ping.installEventFilter(this);
   169         ping.installEventFilter(this);
   165         pong.installEventFilter(this);
   170         pong.installEventFilter(this);
   166     }
   171     }
   167     QEvent *e = new QEvent(QEvent::User);
   172 
   168     QBENCHMARK {
   173     QBENCHMARK {
       
   174         // In case multiple iterations are done, event needs to be created inside the QBENCHMARK,
       
   175         // or it gets deleted once first iteration exits and can cause a crash. Similarly,
       
   176         // ping and pong need their counters reset.
       
   177         QEvent *e = new QEvent(QEvent::User);
       
   178         ping.resetCounter();
       
   179         pong.resetCounter();
   169         QCoreApplication::postEvent(&ping, e);
   180         QCoreApplication::postEvent(&ping, e);
   170         QTestEventLoop::instance().enterLoop( 61 );
   181         QTestEventLoop::instance().enterLoop( 61 );
   171     }
   182     }
   172 }
   183 }
   173 
   184