/**************************************************************************** ** ** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz ** 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 #include #include #include #include #include #include #include #if __has_include() # include # include # define ONLY_IF_BOOST(x) x #else # define ONLY_IF_BOOST(x) QSKIP("This benchmark requires Boost.SharedPtr.") #endif class tst_QSharedPointer : public QObject { Q_OBJECT private Q_SLOTS: void refAndDeref_null_QSP_int() { refAndDeref>(); } void refAndDeref_null_SSP_int() { refAndDeref>(); } void refAndDeref_null_BSP_int() { ONLY_IF_BOOST(refAndDeref>()); } void refAndDeref_null_QSP_QString() { refAndDeref>(); } void refAndDeref_null_SSP_QString() { refAndDeref>(); } void refAndDeref_null_BSP_QString() { ONLY_IF_BOOST(refAndDeref>()); } void refAndDeref_nonnull_QSP_int() { refAndDeref(QSharedPointer::create(42)); } void refAndDeref_nonnull_SSP_int() { refAndDeref(std::make_shared(42)); } void refAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(refAndDeref(boost::make_shared(42))); } void refAndDeref_nonnull_QSP_QString() { refAndDeref(QSharedPointer::create(QStringLiteral("Hello"))); } void refAndDeref_nonnull_SSP_QString() { refAndDeref(std::make_shared(QStringLiteral("Hello"))); } void refAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(refAndDeref(boost::make_shared(QStringLiteral("Hello")))); } private: template void refAndDeref(SP sp = {}) { QBENCHMARK { [[maybe_unused]] auto copy = sp; } } private Q_SLOTS: void threadedRefAndDeref_null_QSP_int() { threadedRefAndDeref>(); } void threadedRefAndDeref_null_SSP_int() { threadedRefAndDeref>(); } void threadedRefAndDeref_null_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref>()); } void threadedRefAndDeref_null_QSP_QString() { threadedRefAndDeref>(); } void threadedRefAndDeref_null_SSP_QString() { threadedRefAndDeref>(); } void threadedRefAndDeref_null_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref>()); } void threadedRefAndDeref_nonnull_QSP_int() { threadedRefAndDeref(QSharedPointer::create(42)); } void threadedRefAndDeref_nonnull_SSP_int() { threadedRefAndDeref(std::make_shared(42)); } void threadedRefAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared(42))); } void threadedRefAndDeref_nonnull_QSP_QString() { threadedRefAndDeref(QSharedPointer::create(QStringLiteral("Hello"))); } void threadedRefAndDeref_nonnull_SSP_QString() { threadedRefAndDeref(std::make_shared(QStringLiteral("Hello"))); } void threadedRefAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared(QStringLiteral("Hello")))); } private: template void threadedRefAndDeref(SP sp = {}) { std::atomic cancel = false; std::vector threads; const auto numCores = std::max(2U, std::thread::hardware_concurrency()); for (uint i = 0; i < numCores - 1; ++i) { threads.emplace_back([sp, &cancel] { while (!cancel.load(std::memory_order_relaxed)) { for (int i = 0; i < 100; ++i) [[maybe_unused]] auto copy = sp; } }); } const auto join = qScopeGuard([&] { cancel.store(true, std::memory_order_relaxed); for (auto &t : threads) t.join(); }); QBENCHMARK { [[maybe_unused]] auto copy = sp; } } }; QTEST_MAIN(tst_QSharedPointer) #include "tst_bench_shared_ptr.moc"