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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module 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 <QtTest/QtTest>
#include <QtCore/QThread>
#include <QtCore/QAtomicInt>
#include <QtCore/QMutexLocker>
#include <QtCore/QMutex>
#include <QtGui/QVector3D>
#include <QtGui/QMatrix4x4>
#include <QtCore/QElapsedTimer>
#include <QtCore/QTimer>
#include <Qt3DCore/private/qaspectjobmanager_p.h>
#include <Qt3DCore/private/qabstractaspectjobmanager_p.h>
#include <Qt3DCore/private/qthreadpooler_p.h>
#include <Qt3DCore/qaspectjob.h>
#include <Qt3DCore/qt3dcore_global.h>
#include <qmath.h>
// Add DEFINES += QT_BUILD_INTERNAL at least to Qt3d's core.pro
// when running these tests. It makes QAspectJobManager available.
class tst_ThreadPooler : public QObject
{
Q_OBJECT
public:
tst_ThreadPooler() {}
~tst_ThreadPooler() {}
private:
Qt3DCore::QAspectJobManager *m_jobManager;
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void defaultPerThread();
void defaultAspectQueue();
void doubleAspectQueue();
void dependencyAspectQueue();
void massTest();
void perThreadUniqueCall();
};
typedef Qt3DCore::QAspectJobManager JobManager;
typedef void (*TestFunction)(QAtomicInt *, int *);
typedef void (*MassFunction)(QVector3D *data);
void perThreadFunction(void *arg)
{
((QAtomicInt *)arg)->ref();
}
// General test AspectJob
class TestAspectJob : public Qt3DCore::QAspectJob
{
public:
TestAspectJob(TestFunction func, QAtomicInt *counter, int *value);
void setMutex(QMutex *mutex);
void run() override;
private:
TestFunction m_func;
QAtomicInt *m_counter;
int *m_value;
QMutex *m_mutex;
};
TestAspectJob::TestAspectJob(TestFunction func, QAtomicInt *counter, int *value)
: m_func(func),
m_counter(counter),
m_value(value)
{
}
void TestAspectJob::setMutex(QMutex *mutex)
{
m_mutex = mutex;
}
void TestAspectJob::run()
{
m_func(m_counter, m_value);
}
// Mass test AspectJob
class MassAspectJob : public Qt3DCore::QAspectJob
{
public:
MassAspectJob(MassFunction func, QVector3D *data);
void run() override;
private:
MassFunction m_func;
QVector3D *m_data;
};
struct PerThreadUniqueData
{
};
MassAspectJob::MassAspectJob(MassFunction func, QVector3D *data)
: m_func(func),
m_data(data)
{
}
void MassAspectJob::run()
{
m_func(m_data);
}
void incrementFunctionCallCounter(QAtomicInt *counter, int *value)
{
Q_UNUSED(value);
counter->ref();
}
void add2(QAtomicInt *counter, int *value)
{
Q_UNUSED(counter);
// Sleep for a while so that we see that multiply task really
// wait for us
QThread::currentThread()->msleep(400);
*value = *value + 2;
}
void multiplyBy2(QAtomicInt *counter, int *value)
{
Q_UNUSED(counter);
*value = *value * 2;
}
void massTestFunction(QVector3D *data)
{
QVector3D point(4.5f, 4.5f, 4.5f);
QMatrix4x4 matrix;
matrix.lookAt(QVector3D(10.0f, 1.5f, 2.0f), QVector3D(1.0f, -1.0f, 1.0f),
QVector3D(0.0f, 0.0f, 1.0f));
QVector3D result = matrix.map(point);
data->setX(result.x());
data->setY(result.y());
data->setZ(result.z());
}
void tst_ThreadPooler::initTestCase()
{
m_jobManager = new JobManager(nullptr);
}
void tst_ThreadPooler::cleanupTestCase()
{
delete m_jobManager;
}
void tst_ThreadPooler::defaultPerThread()
{
// GIVEN
QAtomicInt callCounter;
int maxThreadCount = QThread::idealThreadCount();
callCounter.storeRelaxed(0);
// WHEN
m_jobManager->waitForPerThreadFunction(perThreadFunction, &callCounter);
// THEN
QVERIFY(maxThreadCount == callCounter.loadRelaxed());
}
void tst_ThreadPooler::defaultAspectQueue()
{
// GIVEN
QAtomicInt callCounter;
int value = 0; // Not used in this test
QVector<QSharedPointer<Qt3DCore::QAspectJob> > jobList;
callCounter.storeRelaxed(0);
const int jobCount = 5;
// WHEN
for (int i = 0; i < jobCount; i++) {
QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter,
&callCounter, &value));
jobList.append(job);
}
m_jobManager->enqueueJobs(jobList);
m_jobManager->waitForAllJobs();
// THEN
QVERIFY(jobCount == callCounter.loadRelaxed());
}
/*
* Feeds two list of jobs to queue. The pooler should be able to add jobs on
* the second list to execution. Single call to wait finish.
*/
void tst_ThreadPooler::doubleAspectQueue()
{
// GIVEN
QAtomicInt callCounter;
int value = 0; // Not used in this test
QVector<QSharedPointer<Qt3DCore::QAspectJob> > jobList;
callCounter.storeRelaxed(0);
const int jobCount = 3;
// WHEN
for (int i = 0; i < jobCount; i++) {
QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter,
&callCounter, &value));
jobList.append(job);
}
m_jobManager->enqueueJobs(jobList);
QVector<QSharedPointer<Qt3DCore::QAspectJob> > jobList2;
for (int i = 0; i < jobCount; i++) {
QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter,
&callCounter, &value));
jobList2.append(job);
}
m_jobManager->enqueueJobs(jobList2);
m_jobManager->waitForAllJobs();
// THEN
QVERIFY(jobCount * 2 == callCounter.loadRelaxed());
}
/*
* Default test for jobs that have dependencies.
*/
void tst_ThreadPooler::dependencyAspectQueue()
{
// GIVEN
QAtomicInt callCounter; // Not used in this test
int value = 2;
QVector<QSharedPointer<Qt3DCore::QAspectJob> > jobList;
// WHEN
QSharedPointer<TestAspectJob> job1(new TestAspectJob(add2, &callCounter, &value));
jobList.append(job1);
QSharedPointer<TestAspectJob> job2(new TestAspectJob(multiplyBy2, &callCounter, &value));
job2->addDependency(job1);
jobList.append(job2);
m_jobManager->enqueueJobs(jobList);
m_jobManager->waitForAllJobs();
// THEN
// value should be (2+2)*2 = 8
QVERIFY(value == 8);
}
void tst_ThreadPooler::massTest()
{
// GIVEN
const int mass = 600; // 600
QVector<QSharedPointer<Qt3DCore::QAspectJob> > jobList;
QVector3D data[3 * mass];
// WHEN
QElapsedTimer timer;
timer.start();
for (int i = 0; i < mass; i++) {
QSharedPointer<MassAspectJob> job1(new MassAspectJob(massTestFunction, &(data[i * 3 + 0])));
jobList.append(job1);
QSharedPointer<MassAspectJob> job2(new MassAspectJob(massTestFunction, &(data[i * 3 + 1])));
job2->addDependency(job1);
jobList.append(job2);
QSharedPointer<MassAspectJob> job3(new MassAspectJob(massTestFunction, &(data[i * 3 + 2])));
job3->addDependency(job2);
jobList.append(job3);
}
m_jobManager->enqueueJobs(jobList);
m_jobManager->waitForAllJobs();
// THEN
qDebug() << "timer.elapsed() = " << timer.elapsed() << " ms";
}
class PerThreadUniqueTester {
public:
PerThreadUniqueTester()
{
m_globalAtomic.fetchAndStoreOrdered(0);
m_currentIndex.fetchAndStoreOrdered(0);
}
int currentJobIndex()
{
return m_currentIndex.fetchAndAddOrdered(1);
}
void updateGlobalAtomic(int index)
{
m_globalAtomic.fetchAndAddOrdered(qPow(3, index));
}
quint64 globalAtomicValue() const
{
return m_globalAtomic.loadRelaxed();
}
private:
QAtomicInteger<quint64> m_globalAtomic;
QAtomicInt m_currentIndex;
};
void perThreadFunctionUnique(void *arg)
{
PerThreadUniqueTester *tester = reinterpret_cast<PerThreadUniqueTester *>(arg);
tester->updateGlobalAtomic(tester->currentJobIndex());
}
void tst_ThreadPooler::perThreadUniqueCall()
{
// GIVEN
PerThreadUniqueTester tester;
const int maxThreads = QThread::idealThreadCount();
quint64 maxValue = 0;
for (int i = 0; i < maxThreads; ++i) {
maxValue += qPow(3, i);
}
// WHEN
m_jobManager->waitForPerThreadFunction(perThreadFunctionUnique, &tester);
// THEN
QCOMPARE(maxValue, tester.globalAtomicValue());
}
QTEST_APPLESS_MAIN(tst_ThreadPooler)
#include "tst_threadpooler.moc"
|