diff options
Diffstat (limited to 'tests/benchmarks/corelib/thread')
16 files changed, 457 insertions, 468 deletions
diff --git a/tests/benchmarks/corelib/thread/CMakeLists.txt b/tests/benchmarks/corelib/thread/CMakeLists.txt index 8f7b829a40..48bf0572a6 100644 --- a/tests/benchmarks/corelib/thread/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from thread.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause add_subdirectory(qfuture) add_subdirectory(qmutex) diff --git a/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt b/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt index 4a737c62b8..5988b1c0b0 100644 --- a/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qfuture Binary: ##################################################################### @@ -5,7 +8,7 @@ qt_internal_add_benchmark(tst_bench_qfuture EXCEPTIONS SOURCES - tst_qfuture.cpp - PUBLIC_LIBRARIES + tst_bench_qfuture.cpp + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qfuture/tst_qfuture.cpp b/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp index c7a4ff24d9..38b3f636c9 100644 --- a/tests/benchmarks/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp @@ -1,35 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qexception.h> #include <qfuture.h> +#include <qpromise.h> #include <qsemaphore.h> class tst_QFuture : public QObject @@ -37,7 +13,7 @@ class tst_QFuture : public QObject Q_OBJECT private slots: - void makeReadyfuture(); + void makeReadyValueFuture(); #ifndef QT_NO_EXCEPTIONS void makeExceptionalFuture(); #endif @@ -67,10 +43,10 @@ private slots: void progressText(); }; -void tst_QFuture::makeReadyfuture() +void tst_QFuture::makeReadyValueFuture() { QBENCHMARK { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); Q_UNUSED(future); } } @@ -88,7 +64,7 @@ void tst_QFuture::makeExceptionalFuture() void tst_QFuture::result() { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto value = future.result(); @@ -116,7 +92,7 @@ void tst_QFuture::results() void tst_QFuture::takeResult() { QBENCHMARK { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); auto value = future.takeResult(); Q_UNUSED(value); } @@ -164,7 +140,7 @@ void tst_QFuture::reportException() void tst_QFuture::then() { - auto f = QtFuture::makeReadyFuture(42); + auto f = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto future = f.then([](int value) { return value; }); Q_UNUSED(future); @@ -173,7 +149,7 @@ void tst_QFuture::then() void tst_QFuture::thenVoid() { - auto f = QtFuture::makeReadyFuture(); + auto f = QtFuture::makeReadyVoidFuture(); QBENCHMARK { auto future = f.then([] {}); Q_UNUSED(future); @@ -229,7 +205,7 @@ void tst_QFuture::onFailedVoid() void tst_QFuture::thenOnFailed() { - auto f = QtFuture::makeReadyFuture(42); + auto f = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto future = f.then([](int) { throw std::runtime_error("error"); }).onFailed([] { return 0; }); @@ -239,7 +215,7 @@ void tst_QFuture::thenOnFailed() void tst_QFuture::thenOnFailedVoid() { - auto f = QtFuture::makeReadyFuture(); + auto f = QtFuture::makeReadyVoidFuture(); QBENCHMARK { auto future = f.then([] { throw std::runtime_error("error"); }).onFailed([] {}); Q_UNUSED(future); @@ -317,4 +293,5 @@ void tst_QFuture::progressText() } QTEST_MAIN(tst_QFuture) -#include "tst_qfuture.moc" + +#include "tst_bench_qfuture.moc" diff --git a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt index 8872a50227..2d01f9ff1f 100644 --- a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from qmutex.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_bench_qmutex Binary: @@ -6,10 +7,8 @@ qt_internal_add_benchmark(tst_bench_qmutex SOURCES - tst_qmutex.cpp - PUBLIC_LIBRARIES + tst_bench_qmutex.cpp + LIBRARIES + Qt::CorePrivate Qt::Test ) - -#### Keys ignored in scope 1:.:.:qmutex.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/thread/qmutex/tst_qmutex.cpp b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp index 9cea995433..6ea0c25c0b 100644 --- a/tests/benchmarks/corelib/thread/qmutex/tst_qmutex.cpp +++ b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp @@ -1,38 +1,16 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QtCore> #include <QTest> +#include <QtCore/private/qvolatile_p.h> #include <math.h> //#define USE_SEM_T +using namespace std::chrono_literals; + #if defined(Q_OS_UNIX) #if !defined(USE_SEM_T) # include <pthread.h> @@ -75,8 +53,7 @@ void NativeMutexUnlock(NativeMutexType *mutex) } #endif #elif defined(Q_OS_WIN) -# define _WIN32_WINNT 0x0400 -# include <windows.h> +# include <qt_windows.h> typedef CRITICAL_SECTION NativeMutexType; void NativeMutexInitialize(NativeMutexType *mutex) { @@ -158,7 +135,7 @@ void tst_QMutex::noThread() QBENCHMARK { count = 0; for (int i = 0; i < N; i++) { - count++; + QtPrivate::volatilePreIncrement(count); } } break; @@ -167,7 +144,7 @@ void tst_QMutex::noThread() count = 0; for (int i = 0; i < N; i++) { mtx.lock(); - count++; + QtPrivate::volatilePreIncrement(count); mtx.unlock(); } } @@ -177,7 +154,7 @@ void tst_QMutex::noThread() count = 0; for (int i = 0; i < N; i++) { QMutexLocker locker(&mtx); - count++; + QtPrivate::volatilePreIncrement(count); } } break; @@ -253,7 +230,8 @@ void tst_QMutex::contendedNative_data() class NativeMutexThread : public QThread { NativeMutexType *mutex1, *mutex2; - int iterations, msleepDuration; + int iterations; + std::chrono::milliseconds msleepDuration; bool use2mutexes; public: bool done; @@ -271,8 +249,8 @@ public: NativeMutexLock(mutex1); if (use2mutexes) NativeMutexLock(mutex2); - if (msleepDuration >= 0) - msleep(msleepDuration); + if (msleepDuration >= 0ms) + sleep(msleepDuration); if (use2mutexes) NativeMutexUnlock(mutex2); NativeMutexUnlock(mutex1); @@ -296,7 +274,7 @@ void tst_QMutex::contendedNative() NativeMutexInitialize(&mutex2); QList<NativeMutexThread *> threads(threadCount); - for (int i = 0; i < threads.count(); ++i) { + for (int i = 0; i < threads.size(); ++i) { threads[i] = new NativeMutexThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes); threads[i]->start(); } @@ -308,11 +286,11 @@ void tst_QMutex::contendedNative() semaphore4.release(threadCount); } - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->done = true; semaphore1.acquire(threadCount); semaphore2.release(threadCount); - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->wait(); qDeleteAll(threads); @@ -323,7 +301,8 @@ void tst_QMutex::contendedNative() class QMutexThread : public QThread { QMutex *mutex1, *mutex2; - int iterations, msleepDuration; + int iterations; + std::chrono::milliseconds msleepDuration; bool use2mutexes; public: bool done; @@ -341,8 +320,8 @@ public: mutex1->lock(); if (use2mutexes) mutex2->lock(); - if (msleepDuration >= 0) - msleep(msleepDuration); + if (msleepDuration >= 0ms) + sleep(msleepDuration); if (use2mutexes) mutex2->unlock(); mutex1->unlock(); @@ -364,7 +343,7 @@ void tst_QMutex::contendedQMutex() QMutex mutex1, mutex2; QList<QMutexThread *> threads(threadCount); - for (int i = 0; i < threads.count(); ++i) { + for (int i = 0; i < threads.size(); ++i) { threads[i] = new QMutexThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes); threads[i]->start(); } @@ -376,11 +355,11 @@ void tst_QMutex::contendedQMutex() semaphore4.release(threadCount); } - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->done = true; semaphore1.acquire(threadCount); semaphore2.release(threadCount); - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->wait(); qDeleteAll(threads); } @@ -388,7 +367,8 @@ void tst_QMutex::contendedQMutex() class QMutexLockerThread : public QThread { QMutex *mutex1, *mutex2; - int iterations, msleepDuration; + int iterations; + std::chrono::milliseconds msleepDuration; bool use2mutexes; public: bool done; @@ -406,8 +386,8 @@ public: { QMutexLocker locker1(mutex1); QMutexLocker locker2(use2mutexes ? mutex2 : 0); - if (msleepDuration >= 0) - msleep(msleepDuration); + if (msleepDuration >= 0ms) + sleep(msleepDuration); } QThread::yieldCurrentThread(); @@ -427,7 +407,7 @@ void tst_QMutex::contendedQMutexLocker() QMutex mutex1, mutex2; QList<QMutexLockerThread *> threads(threadCount); - for (int i = 0; i < threads.count(); ++i) { + for (int i = 0; i < threads.size(); ++i) { threads[i] = new QMutexLockerThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes); threads[i]->start(); } @@ -439,14 +419,15 @@ void tst_QMutex::contendedQMutexLocker() semaphore4.release(threadCount); } - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->done = true; semaphore1.acquire(threadCount); semaphore2.release(threadCount); - for (int i = 0; i < threads.count(); ++i) + for (int i = 0; i < threads.size(); ++i) threads[i]->wait(); qDeleteAll(threads); } QTEST_MAIN(tst_QMutex) -#include "tst_qmutex.moc" + +#include "tst_bench_qmutex.moc" diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt b/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt index 79d3a2f879..b56f484b48 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from qreadwritelock.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_bench_qreadwritelock Binary: @@ -6,11 +7,8 @@ qt_internal_add_benchmark(tst_bench_qreadwritelock SOURCES - tst_qreadwritelock.cpp - PUBLIC_LIBRARIES + tst_bench_qreadwritelock.cpp + LIBRARIES Qt::CorePrivate Qt::Test ) - -#### Keys ignored in scope 1:.:.:qreadwritelock.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp index 1018dae878..e63ac6573f 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp @@ -1,39 +1,12 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QtCore> #include <QTest> #include <mutex> #if __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 @@ -46,6 +19,49 @@ struct LockerWrapper : T } }; +struct QRecursiveReadWriteLock : QReadWriteLock +{ + QRecursiveReadWriteLock() : QReadWriteLock(Recursive) {} +}; + +template <typename T, size_t N> + // requires N = 2^M for some Integral M >= 0 +struct Recursive +{ + Recursive<T, N/2> r1, r2; + + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...args) + : r1(args...), + r2(args...) + {} +}; + +template <typename T> +struct Recursive<T, 1> +{ + T t; + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...args) + : t(args...) {} +}; + +template <typename T> +struct Recursive<T, 0> +{ + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...) {} +}; + +template <size_t N> +using QRecursiveReadLocker = Recursive<QReadLocker, N>; + +template <size_t N> +using QRecursiveWriteLocker = Recursive<QWriteLocker, N>; + int threadCount; class tst_QReadWriteLock : public QObject @@ -107,6 +123,15 @@ void tst_QReadWriteLock::uncontended_data() << FunctionPtrHolder(testUncontended<QReadWriteLock, QReadLocker>); QTest::newRow("QReadWriteLock, write") << FunctionPtrHolder(testUncontended<QReadWriteLock, QWriteLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, %s, recursive: %d", "read", n) \ + << FunctionPtrHolder(testUncontended<QRecursiveReadWriteLock, QRecursiveReadLocker<n>>); \ + QTest::addRow("QReadWriteLock, %s, recursive: %d", "write", n) \ + << FunctionPtrHolder(testUncontended<QRecursiveReadWriteLock, QRecursiveWriteLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testUncontended<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex @@ -174,6 +199,13 @@ void tst_QReadWriteLock::readOnly_data() QTest::newRow("nothing") << FunctionPtrHolder(testReadOnly<int, FakeLock>); QTest::newRow("QMutex") << FunctionPtrHolder(testReadOnly<QMutex, QMutexLocker<QMutex>>); QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testReadOnly<QReadWriteLock, QReadLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, recursive: %d", n) \ + << FunctionPtrHolder(testReadOnly<QRecursiveReadWriteLock, QRecursiveReadLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testReadOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex @@ -235,6 +267,13 @@ void tst_QReadWriteLock::writeOnly_data() // QTest::newRow("nothing") << FunctionPtrHolder(testWriteOnly<int, FakeLock>); QTest::newRow("QMutex") << FunctionPtrHolder(testWriteOnly<QMutex, QMutexLocker<QMutex>>); QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testWriteOnly<QReadWriteLock, QWriteLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, recursive: %d", n) \ + << FunctionPtrHolder(testWriteOnly<QRecursiveReadWriteLock, QRecursiveWriteLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testWriteOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex @@ -256,4 +295,4 @@ void tst_QReadWriteLock::writeOnly() } QTEST_MAIN(tst_QReadWriteLock) -#include "tst_qreadwritelock.moc" +#include "tst_bench_qreadwritelock.moc" diff --git a/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt b/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt index 7b0c81891e..1353eaa2c4 100644 --- a/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from qthreadpool.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_bench_qthreadpool Binary: @@ -6,10 +7,7 @@ qt_internal_add_benchmark(tst_bench_qthreadpool SOURCES - tst_qthreadpool.cpp - PUBLIC_LIBRARIES + tst_bench_qthreadpool.cpp + LIBRARIES Qt::Test ) - -#### Keys ignored in scope 1:.:.:qthreadpool.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp b/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp new file mode 100644 index 0000000000..e7e5636307 --- /dev/null +++ b/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp @@ -0,0 +1,55 @@ +// Copyright (C) 2013 David Faure <david.faure@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <qtest.h> +#include <QtCore> + +class tst_QThreadPool : public QObject +{ + Q_OBJECT + +public: + tst_QThreadPool(); + ~tst_QThreadPool(); + +private slots: + void startRunnables(); + void activeThreadCount(); +}; + +tst_QThreadPool::tst_QThreadPool() +{ +} + +tst_QThreadPool::~tst_QThreadPool() +{ +} + +class NoOpRunnable : public QRunnable +{ +public: + void run() override { + } +}; + +void tst_QThreadPool::startRunnables() +{ + QThreadPool threadPool; + threadPool.setMaxThreadCount(10); + QBENCHMARK { + threadPool.start(new NoOpRunnable()); + } +} + +void tst_QThreadPool::activeThreadCount() +{ + QThreadPool threadPool; + threadPool.start(new NoOpRunnable()); + QBENCHMARK { + QVERIFY(threadPool.activeThreadCount() <= 10); + } +} + +QTEST_MAIN(tst_QThreadPool) + +#include "tst_bench_qthreadpool.moc" diff --git a/tests/benchmarks/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/benchmarks/corelib/thread/qthreadpool/tst_qthreadpool.cpp deleted file mode 100644 index 10928e3afc..0000000000 --- a/tests/benchmarks/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 David Faure <david.faure@kdab.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <qtest.h> -#include <QtCore> - -class tst_QThreadPool : public QObject -{ - Q_OBJECT - -public: - tst_QThreadPool(); - ~tst_QThreadPool(); - -private slots: - void startRunnables(); - void activeThreadCount(); -}; - -tst_QThreadPool::tst_QThreadPool() -{ -} - -tst_QThreadPool::~tst_QThreadPool() -{ -} - -class NoOpRunnable : public QRunnable -{ -public: - void run() override { - } -}; - -void tst_QThreadPool::startRunnables() -{ - QThreadPool threadPool; - threadPool.setMaxThreadCount(10); - QBENCHMARK { - threadPool.start(new NoOpRunnable()); - } -} - -void tst_QThreadPool::activeThreadCount() -{ - QThreadPool threadPool; - threadPool.start(new NoOpRunnable()); - QBENCHMARK { - QVERIFY(threadPool.activeThreadCount() <= 10); - } -} - -QTEST_MAIN(tst_QThreadPool) -#include "tst_qthreadpool.moc" diff --git a/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt b/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt index 78ac072d35..916a2d8dee 100644 --- a/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from qthreadstorage.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_bench_qthreadstorage Binary: @@ -6,10 +7,7 @@ qt_internal_add_benchmark(tst_bench_qthreadstorage SOURCES - tst_qthreadstorage.cpp - PUBLIC_LIBRARIES + tst_bench_qthreadstorage.cpp + LIBRARIES Qt::Test ) - -#### Keys ignored in scope 1:.:.:qthreadstorage.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp b/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp new file mode 100644 index 0000000000..47db215465 --- /dev/null +++ b/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp @@ -0,0 +1,85 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <qtest.h> +#include <QtCore> + +QThreadStorage<int *> dummy[8]; + +QThreadStorage<QString *> tls1; + +class tst_QThreadStorage : public QObject +{ + Q_OBJECT + +public: + tst_QThreadStorage(); + virtual ~tst_QThreadStorage(); + +public slots: + void init(); + void cleanup(); + +private slots: + void construct(); + void get(); + void set(); +}; + +tst_QThreadStorage::tst_QThreadStorage() +{ +} + +tst_QThreadStorage::~tst_QThreadStorage() +{ +} + +void tst_QThreadStorage::init() +{ + dummy[1].setLocalData(new int(5)); + dummy[2].setLocalData(new int(4)); + dummy[3].setLocalData(new int(3)); + tls1.setLocalData(new QString()); +} + +void tst_QThreadStorage::cleanup() +{ +} + +void tst_QThreadStorage::construct() +{ + QBENCHMARK { + QThreadStorage<int *> ts; + } +} + + +void tst_QThreadStorage::get() +{ + QThreadStorage<int *> ts; + ts.setLocalData(new int(45)); + + int count = 0; + QBENCHMARK { + int *i = ts.localData(); + count += *i; + } + QVERIFY(count > 0); + ts.setLocalData(0); +} + +void tst_QThreadStorage::set() +{ + QThreadStorage<int *> ts; + + int count = 0; + QBENCHMARK { + ts.setLocalData(new int(count)); + count++; + } + ts.setLocalData(0); +} + +QTEST_MAIN(tst_QThreadStorage) + +#include "tst_bench_qthreadstorage.moc" diff --git a/tests/benchmarks/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp b/tests/benchmarks/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp deleted file mode 100644 index c8f9f23c76..0000000000 --- a/tests/benchmarks/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <qtest.h> -#include <QtCore> - -QThreadStorage<int *> dummy[8]; - -QThreadStorage<QString *> tls1; - -class tst_QThreadStorage : public QObject -{ - Q_OBJECT - -public: - tst_QThreadStorage(); - virtual ~tst_QThreadStorage(); - -public slots: - void init(); - void cleanup(); - -private slots: - void construct(); - void get(); - void set(); -}; - -tst_QThreadStorage::tst_QThreadStorage() -{ -} - -tst_QThreadStorage::~tst_QThreadStorage() -{ -} - -void tst_QThreadStorage::init() -{ - dummy[1].setLocalData(new int(5)); - dummy[2].setLocalData(new int(4)); - dummy[3].setLocalData(new int(3)); - tls1.setLocalData(new QString()); -} - -void tst_QThreadStorage::cleanup() -{ -} - -void tst_QThreadStorage::construct() -{ - QBENCHMARK { - QThreadStorage<int *> ts; - } -} - - -void tst_QThreadStorage::get() -{ - QThreadStorage<int *> ts; - ts.setLocalData(new int(45)); - - int count = 0; - QBENCHMARK { - int *i = ts.localData(); - count += *i; - } - ts.setLocalData(0); -} - -void tst_QThreadStorage::set() -{ - QThreadStorage<int *> ts; - - int count = 0; - QBENCHMARK { - ts.setLocalData(new int(count)); - count++; - } - ts.setLocalData(0); -} - - -QTEST_MAIN(tst_QThreadStorage) -#include "tst_qthreadstorage.moc" diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt b/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt index 68c6e88342..c7a15f9e8a 100644 --- a/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from qwaitcondition.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_bench_qwaitcondition Binary: @@ -6,10 +7,7 @@ qt_internal_add_benchmark(tst_bench_qwaitcondition SOURCES - tst_qwaitcondition.cpp - PUBLIC_LIBRARIES + tst_bench_qwaitcondition.cpp + LIBRARIES Qt::Test ) - -#### Keys ignored in scope 1:.:.:qwaitcondition.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp b/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp new file mode 100644 index 0000000000..0abd53fc93 --- /dev/null +++ b/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp @@ -0,0 +1,174 @@ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtCore/QtCore> +#include <QTest> + +#include <math.h> +#include <condition_variable> +#include <mutex> + +#include <limits.h> + +using namespace std::chrono_literals; + +class tst_QWaitCondition : public QObject +{ + Q_OBJECT + +public: + tst_QWaitCondition() + { + } + +private slots: + void oscillate_QWaitCondition_QMutex_data() { oscillate_mutex_data(); } + void oscillate_QWaitCondition_QMutex(); + void oscillate_QWaitCondition_QReadWriteLock_data() { oscillate_mutex_data(); } + void oscillate_QWaitCondition_QReadWriteLock(); + void oscillate_std_condition_variable_std_mutex_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_std_mutex(); + void oscillate_std_condition_variable_any_QMutex_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_any_QMutex(); + void oscillate_std_condition_variable_any_QReadWriteLock_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_any_QReadWriteLock(); + +private: + void oscillate_mutex_data(); +}; + + +int turn; +const int threadCount = 10; +QWaitCondition cond; +std::condition_variable cv; +std::condition_variable_any cva; + +template <typename Cond> +Cond *get(); + +template <> std::condition_variable *get() { return &cv; } +template <> std::condition_variable_any *get() { return &cva; } + +template <class Cond, class Mutex, class Locker> +class OscillateThread : public QThread +{ +public: + Mutex *mutex; + int m_threadid; + unsigned long timeout; + + void run() override + { + for (int count = 0; count < 5000; ++count) { + Locker lock(*mutex); + while (m_threadid != turn) { + if (timeout == ULONG_MAX) + get<Cond>()->wait(lock); + else if (timeout == 0) // Windows doesn't unlock the mutex with a zero timeout + get<Cond>()->wait_for(lock, 1ns); + else + get<Cond>()->wait_for(lock, timeout * 1ms); + } + turn = (turn+1) % threadCount; + get<Cond>()->notify_all(); + } + } +}; + +template <class Mutex, class Locker> +class OscillateThread<QWaitCondition, Mutex, Locker> : public QThread +{ +public: + Mutex *mutex; + int m_threadid; + unsigned long timeout; + + void run() override + { + for (int count = 0; count < 5000; ++count) { + + Locker lock(mutex); + while (m_threadid != turn) { + cond.wait(mutex, timeout); + } + turn = (turn+1) % threadCount; + cond.wakeAll(); + } + } +}; + +template <class Cond, class Mutex, class Locker> +void oscillate(unsigned long timeout) { + + OscillateThread<Cond, 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<unsigned long>("timeout"); + + QTest::newRow("0") << 0ul; + QTest::newRow("1") << 1ul; + QTest::newRow("1000") << 1000ul; + QTest::newRow("forever") << ULONG_MAX; +} + +void tst_QWaitCondition::oscillate_QWaitCondition_QMutex() +{ + QFETCH(unsigned long, timeout); + oscillate<QWaitCondition, QMutex, QMutexLocker<QMutex>>(timeout); +} + +void tst_QWaitCondition::oscillate_QWaitCondition_QReadWriteLock() +{ + QFETCH(unsigned long, timeout); + oscillate<QWaitCondition, QReadWriteLock, QWriteLocker>(timeout); +} + +void tst_QWaitCondition::oscillate_std_condition_variable_std_mutex() +{ + QFETCH(unsigned long, timeout); + oscillate<std::condition_variable, std::mutex, std::unique_lock<std::mutex>>(timeout); +} + + +void tst_QWaitCondition::oscillate_std_condition_variable_any_QMutex() +{ + QFETCH(unsigned long, timeout); + oscillate<std::condition_variable_any, QMutex, std::unique_lock<QMutex>>(timeout); +} + + +void tst_QWaitCondition::oscillate_std_condition_variable_any_QReadWriteLock() +{ + QFETCH(unsigned long, timeout); + + struct WriteLocker : QWriteLocker { + // adapt to BasicLockable + explicit WriteLocker(QReadWriteLock &m) : QWriteLocker{&m} {} + void lock() { relock(); } + }; + + oscillate<std::condition_variable_any, QReadWriteLock, WriteLocker>(timeout); +} + +QTEST_MAIN(tst_QWaitCondition) + +#include "tst_bench_qwaitcondition.moc" diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp b/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp deleted file mode 100644 index b87da463dc..0000000000 --- a/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp +++ /dev/null @@ -1,129 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtCore/QtCore> -#include <QTest> - -#include <math.h> - - -class tst_QWaitCondition : public QObject -{ - Q_OBJECT - -public: - tst_QWaitCondition() - { - } - -private slots: - void oscillate_mutex_data(); - void oscillate_mutex(); - void oscillate_writelock_data(); - void oscillate_writelock(); -}; - - -int turn; -const int threadCount = 10; -QWaitCondition cond; - -template <class Mutex, class Locker> -class OscillateThread : public QThread -{ -public: - Mutex *mutex; - int m_threadid; - int timeout; - - void run() override - { - for (int count = 0; count < 5000; ++count) { - - Locker lock(mutex); - while (m_threadid != turn) { - cond.wait(mutex, timeout); - } - turn = (turn+1) % threadCount; - cond.wakeAll(); - } - } -}; - -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<unsigned long>("timeout"); - - QTest::newRow("0") << 0ul; - QTest::newRow("1") << 1ul; - QTest::newRow("1000") << 1000ul; - QTest::newRow("forever") << ULONG_MAX; -} - -void tst_QWaitCondition::oscillate_mutex() -{ - QFETCH(unsigned long, timeout); - oscillate<QMutex, QMutexLocker<QMutex>>(timeout); -} - -void tst_QWaitCondition::oscillate_writelock_data() -{ - oscillate_mutex_data(); -} - -void tst_QWaitCondition::oscillate_writelock() -{ - QFETCH(unsigned long, timeout); - oscillate<QReadWriteLock, QWriteLocker>(timeout); -} - - -QTEST_MAIN(tst_QWaitCondition) -#include "tst_qwaitcondition.moc" |