| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <QtCore/QThread> |
| 6 | #include <QtCore/QAtomicInt> |
| 7 | #include <QtCore/QMutexLocker> |
| 8 | #include <QtCore/QMutex> |
| 9 | #include <QtGui/QVector3D> |
| 10 | #include <QtGui/QMatrix4x4> |
| 11 | #include <QtCore/QElapsedTimer> |
| 12 | #include <QtCore/QTimer> |
| 13 | |
| 14 | #include <Qt3DCore/private/qaspectjobmanager_p.h> |
| 15 | #include <Qt3DCore/private/qabstractaspectjobmanager_p.h> |
| 16 | #include <Qt3DCore/private/qthreadpooler_p.h> |
| 17 | #include <Qt3DCore/qaspectjob.h> |
| 18 | #include <Qt3DCore/qt3dcore_global.h> |
| 19 | #include <qmath.h> |
| 20 | |
| 21 | // Add DEFINES += QT_BUILD_INTERNAL at least to Qt3d's core.pro |
| 22 | // when running these tests. It makes QAspectJobManager available. |
| 23 | |
| 24 | class tst_ThreadPooler : public QObject |
| 25 | { |
| 26 | Q_OBJECT |
| 27 | |
| 28 | public: |
| 29 | tst_ThreadPooler() {} |
| 30 | ~tst_ThreadPooler() {} |
| 31 | |
| 32 | private: |
| 33 | Qt3DCore::QAspectJobManager *m_jobManager; |
| 34 | |
| 35 | private Q_SLOTS: |
| 36 | void initTestCase(); |
| 37 | void cleanupTestCase(); |
| 38 | |
| 39 | void defaultPerThread(); |
| 40 | void defaultAspectQueue(); |
| 41 | void doubleAspectQueue(); |
| 42 | void dependencyAspectQueue(); |
| 43 | void massTest(); |
| 44 | void perThreadUniqueCall(); |
| 45 | }; |
| 46 | |
| 47 | typedef Qt3DCore::QAspectJobManager JobManager; |
| 48 | typedef void (*TestFunction)(QAtomicInt *, int *); |
| 49 | typedef void (*MassFunction)(QVector3D *data); |
| 50 | |
| 51 | void perThreadFunction(void *arg) |
| 52 | { |
| 53 | ((QAtomicInt *)arg)->ref(); |
| 54 | } |
| 55 | |
| 56 | // General test AspectJob |
| 57 | |
| 58 | class TestAspectJob : public Qt3DCore::QAspectJob |
| 59 | { |
| 60 | public: |
| 61 | TestAspectJob(TestFunction func, QAtomicInt *counter, int *value); |
| 62 | |
| 63 | void setMutex(QMutex *mutex); |
| 64 | |
| 65 | void run() override; |
| 66 | |
| 67 | private: |
| 68 | TestFunction m_func; |
| 69 | QAtomicInt *m_counter; |
| 70 | int *m_value; |
| 71 | QMutex *m_mutex; |
| 72 | }; |
| 73 | |
| 74 | TestAspectJob::TestAspectJob(TestFunction func, QAtomicInt *counter, int *value) |
| 75 | : m_func(func), |
| 76 | m_counter(counter), |
| 77 | m_value(value) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void TestAspectJob::setMutex(QMutex *mutex) |
| 82 | { |
| 83 | m_mutex = mutex; |
| 84 | } |
| 85 | |
| 86 | void TestAspectJob::run() |
| 87 | { |
| 88 | m_func(m_counter, m_value); |
| 89 | } |
| 90 | |
| 91 | // Mass test AspectJob |
| 92 | |
| 93 | class MassAspectJob : public Qt3DCore::QAspectJob |
| 94 | { |
| 95 | public: |
| 96 | MassAspectJob(MassFunction func, QVector3D *data); |
| 97 | |
| 98 | void run() override; |
| 99 | |
| 100 | private: |
| 101 | MassFunction m_func; |
| 102 | QVector3D *m_data; |
| 103 | }; |
| 104 | |
| 105 | struct PerThreadUniqueData |
| 106 | { |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | MassAspectJob::MassAspectJob(MassFunction func, QVector3D *data) |
| 111 | : m_func(func), |
| 112 | m_data(data) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | void MassAspectJob::run() |
| 117 | { |
| 118 | m_func(m_data); |
| 119 | } |
| 120 | |
| 121 | void incrementFunctionCallCounter(QAtomicInt *counter, int *value) |
| 122 | { |
| 123 | Q_UNUSED(value); |
| 124 | |
| 125 | counter->ref(); |
| 126 | } |
| 127 | |
| 128 | void add2(QAtomicInt *counter, int *value) |
| 129 | { |
| 130 | Q_UNUSED(counter); |
| 131 | |
| 132 | // Sleep for a while so that we see that multiply task really |
| 133 | // wait for us |
| 134 | QThread::currentThread()->msleep(400); |
| 135 | *value = *value + 2; |
| 136 | } |
| 137 | |
| 138 | void multiplyBy2(QAtomicInt *counter, int *value) |
| 139 | { |
| 140 | Q_UNUSED(counter); |
| 141 | |
| 142 | *value = *value * 2; |
| 143 | } |
| 144 | |
| 145 | void massTestFunction(QVector3D *data) |
| 146 | { |
| 147 | QVector3D point(4.5f, 4.5f, 4.5f); |
| 148 | |
| 149 | QMatrix4x4 matrix; |
| 150 | matrix.lookAt(QVector3D(10.0f, 1.5f, 2.0f), QVector3D(1.0f, -1.0f, 1.0f), |
| 151 | QVector3D(0.0f, 0.0f, 1.0f)); |
| 152 | QVector3D result = matrix.map(point); |
| 153 | data->setX(result.x()); |
| 154 | data->setY(result.y()); |
| 155 | data->setZ(result.z()); |
| 156 | } |
| 157 | |
| 158 | void tst_ThreadPooler::initTestCase() |
| 159 | { |
| 160 | m_jobManager = new JobManager(nullptr); |
| 161 | } |
| 162 | |
| 163 | void tst_ThreadPooler::cleanupTestCase() |
| 164 | { |
| 165 | delete m_jobManager; |
| 166 | } |
| 167 | |
| 168 | void tst_ThreadPooler::defaultPerThread() |
| 169 | { |
| 170 | // GIVEN |
| 171 | QAtomicInt callCounter; |
| 172 | int maxThreadCount = QThread::idealThreadCount(); |
| 173 | callCounter.storeRelaxed(0); |
| 174 | |
| 175 | // WHEN |
| 176 | m_jobManager->waitForPerThreadFunction(perThreadFunction, &callCounter); |
| 177 | |
| 178 | // THEN |
| 179 | QVERIFY(maxThreadCount == callCounter.loadRelaxed()); |
| 180 | } |
| 181 | |
| 182 | void tst_ThreadPooler::defaultAspectQueue() |
| 183 | { |
| 184 | // GIVEN |
| 185 | QAtomicInt callCounter; |
| 186 | int value = 0; // Not used in this test |
| 187 | std::vector<QSharedPointer<Qt3DCore::QAspectJob> > jobList; |
| 188 | callCounter.storeRelaxed(0); |
| 189 | const int jobCount = 5; |
| 190 | |
| 191 | // WHEN |
| 192 | for (int i = 0; i < jobCount; i++) { |
| 193 | QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter, |
| 194 | &callCounter, &value)); |
| 195 | jobList.push_back(job); |
| 196 | } |
| 197 | m_jobManager->enqueueJobs(jobList); |
| 198 | m_jobManager->waitForAllJobs(); |
| 199 | |
| 200 | // THEN |
| 201 | QVERIFY(jobCount == callCounter.loadRelaxed()); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Feeds two list of jobs to queue. The pooler should be able to add jobs on |
| 206 | * the second list to execution. Single call to wait finish. |
| 207 | */ |
| 208 | void tst_ThreadPooler::doubleAspectQueue() |
| 209 | { |
| 210 | // GIVEN |
| 211 | QAtomicInt callCounter; |
| 212 | int value = 0; // Not used in this test |
| 213 | std::vector<QSharedPointer<Qt3DCore::QAspectJob> > jobList; |
| 214 | callCounter.storeRelaxed(0); |
| 215 | const int jobCount = 3; |
| 216 | |
| 217 | // WHEN |
| 218 | for (int i = 0; i < jobCount; i++) { |
| 219 | QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter, |
| 220 | &callCounter, &value)); |
| 221 | jobList.push_back(job); |
| 222 | } |
| 223 | m_jobManager->enqueueJobs(jobList); |
| 224 | |
| 225 | std::vector<QSharedPointer<Qt3DCore::QAspectJob> > jobList2; |
| 226 | for (int i = 0; i < jobCount; i++) { |
| 227 | QSharedPointer<TestAspectJob> job(new TestAspectJob(incrementFunctionCallCounter, |
| 228 | &callCounter, &value)); |
| 229 | jobList2.push_back(job); |
| 230 | } |
| 231 | m_jobManager->enqueueJobs(jobList2); |
| 232 | |
| 233 | m_jobManager->waitForAllJobs(); |
| 234 | |
| 235 | // THEN |
| 236 | QVERIFY(jobCount * 2 == callCounter.loadRelaxed()); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Default test for jobs that have dependencies. |
| 241 | */ |
| 242 | void tst_ThreadPooler::dependencyAspectQueue() |
| 243 | { |
| 244 | // GIVEN |
| 245 | QAtomicInt callCounter; // Not used in this test |
| 246 | int value = 2; |
| 247 | std::vector<QSharedPointer<Qt3DCore::QAspectJob> > jobList; |
| 248 | |
| 249 | // WHEN |
| 250 | QSharedPointer<TestAspectJob> job1(new TestAspectJob(add2, &callCounter, &value)); |
| 251 | jobList.push_back(job1); |
| 252 | QSharedPointer<TestAspectJob> job2(new TestAspectJob(multiplyBy2, &callCounter, &value)); |
| 253 | job2->addDependency(job1); |
| 254 | jobList.push_back(job2); |
| 255 | m_jobManager->enqueueJobs(jobList); |
| 256 | m_jobManager->waitForAllJobs(); |
| 257 | |
| 258 | // THEN |
| 259 | // value should be (2+2)*2 = 8 |
| 260 | QVERIFY(value == 8); |
| 261 | } |
| 262 | |
| 263 | void tst_ThreadPooler::massTest() |
| 264 | { |
| 265 | // GIVEN |
| 266 | const int mass = 600; // 600 |
| 267 | std::vector<QSharedPointer<Qt3DCore::QAspectJob> > jobList; |
| 268 | QVector3D data[3 * mass]; |
| 269 | |
| 270 | // WHEN |
| 271 | QElapsedTimer timer; |
| 272 | timer.start(); |
| 273 | |
| 274 | for (int i = 0; i < mass; i++) { |
| 275 | QSharedPointer<MassAspectJob> job1(new MassAspectJob(massTestFunction, &(data[i * 3 + 0]))); |
| 276 | jobList.push_back(job1); |
| 277 | QSharedPointer<MassAspectJob> job2(new MassAspectJob(massTestFunction, &(data[i * 3 + 1]))); |
| 278 | job2->addDependency(job1); |
| 279 | jobList.push_back(job2); |
| 280 | QSharedPointer<MassAspectJob> job3(new MassAspectJob(massTestFunction, &(data[i * 3 + 2]))); |
| 281 | job3->addDependency(job2); |
| 282 | jobList.push_back(job3); |
| 283 | } |
| 284 | |
| 285 | m_jobManager->enqueueJobs(jobList); |
| 286 | m_jobManager->waitForAllJobs(); |
| 287 | |
| 288 | // THEN |
| 289 | qDebug() << "timer.elapsed() = " << timer.elapsed() << " ms"; |
| 290 | } |
| 291 | |
| 292 | class PerThreadUniqueTester { |
| 293 | |
| 294 | public: |
| 295 | PerThreadUniqueTester() |
| 296 | { |
| 297 | m_globalAtomic.fetchAndStoreOrdered(0); |
| 298 | m_currentIndex.fetchAndStoreOrdered(0); |
| 299 | } |
| 300 | |
| 301 | int currentJobIndex() |
| 302 | { |
| 303 | return m_currentIndex.fetchAndAddOrdered(1); |
| 304 | } |
| 305 | |
| 306 | void updateGlobalAtomic(int index) |
| 307 | { |
| 308 | m_globalAtomic.fetchAndAddOrdered(qPow(3, index)); |
| 309 | } |
| 310 | |
| 311 | quint64 globalAtomicValue() const |
| 312 | { |
| 313 | return m_globalAtomic.loadRelaxed(); |
| 314 | } |
| 315 | |
| 316 | private: |
| 317 | QAtomicInteger<quint64> m_globalAtomic; |
| 318 | QAtomicInt m_currentIndex; |
| 319 | }; |
| 320 | |
| 321 | void perThreadFunctionUnique(void *arg) |
| 322 | { |
| 323 | PerThreadUniqueTester *tester = reinterpret_cast<PerThreadUniqueTester *>(arg); |
| 324 | tester->updateGlobalAtomic(tester->currentJobIndex()); |
| 325 | } |
| 326 | |
| 327 | void tst_ThreadPooler::perThreadUniqueCall() |
| 328 | { |
| 329 | // GIVEN |
| 330 | PerThreadUniqueTester tester; |
| 331 | const int maxThreads = QThread::idealThreadCount(); |
| 332 | quint64 maxValue = 0; |
| 333 | for (int i = 0; i < maxThreads; ++i) { |
| 334 | maxValue += qPow(3, i); |
| 335 | } |
| 336 | |
| 337 | // WHEN |
| 338 | m_jobManager->waitForPerThreadFunction(perThreadFunctionUnique, &tester); |
| 339 | |
| 340 | // THEN |
| 341 | QCOMPARE(maxValue, tester.globalAtomicValue()); |
| 342 | } |
| 343 | |
| 344 | QTEST_APPLESS_MAIN(tst_ThreadPooler) |
| 345 | |
| 346 | #include "tst_threadpooler.moc" |