提交 0bfb505a 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

加入直接调用测试

上级 e3772c4c
......@@ -82,32 +82,43 @@ int main(int argc, char *argv[])
thread2->start();
printf("Test signals and slots...\n");
QCoreApplication::processEvents();
a.connect (obj1,&TestObj::sig_finished,[=]()->void{
printf("Test Events...\n");
QThread::msleep(1000);
QCoreApplication::postEvent(obj1,new QEvent(TestEvent::startEvt()));
QCoreApplication::postEvent(obj1,new QEvent(TestMsg::startEvt()));
});
a.connect (obj1,&TestObj::evt_finished,[=]()->void{
QThread::msleep(1000);
QCoreApplication::postEvent(obj1,new QEvent(TestEvent::quitEvt()));
QCoreApplication::postEvent(obj2,new QEvent(TestEvent::quitEvt()));
QCoreApplication::postEvent(obj1,new QEvent(TestMsg::quitEvt()));
QCoreApplication::postEvent(obj2,new QEvent(TestMsg::quitEvt()));
});
QThread::msleep(1000);
QCoreApplication::processEvents();
QCoreApplication::postEvent(obj1,new QEvent(TestEvent::startSig()));
QCoreApplication::postEvent(obj1,new QEvent(TestMsg::startSig()));
a.processEvents();
thread1->wait();
thread2->wait();
QThread::msleep(1000);
obj1->runDirectCall();
printf("Finished.\n");
thread1->deleteLater();
thread2->deleteLater();
obj1->deleteLater();
obj2->deleteLater();
QThread::msleep(1000);
QCoreApplication::processEvents();
return 0;
}
#include "testevent.h"
#include <QDebug>
//Regisit a event
QEvent::Type TestEvent::m_testEvt = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestEvent::m_startEvt = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestEvent::m_startSig = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestEvent::m_quit = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestMsg::m_testEvt = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestMsg::m_startEvt = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestMsg::m_startSig = (QEvent::Type)QEvent::registerEventType();
QEvent::Type TestMsg::m_quit = (QEvent::Type)QEvent::registerEventType();
TestEvent::TestEvent(clock_t clk)
TestMsg::TestMsg(clock_t clk)
:QEvent(m_testEvt)
,m_clk(clk)
{
}
TestEvent::~TestEvent()
TestMsg::~TestMsg()
{
//qDebug()<<"Destory";
}
......@@ -3,7 +3,8 @@
#include <QEvent>
#include <time.h>
class TestEvent : public QEvent
#include <QString>
class TestMsg : public QEvent
{
private:
static QEvent::Type m_testEvt;
......@@ -11,15 +12,23 @@ private:
static QEvent::Type m_startSig;
static QEvent::Type m_quit;
clock_t m_clk = 0;
QString m_dummyLongStr;
public:
TestEvent(clock_t clk);
~TestEvent();
TestMsg(clock_t clk);
~TestMsg();
inline clock_t clock() {return m_clk;}
inline void fillStr(int len)
{
for (int i=0;i<len;++i)
{
m_dummyLongStr.push_back((char)(i*37%64+32));
}
}
public:
static inline QEvent::Type type() {return m_testEvt;}
static inline QEvent::Type startEvt() {return m_startEvt;}
static inline QEvent::Type startSig() {return m_startSig;}
static inline QEvent::Type quitEvt() {return m_quit;}
static inline QEvent::Type quitEvt() {return m_quit;}
};
#endif // TESTEVENT_H
......@@ -3,8 +3,8 @@
#include <QCoreApplication>
#include <QTextStream>
#include <QThread>
static const int testCounts = 100000;
static const int testCounts = 10000;
static const int fillStrLen = 1024;
TestObj::TestObj(QObject *parent)
: QObject{parent}
{
......@@ -13,16 +13,17 @@ TestObj::TestObj(QObject *parent)
void TestObj::customEvent(QEvent * evt)
{
if (evt->type()==TestEvent::type())
if (evt->type()==TestMsg::type())
{
clock_t curr_clk = clock();
if (m_nFirstClkEvt==-1)
m_nFirstClkEvt = curr_clk;
TestEvent * e = dynamic_cast<TestEvent *>(evt);
TestMsg * e = dynamic_cast<TestMsg *>(evt);
if (e)
{
clock_t delay = curr_clk - e->clock();
m_nDelay_Evts += delay;
e->fillStr(fillStrLen);
++m_nCount_Evts;
}
if (m_nEmittedEvts<=testCounts)
......@@ -43,7 +44,7 @@ void TestObj::customEvent(QEvent * evt)
if (m_buddy->m_nCount_Evts>testCounts && m_nCount_Evts>testCounts )
emit evt_finished();
}
else if (evt->type()==TestEvent::startEvt())
else if (evt->type()==TestMsg::startEvt())
{
m_nCount_Evts = 0;
m_nDelay_Evts = 0;
......@@ -58,7 +59,7 @@ void TestObj::customEvent(QEvent * evt)
}
next_event();
}
else if (evt->type()==TestEvent::startSig())
else if (evt->type()==TestMsg::startSig())
{
m_nCount_Sigs = 0;
m_nDelay_Sigs = 0;
......@@ -73,7 +74,7 @@ void TestObj::customEvent(QEvent * evt)
}
next_signal();
}
else if (evt->type()==TestEvent::quitEvt())
else if (evt->type()==TestMsg::quitEvt())
{
thread()->quit();
}
......@@ -86,14 +87,13 @@ void TestObj::test_slot16(QEvent * evt)
if (m_nFirstClkSig==-1)
m_nFirstClkSig = curr_clk;
TestEvent * e = dynamic_cast<TestEvent *>(evt);
TestMsg * e = dynamic_cast<TestMsg *>(evt);
if (e)
{
clock_t delay = curr_clk - e->clock();
m_nDelay_Evts += delay;
++m_nCount_Evts;
m_nDelay_Sigs += delay;
++m_nCount_Sigs;
e->fillStr(fillStrLen);
if (m_nEmittedSigs<=testCounts)
next_signal();
if (m_nCount_Sigs==testCounts)
......@@ -116,7 +116,7 @@ void TestObj::test_slot16(QEvent * evt)
void TestObj::next_signal()
{
emit test_sig16(new TestEvent(clock()));
emit test_sig16(new TestMsg(clock()));
++m_nEmittedSigs;
}
void TestObj::next_event()
......@@ -124,5 +124,45 @@ void TestObj::next_event()
if (!m_buddy)
return;
++m_nEmittedEvts;
QCoreApplication::postEvent(m_buddy,new TestEvent(clock()));
QCoreApplication::postEvent(m_buddy,new TestMsg(clock()));
}
void TestObj::runDirectCall()
{
m_nCount_Dir = 0;
m_nDelay_Dir = 0;
m_nFirstClkDir = -1;
printf("Test Direct Call...\n");
for (int i=0;i<testCounts;++i)
{
direct_call(new TestMsg(clock()));
}
}
void TestObj::direct_call(QEvent * evt)
{
clock_t curr_clk = clock();
if (m_nFirstClkDir==-1)
m_nFirstClkDir = curr_clk;
TestMsg * e = dynamic_cast<TestMsg *>(evt);
if (e)
{
clock_t delay = curr_clk - e->clock();
m_nDelay_Dir += delay;
++m_nCount_Dir;
e->fillStr(fillStrLen);
if (m_nCount_Dir==testCounts)
{
QTextStream strm(stdout);
strm << objectName()<< QString().asprintf(" (%llX) run %d Direct Calls, total costs %.2lf ms, AVG cost %.2lf us / test, AVG delay=%.2lf us.\n"
,(unsigned long long)this
,(int)(m_nCount_Dir)
,1e3 * (curr_clk - m_nFirstClkDir) / CLOCKS_PER_SEC
,1e6 * (curr_clk - m_nFirstClkDir) / CLOCKS_PER_SEC / testCounts
,1e6 * m_nDelay_Dir/ CLOCKS_PER_SEC / m_nCount_Dir
);
strm.flush();
}
delete e;
}
}
......@@ -13,6 +13,7 @@ public:
m_buddy = buddy;
buddy->m_buddy = this;
}
void runDirectCall();
public slots:
void test_slot1(QEvent * evt){}
void test_slot2(QEvent * evt){}
......@@ -80,6 +81,10 @@ private:
clock_t m_nFirstClkEvt = -1;
quint32 m_nCount_Evts = 0;
clock_t m_nDelay_Evts = 0;
clock_t m_nFirstClkDir = -1;
quint32 m_nCount_Dir = 0;
clock_t m_nDelay_Dir = 0;
private:
quint32 m_nEmittedSigs = 0;
quint32 m_nEmittedEvts = 0;
......@@ -87,6 +92,8 @@ private:
private:
void next_signal();
void next_event();
private:
void direct_call(QEvent * evt);
};
#endif // TESTOBJ_H
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册