summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/tools/qsharedpointer/tst_bench_shared_ptr.cpp
blob: 4430282403a012006b15f95c3e32784498c04fb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/****************************************************************************
**
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@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 <QObject>
#include <QScopeGuard>
#include <QSharedPointer>
#include <QTest>

#include <atomic>
#include <memory>
#include <thread>
#include <vector>

#if __has_include(<boost/shared_ptr.hpp>)
#  include <boost/shared_ptr.hpp>
#  include <boost/make_shared.hpp>
#  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<QSharedPointer<int>>(); }
    void refAndDeref_null_SSP_int() { refAndDeref<std::shared_ptr<int>>(); }
    void refAndDeref_null_BSP_int() { ONLY_IF_BOOST(refAndDeref<boost::shared_ptr<int>>()); }

    void refAndDeref_null_QSP_QString() { refAndDeref<QSharedPointer<QString>>(); }
    void refAndDeref_null_SSP_QString() { refAndDeref<std::shared_ptr<QString>>(); }
    void refAndDeref_null_BSP_QString() { ONLY_IF_BOOST(refAndDeref<boost::shared_ptr<QString>>()); }

    void refAndDeref_nonnull_QSP_int() { refAndDeref(QSharedPointer<int>::create(42)); }
    void refAndDeref_nonnull_SSP_int() { refAndDeref(std::make_shared<int>(42)); }
    void refAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(refAndDeref(boost::make_shared<int>(42))); }

    void refAndDeref_nonnull_QSP_QString() { refAndDeref(QSharedPointer<QString>::create(QStringLiteral("Hello"))); }
    void refAndDeref_nonnull_SSP_QString() { refAndDeref(std::make_shared<QString>(QStringLiteral("Hello"))); }
    void refAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(refAndDeref(boost::make_shared<QString>(QStringLiteral("Hello")))); }

private:
    template <typename SP>
    void refAndDeref(SP sp = {})
    {
        QBENCHMARK {
            [[maybe_unused]] auto copy = sp;
        }
    }

private Q_SLOTS:
    void threadedRefAndDeref_null_QSP_int() { threadedRefAndDeref<QSharedPointer<int>>(); }
    void threadedRefAndDeref_null_SSP_int() { threadedRefAndDeref<std::shared_ptr<int>>(); }
    void threadedRefAndDeref_null_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref<boost::shared_ptr<int>>()); }

    void threadedRefAndDeref_null_QSP_QString() { threadedRefAndDeref<QSharedPointer<QString>>(); }
    void threadedRefAndDeref_null_SSP_QString() { threadedRefAndDeref<std::shared_ptr<QString>>(); }
    void threadedRefAndDeref_null_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref<boost::shared_ptr<QString>>()); }

    void threadedRefAndDeref_nonnull_QSP_int() { threadedRefAndDeref(QSharedPointer<int>::create(42)); }
    void threadedRefAndDeref_nonnull_SSP_int() { threadedRefAndDeref(std::make_shared<int>(42)); }
    void threadedRefAndDeref_nonnull_BSP_int() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared<int>(42))); }

    void threadedRefAndDeref_nonnull_QSP_QString() { threadedRefAndDeref(QSharedPointer<QString>::create(QStringLiteral("Hello"))); }
    void threadedRefAndDeref_nonnull_SSP_QString() { threadedRefAndDeref(std::make_shared<QString>(QStringLiteral("Hello"))); }
    void threadedRefAndDeref_nonnull_BSP_QString() { ONLY_IF_BOOST(threadedRefAndDeref(boost::make_shared<QString>(QStringLiteral("Hello")))); }

private:
    template <typename SP>
    void threadedRefAndDeref(SP sp = {})
    {
        std::atomic<bool> cancel = false;
        std::vector<std::thread> 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"