Brush up QReadWriteLock benchmark

- add override
- tests C++17 shared_mutex in addition to C++14 shared_timed_mutex
- replace manual memory management with unique_ptr

Change-Id: If52df2097a4b92c10df4a7cdbb1c506d64b673e3
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2019-09-09 17:35:05 +02:00
parent 3fede6cb54
commit 72259e7186
2 changed files with 23 additions and 8 deletions

View File

@ -1,6 +1,7 @@
TEMPLATE = app
TARGET = tst_bench_qreadwritelock
QT = core testlib
QT = core-private testlib
SOURCES += tst_qreadwritelock.cpp
CONFIG += c++14 # for std::shared_timed_mutex
CONFIG += c++1z # for std::shared_mutex

View File

@ -28,12 +28,14 @@
#include <QtCore/QtCore>
#include <QtTest/QtTest>
#include <QtCore/private/qmemory_p.h>
#include <mutex>
#if QT_HAS_INCLUDE(<shared_mutex>)
#if __cplusplus > 201103L
#include <shared_mutex>
#endif
#endif
#include <vector>
// Wrapers that take pointers instead of reference to have the same interface as Qt
template <typename T>
@ -106,6 +108,14 @@ void tst_QReadWriteLock::uncontended_data()
<< FunctionPtrHolder(testUncontended<QReadWriteLock, QWriteLocker>);
QTest::newRow("std::mutex") << FunctionPtrHolder(
testUncontended<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>);
#ifdef __cpp_lib_shared_mutex
QTest::newRow("std::shared_mutex, read") << FunctionPtrHolder(
testUncontended<std::shared_mutex,
LockerWrapper<std::shared_lock<std::shared_mutex>>>);
QTest::newRow("std::shared_mutex, write") << FunctionPtrHolder(
testUncontended<std::shared_mutex,
LockerWrapper<std::unique_lock<std::shared_mutex>>>);
#endif
#if defined __cpp_lib_shared_timed_mutex
QTest::newRow("std::shared_timed_mutex, read") << FunctionPtrHolder(
testUncontended<std::shared_timed_mutex,
@ -130,7 +140,7 @@ void testReadOnly()
struct Thread : QThread
{
Mutex *lock;
void run()
void run() override
{
for (int i = 0; i < Iterations; ++i) {
QString s = QString::number(i); // Do something outside the lock
@ -140,21 +150,20 @@ void testReadOnly()
}
};
Mutex lock;
QVector<QThread *> threads;
std::vector<std::unique_ptr<Thread>> threads;
for (int i = 0; i < threadCount; ++i) {
auto t = new Thread;
auto t = qt_make_unique<Thread>();
t->lock = &lock;
threads.append(t);
threads.push_back(std::move(t));
}
QBENCHMARK {
for (auto t : threads) {
for (auto &t : threads) {
t->start();
}
for (auto t : threads) {
for (auto &t : threads) {
t->wait();
}
}
qDeleteAll(threads);
}
void tst_QReadWriteLock::readOnly_data()
@ -166,6 +175,11 @@ void tst_QReadWriteLock::readOnly_data()
QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testReadOnly<QReadWriteLock, QReadLocker>);
QTest::newRow("std::mutex") << FunctionPtrHolder(
testReadOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>);
#ifdef __cpp_lib_shared_mutex
QTest::newRow("std::shared_mutex") << FunctionPtrHolder(
testReadOnly<std::shared_mutex,
LockerWrapper<std::shared_lock<std::shared_mutex>>>);
#endif
#if defined __cpp_lib_shared_timed_mutex
QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder(
testReadOnly<std::shared_timed_mutex,