Benchmark for QWaitCondition
Change-Id: I89fc0819324c12030bc23ba080b21f3d8d5c9852 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
7e468353c3
commit
3d2691ae77
@ -55,156 +55,88 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void oscillate_data();
|
void oscillate_mutex_data();
|
||||||
void oscillate();
|
void oscillate_mutex();
|
||||||
|
void oscillate_writelock_data();
|
||||||
void thrash_data();
|
void oscillate_writelock();
|
||||||
void thrash();
|
|
||||||
|
|
||||||
public:
|
|
||||||
static QWaitCondition local, remote;
|
|
||||||
enum Turn {LocalTurn, RemoteTurn};
|
|
||||||
static Turn turn;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QWaitCondition tst_QWaitCondition::local;
|
|
||||||
QWaitCondition tst_QWaitCondition::remote;
|
|
||||||
tst_QWaitCondition::Turn tst_QWaitCondition::turn = tst_QWaitCondition::LocalTurn;
|
|
||||||
|
|
||||||
|
int turn;
|
||||||
|
const int threadCount = 10;
|
||||||
|
QWaitCondition cond;
|
||||||
|
|
||||||
|
template <class Mutex, class Locker>
|
||||||
class OscillateThread : public QThread
|
class OscillateThread : public QThread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bool m_done;
|
Mutex *mutex;
|
||||||
bool m_useMutex;
|
int m_threadid;
|
||||||
unsigned long m_timeout;
|
int timeout;
|
||||||
bool m_wakeOne;
|
|
||||||
int count;
|
|
||||||
|
|
||||||
OscillateThread(bool useMutex, unsigned long timeout, bool wakeOne)
|
|
||||||
: m_done(false), m_useMutex(useMutex), m_timeout(timeout), m_wakeOne(wakeOne)
|
|
||||||
{}
|
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
QMutex mtx;
|
for (int count = 0; count < 5000; ++count) {
|
||||||
QReadWriteLock rwl;
|
|
||||||
count = 0;
|
|
||||||
|
|
||||||
forever {
|
Locker lock(mutex);
|
||||||
if (m_done)
|
while (m_threadid != turn) {
|
||||||
break;
|
cond.wait(mutex, timeout);
|
||||||
if (m_useMutex) {
|
|
||||||
mtx.lock();
|
|
||||||
while (tst_QWaitCondition::turn == tst_QWaitCondition::LocalTurn)
|
|
||||||
tst_QWaitCondition::remote.wait(&mtx, m_timeout);
|
|
||||||
mtx.unlock();
|
|
||||||
} else {
|
|
||||||
rwl.lockForWrite();
|
|
||||||
while (tst_QWaitCondition::turn == tst_QWaitCondition::LocalTurn)
|
|
||||||
tst_QWaitCondition::remote.wait(&rwl, m_timeout);
|
|
||||||
rwl.unlock();
|
|
||||||
}
|
}
|
||||||
tst_QWaitCondition::turn = tst_QWaitCondition::LocalTurn;
|
turn = (turn+1) % threadCount;
|
||||||
if (m_wakeOne)
|
cond.wakeAll();
|
||||||
tst_QWaitCondition::local.wakeOne();
|
|
||||||
else
|
|
||||||
tst_QWaitCondition::local.wakeAll();
|
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void tst_QWaitCondition::oscillate_data()
|
template <class Mutex, class Locker>
|
||||||
|
void oscillate(unsigned long timeout) {
|
||||||
|
|
||||||
|
OscillateThread<Mutex, Locker> thrd[threadCount];
|
||||||
|
Mutex m;
|
||||||
|
for (int i = 0; i < threadCount; ++i) {
|
||||||
|
thrd[i].mutex = &m;
|
||||||
|
thrd[i].m_threadid = i;
|
||||||
|
thrd[i].timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
QBENCHMARK {
|
||||||
|
for (int i = 0; i < threadCount; ++i) {
|
||||||
|
thrd[i].start();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < threadCount; ++i) {
|
||||||
|
thrd[i].wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QWaitCondition::oscillate_mutex_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<bool>("useMutex");
|
|
||||||
QTest::addColumn<unsigned long>("timeout");
|
QTest::addColumn<unsigned long>("timeout");
|
||||||
QTest::addColumn<bool>("wakeOne");
|
|
||||||
|
|
||||||
QTest::newRow("mutex, timeout, one") << true << 1000ul << true;
|
QTest::newRow("0") << 0ul;
|
||||||
QTest::newRow("readWriteLock, timeout, one") << false << 1000ul << true;
|
QTest::newRow("1") << 1ul;
|
||||||
QTest::newRow("mutex, timeout, all") << true << 1000ul << false;
|
QTest::newRow("1000") << 1000ul;
|
||||||
QTest::newRow("readWriteLock, timeout, all") << false << 1000ul << false;
|
QTest::newRow("forever") << ULONG_MAX;
|
||||||
QTest::newRow("mutex, forever, one") << true << ULONG_MAX << true;
|
|
||||||
QTest::newRow("readWriteLock, forever, one") << false << ULONG_MAX << true;
|
|
||||||
QTest::newRow("mutex, forever, all") << true << ULONG_MAX << false;
|
|
||||||
QTest::newRow("readWriteLock, forever, all") << false << ULONG_MAX << false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QWaitCondition::oscillate()
|
void tst_QWaitCondition::oscillate_mutex()
|
||||||
{
|
{
|
||||||
QMutex mtx;
|
|
||||||
QReadWriteLock rwl;
|
|
||||||
|
|
||||||
QFETCH(bool, useMutex);
|
|
||||||
QFETCH(unsigned long, timeout);
|
QFETCH(unsigned long, timeout);
|
||||||
QFETCH(bool, wakeOne);
|
oscillate<QMutex, QMutexLocker>(timeout);
|
||||||
|
|
||||||
turn = LocalTurn;
|
|
||||||
OscillateThread thrd(useMutex, timeout, wakeOne);
|
|
||||||
thrd.start();
|
|
||||||
|
|
||||||
QBENCHMARK {
|
|
||||||
if (useMutex)
|
|
||||||
mtx.lock();
|
|
||||||
else
|
|
||||||
rwl.lockForWrite();
|
|
||||||
turn = RemoteTurn;
|
|
||||||
if (wakeOne)
|
|
||||||
remote.wakeOne();
|
|
||||||
else
|
|
||||||
remote.wakeAll();
|
|
||||||
if (useMutex) {
|
|
||||||
while (turn == RemoteTurn)
|
|
||||||
local.wait(&mtx, timeout);
|
|
||||||
mtx.unlock();
|
|
||||||
} else {
|
|
||||||
while (turn == RemoteTurn)
|
|
||||||
local.wait(&rwl, timeout);
|
|
||||||
rwl.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thrd.m_done = true;
|
void tst_QWaitCondition::oscillate_writelock_data()
|
||||||
remote.wakeAll();
|
|
||||||
thrd.wait();
|
|
||||||
|
|
||||||
QCOMPARE(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tst_QWaitCondition::thrash_data()
|
|
||||||
{
|
{
|
||||||
oscillate_data();
|
oscillate_mutex_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QWaitCondition::thrash()
|
void tst_QWaitCondition::oscillate_writelock()
|
||||||
{
|
{
|
||||||
QMutex mtx;
|
|
||||||
mtx.lock();
|
|
||||||
|
|
||||||
QFETCH(bool, useMutex);
|
|
||||||
QFETCH(unsigned long, timeout);
|
QFETCH(unsigned long, timeout);
|
||||||
QFETCH(bool, wakeOne);
|
oscillate<QReadWriteLock, QWriteLocker>(timeout);
|
||||||
|
|
||||||
turn = LocalTurn;
|
|
||||||
OscillateThread thrd(useMutex, timeout, wakeOne);
|
|
||||||
thrd.start();
|
|
||||||
local.wait(&mtx, 1000ul);
|
|
||||||
mtx.unlock();
|
|
||||||
|
|
||||||
QBENCHMARK {
|
|
||||||
turn = RemoteTurn;
|
|
||||||
if (wakeOne)
|
|
||||||
remote.wakeOne();
|
|
||||||
else
|
|
||||||
remote.wakeAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thrd.m_done = true;
|
|
||||||
turn = RemoteTurn;
|
|
||||||
remote.wakeAll();
|
|
||||||
thrd.wait();
|
|
||||||
|
|
||||||
QCOMPARE(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
QTEST_MAIN(tst_QWaitCondition)
|
QTEST_MAIN(tst_QWaitCondition)
|
||||||
#include "tst_qwaitcondition.moc"
|
#include "tst_qwaitcondition.moc"
|
||||||
|
@ -3,3 +3,4 @@ SUBDIRS = \
|
|||||||
qmutex \
|
qmutex \
|
||||||
qthreadstorage \
|
qthreadstorage \
|
||||||
qthreadpool \
|
qthreadpool \
|
||||||
|
qwaitcondition \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user