From 72259e7186cdba25fa856bf379a35326f1cbad3d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 9 Sep 2019 17:35:05 +0200 Subject: Brush up QReadWriteLock benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../thread/qreadwritelock/qreadwritelock.pro | 3 ++- .../thread/qreadwritelock/tst_qreadwritelock.cpp | 28 ++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'tests/benchmarks/corelib/thread') diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/qreadwritelock.pro b/tests/benchmarks/corelib/thread/qreadwritelock/qreadwritelock.pro index 86102adecd..a1827d0276 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/qreadwritelock.pro +++ b/tests/benchmarks/corelib/thread/qreadwritelock/qreadwritelock.pro @@ -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 diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp index fcf600a059..4093e97607 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp @@ -28,12 +28,14 @@ #include #include +#include #include #if QT_HAS_INCLUDE() #if __cplusplus > 201103L #include #endif #endif +#include // Wrapers that take pointers instead of reference to have the same interface as Qt template @@ -106,6 +108,14 @@ void tst_QReadWriteLock::uncontended_data() << FunctionPtrHolder(testUncontended); QTest::newRow("std::mutex") << FunctionPtrHolder( testUncontended>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex, read") << FunctionPtrHolder( + testUncontended>>); + QTest::newRow("std::shared_mutex, write") << FunctionPtrHolder( + testUncontended>>); +#endif #if defined __cpp_lib_shared_timed_mutex QTest::newRow("std::shared_timed_mutex, read") << FunctionPtrHolder( testUncontended threads; + std::vector> threads; for (int i = 0; i < threadCount; ++i) { - auto t = new Thread; + auto t = qt_make_unique(); 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); QTest::newRow("std::mutex") << FunctionPtrHolder( testReadOnly>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex") << FunctionPtrHolder( + testReadOnly>>); +#endif #if defined __cpp_lib_shared_timed_mutex QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder( testReadOnly Date: Wed, 11 Sep 2019 10:39:14 +0200 Subject: QReadWriteLock: add a check for writeOnly, too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Results on my machine: PASS : tst_QReadWriteLock::writeOnly(QMutex) RESULT : tst_QReadWriteLock::writeOnly():QMutex: 3,607 msecs per iteration (total: 3,607, iterations: 1) PASS : tst_QReadWriteLock::writeOnly(QReadWriteLock) RESULT : tst_QReadWriteLock::writeOnly():QReadWriteLock: 39,703 msecs per iteration (total: 39,703, iterations: 1) PASS : tst_QReadWriteLock::writeOnly(std::mutex) RESULT : tst_QReadWriteLock::writeOnly():std::mutex: 3,697 msecs per iteration (total: 3,697, iterations: 1) PASS : tst_QReadWriteLock::writeOnly(std::shared_mutex) RESULT : tst_QReadWriteLock::writeOnly():std::shared_mutex: 5,727 msecs per iteration (total: 5,727, iterations: 1) PASS : tst_QReadWriteLock::writeOnly(std::shared_timed_mutex) RESULT : tst_QReadWriteLock::writeOnly():std::shared_timed_mutex: 5,921 msecs per iteration (total: 5,921, iterations: 1) (the 'nothing' test of course doesn't work with writing, as writing to the same QString from different threads is UB) Change-Id: Ia78b54963a51eaf6563ce0d243316a3337056a83 Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Mårten Nordheim --- .../thread/qreadwritelock/tst_qreadwritelock.cpp | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'tests/benchmarks/corelib/thread') diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp index 4093e97607..1d47d98657 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp @@ -65,6 +65,8 @@ private slots: void uncontended(); void readOnly_data(); void readOnly(); + void writeOnly_data(); + void writeOnly(); // void readWrite(); }; @@ -193,5 +195,66 @@ void tst_QReadWriteLock::readOnly() holder.value(); } +static QString global_string; + +template +void testWriteOnly() +{ + struct Thread : QThread + { + Mutex *lock; + void run() override + { + for (int i = 0; i < Iterations; ++i) { + QString s = QString::number(i); // Do something outside the lock + Locker locker(lock); + global_string = s; + } + } + }; + Mutex lock; + std::vector> threads; + for (int i = 0; i < threadCount; ++i) { + auto t = qt_make_unique(); + t->lock = &lock; + threads.push_back(std::move(t)); + } + QBENCHMARK { + for (auto &t : threads) { + t->start(); + } + for (auto &t : threads) { + t->wait(); + } + } +} + +void tst_QReadWriteLock::writeOnly_data() +{ + QTest::addColumn("holder"); + + // QTest::newRow("nothing") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("QMutex") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("std::mutex") << FunctionPtrHolder( + testWriteOnly>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex") << FunctionPtrHolder( + testWriteOnly>>); +#endif +#if defined __cpp_lib_shared_timed_mutex + QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder( + testWriteOnly>>); +#endif +} + +void tst_QReadWriteLock::writeOnly() +{ + QFETCH(FunctionPtrHolder, holder); + holder.value(); +} + QTEST_MAIN(tst_QReadWriteLock) #include "tst_qreadwritelock.moc" -- cgit v1.2.3