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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* -*- C++ -*-
This file contains a benchmark for job processing in ThreadWeaver.
SPDX-FileCopyrightText: 2005-2013 Mirko Boehm <mirko@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <numeric>
#include <QCoreApplication>
#include <QList>
#include <QString>
#include <QTest>
#include <QtDebug>
#include <ThreadWeaver/Collection>
#include <ThreadWeaver/Job>
#include <ThreadWeaver/Queueing>
#include <ThreadWeaver/Sequence>
#include <ThreadWeaver/ThreadWeaver>
class AccumulateJob : public ThreadWeaver::Job
{
public:
explicit AccumulateJob()
: m_count(0)
, m_result(0)
{
}
AccumulateJob(const AccumulateJob &a)
: ThreadWeaver::Job()
, m_count(a.m_count)
, m_result(a.m_result)
{
}
void setCount(quint64 count)
{
m_count = count;
}
quint64 result() const
{
return m_result;
}
void payload()
{
std::vector<quint64> numbers(m_count);
std::generate(numbers.begin(), numbers.end(), []() -> quint64 {
static quint64 i = 0;
return i++;
});
m_result = std::accumulate(numbers.begin(), numbers.end(), 0);
}
protected:
void run(ThreadWeaver::JobPointer, ThreadWeaver::Thread *) override
{
payload();
}
private:
quint64 m_count;
quint64 m_result;
};
class QueueBenchmarksTest : public QObject
{
Q_OBJECT
public:
QueueBenchmarksTest();
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void BaselineBenchmark();
void BaselineBenchmark_data();
void BaselineAsJobsBenchmark();
void BaselineAsJobsBenchmark_data();
void IndividualJobsBenchmark();
void IndividualJobsBenchmark_data();
void CollectionsBenchmark();
void CollectionsBenchmark_data();
void SequencesBenchmark();
void SequencesBenchmark_data();
private:
void defaultBenchmarkData(bool singleThreaded);
};
QueueBenchmarksTest::QueueBenchmarksTest()
{
}
void QueueBenchmarksTest::initTestCase()
{
}
void QueueBenchmarksTest::cleanupTestCase()
{
}
/** @brief BaselineBenchmark simply performs the same operations in a loop.
*
* The result amounts to what time the jobs used in the benchmark need to execute without queueing or thread
* synchronization overhead. */
void QueueBenchmarksTest::BaselineBenchmark()
{
QFETCH(int, m);
QFETCH(int, c);
QFETCH(int, b);
QFETCH(int, t);
const int n = c * b;
Q_UNUSED(t); // in this case
QVector<AccumulateJob> jobs(n);
for (int i = 0; i < n; ++i) {
jobs[i].setCount(m);
}
// executeLocal needs to emit similar signals as execute(), to be comparable to the threaded variants.
// BaselineAsJobsBenchmark does that. Compare BaselineAsJobsBenchmark and BaselineBenchmark to evaluate the overhead of executing
// an operation in a job.
QBENCHMARK {
for (int i = 0; i < n; ++i) {
jobs[i].payload();
}
}
}
void QueueBenchmarksTest::BaselineBenchmark_data()
{
defaultBenchmarkData(true);
}
void QueueBenchmarksTest::BaselineAsJobsBenchmark()
{
QFETCH(int, m);
QFETCH(int, c);
QFETCH(int, b);
QFETCH(int, t);
const int n = c * b;
Q_UNUSED(t); // in this case
QVector<AccumulateJob> jobs(n);
for (int i = 0; i < n; ++i) {
jobs[i].setCount(m);
}
QBENCHMARK {
for (int i = 0; i < n; ++i) {
jobs[i].blockingExecute();
}
}
}
void QueueBenchmarksTest::BaselineAsJobsBenchmark_data()
{
defaultBenchmarkData(true);
}
void QueueBenchmarksTest::IndividualJobsBenchmark()
{
QFETCH(int, m);
QFETCH(int, c);
QFETCH(int, b);
QFETCH(int, t);
const int n = c * b;
ThreadWeaver::Queue weaver;
weaver.setMaximumNumberOfThreads(t);
weaver.suspend();
QVector<AccumulateJob> jobs(n);
{
ThreadWeaver::QueueStream stream(&weaver);
for (int i = 0; i < n; ++i) {
jobs[i].setCount(m);
stream << jobs[i];
}
}
QBENCHMARK_ONCE {
weaver.resume();
weaver.finish();
}
}
void QueueBenchmarksTest::IndividualJobsBenchmark_data()
{
defaultBenchmarkData(false);
}
void QueueBenchmarksTest::CollectionsBenchmark()
{
QFETCH(int, m);
QFETCH(int, c);
QFETCH(int, b);
QFETCH(int, t);
const int n = c * b;
ThreadWeaver::Queue weaver;
weaver.setMaximumNumberOfThreads(t);
weaver.suspend();
QVector<AccumulateJob> jobs(n);
// FIXME currently, memory management of the job sequences (they are deleted when they go out of scope)
// is measured as part of the benchmark
qDebug() << b << "blocks" << c << "operations, queueing...";
// queue the jobs blockwise as collections
for (int block = 0; block < b; ++block) {
ThreadWeaver::Collection *collection = new ThreadWeaver::Collection();
for (int operation = 0; operation < c; ++operation) {
const int index = block * b + operation;
jobs[index].setCount(m);
*collection << jobs[index];
}
weaver.stream() << collection;
}
qDebug() << b << "blocks" << c << "operations, executing...";
QBENCHMARK_ONCE {
weaver.resume();
weaver.finish();
}
}
void QueueBenchmarksTest::CollectionsBenchmark_data()
{
defaultBenchmarkData(false);
}
void QueueBenchmarksTest::SequencesBenchmark()
{
QFETCH(int, m);
QFETCH(int, c);
QFETCH(int, b);
QFETCH(int, t);
const int n = c * b;
ThreadWeaver::Queue weaver;
weaver.setMaximumNumberOfThreads(t);
weaver.suspend();
QVector<AccumulateJob> jobs(n);
qDebug() << b << "blocks" << c << "operations, queueing...";
// queue the jobs blockwise as collections
for (int block = 0; block < b; ++block) {
ThreadWeaver::Sequence *sequence = new ThreadWeaver::Sequence();
for (int operation = 0; operation < c; ++operation) {
const int index = block * b + operation;
jobs[index].setCount(m);
*sequence << jobs[index];
}
weaver.stream() << sequence;
}
qDebug() << b << "blocks" << c << "operations, executing...";
QBENCHMARK_ONCE {
weaver.resume();
weaver.finish();
}
}
void QueueBenchmarksTest::SequencesBenchmark_data()
{
defaultBenchmarkData(false);
}
void QueueBenchmarksTest::defaultBenchmarkData(bool singleThreaded)
{
QTest::addColumn<int>("m"); // number of quint64's to accumulate
QTest::addColumn<int>("c"); // operations per block
QTest::addColumn<int>("b"); // number of blocks, number of jobs is b*c
QTest::addColumn<int>("t"); // number of worker threads
const QList<int> threads = singleThreaded ? QList<int>() << 1 : QList<int>() << 1 << 2 << 4 << 8 << 16 << 32 << 64 << 128;
const QList<int> ms = QList<int>() << 1 << 10 << 100 << 1000 << 10000 << 100000;
for (int m : ms) {
for (int t : threads) {
const QString name = QString::fromLatin1("%1 threads, %2 values").arg(t).arg(m);
// newRow expects const char*, but then qstrdup's it in the QTestData constructor. Eeeew.
QTest::newRow(qPrintable(name)) << m << 256 << 256 << t;
}
}
}
QTEST_MAIN(QueueBenchmarksTest)
#include "QueueBenchmarks.moc"
|