From 0949ea856f24f2e8f14a4659bcfb1771015db9c8 Mon Sep 17 00:00:00 2001 From: Paul Lemire Date: Thu, 23 Oct 2014 12:12:28 +0200 Subject: QResourcesManager: Added dynamic ArrayAllocation policy Startup memory usage is now 10% of what it was before. Unit tests corrected and benchmarks added. Benchmarks: DynamicArrayPolicy QResourcesManager Change-Id: Idaeedba5156ac2d6a0a494e7399d9dcb141dc81c Reviewed-by: Sean Harmer Reviewed-by: Paul Lemire --- .../arrayresourcesmanager.pro | 10 +- .../dynamicarraypolicy/dynamicarraypolicy.pro | 7 + .../dynamicarraypolicy/tst_dynamicarraypolicy.cpp | 440 +++++++++++++++++++++ .../preallocatedarraypolicy.pro | 7 + .../tst_preallocatedarraypolicy.cpp | 440 +++++++++++++++++++++ .../tst_arrayresourcesmanager.cpp | 440 --------------------- 6 files changed, 898 insertions(+), 446 deletions(-) create mode 100644 tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro create mode 100644 tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp create mode 100644 tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro create mode 100644 tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp delete mode 100644 tests/auto/core/arrayresourcesmanager/tst_arrayresourcesmanager.cpp (limited to 'tests/auto') diff --git a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro b/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro index bc08d264b..cc929575f 100644 --- a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro +++ b/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro @@ -1,7 +1,5 @@ -TARGET = tst_arrayresourcesmanager -CONFIG += testcase -TEMPLATE = app +TEMPLATE = subdirs -SOURCES += tst_arrayresourcesmanager.cpp - -QT += testlib 3dcore +SUBDIRS = \ + dynamicarraypolicy \ + preallocatedarraypolicy diff --git a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro new file mode 100644 index 000000000..db6704cd6 --- /dev/null +++ b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro @@ -0,0 +1,7 @@ +TARGET = tst_dynamicarraypolicy +CONFIG += testcase +TEMPLATE = app + +SOURCES += tst_dynamicarraypolicy.cpp + +QT += testlib 3dcore diff --git a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp new file mode 100644 index 000000000..1b41055b4 --- /dev/null +++ b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp @@ -0,0 +1,440 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +class tst_DynamicArrayPolicy : public QObject +{ + Q_OBJECT +public: + tst_DynamicArrayPolicy() {} + ~tst_DynamicArrayPolicy() {} + +private slots: + void createResourcesManager(); + void acquireResources(); + void getResources(); + void registerResourcesResize(); + void removeResource(); + void resetResource(); + void lookupResource(); + void releaseResource(); + void heavyDutyMultiThreadedAccess(); + void heavyDutyMultiThreadedAccessRelease(); + void maximumNumberOfResources(); +}; + +class tst_ArrayResource +{ +public: + tst_ArrayResource() : m_value(0) + {} + + int m_value; +}; + +typedef Qt3D::QHandle tHandle; +typedef Qt3D::QHandle tHandle4; +typedef Qt3D::QHandle tHandle8; +typedef Qt3D::QHandle tHandle16; + +void tst_DynamicArrayPolicy::createResourcesManager() +{ + Qt3D::QResourcesManager manager16; + Qt3D::QResourcesManager manager4; + Qt3D::QResourcesManager manager8; + QVERIFY(manager16.maxResourcesEntries() == 65535); + QVERIFY(manager8.maxResourcesEntries() == 255); + QVERIFY(manager4.maxResourcesEntries() == 15); +} + +/*! + * Check that the handles returned when a registering resources + * have a correct index and counter. + */ +void tst_DynamicArrayPolicy::acquireResources() +{ + Qt3D::QResourcesManager manager; + + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + } +} + +/*! + * Test that values can be properly retrieved. + */ +void tst_DynamicArrayPolicy::getResources() +{ + + Qt3D::QResourcesManager manager; + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + resources << manager.data(handles.at(i)); + QVERIFY(resources.at(i) != Q_NULLPTR); + resources.at(i)->m_value = i; + } + + for (int i = 0; i < 5; i++) + QVERIFY(manager.data(handles.at(i))->m_value == i); + + // Check that an invalid resource returns NULL + tHandle8 iHandle; + QVERIFY(manager.data(iHandle) == Q_NULLPTR); + +} + +/*! + * Test that when a resize of the data vectors in the manager occurs, + * everything behaves correctly. + */ +void tst_DynamicArrayPolicy::registerResourcesResize() +{ + Qt3D::QResourcesManager manager; + QList handles; + + for (uint i = 0; i < 2; i++) { + handles << manager.acquire(); + manager.data(handles.at(i))->m_value = i + 2; + } + + for (uint i = 0; i < 5; i++) { + handles << manager.acquire(); + manager.data(handles.at(i + 2))->m_value = i + 2 + 5; + } + + for (int i = 0; i < 7; i++) { + QVERIFY(handles.at(i).index() == static_cast(i)); + QVERIFY(handles.at(i).counter() == 1); + if (i < 2) + QVERIFY(manager.data(handles.at(i))->m_value == i + 2); + else + QVERIFY(manager.data(handles.at(i))->m_value == i + 5); + } +} + +/*! + * Checks for the removal of resources. + */ +void tst_DynamicArrayPolicy::removeResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 32; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + } + + manager.release(handles.at(2)); + QVERIFY(manager.data(handles.at(2)) == Q_NULLPTR); + // Triggers QASSERT so commented + // manager.release(handles.at(2)); + + tHandle nHandle = manager.acquire(); + QVERIFY(manager.data(nHandle) != Q_NULLPTR); +} + +/*! + * Checks that reset behaves correctly. + */ +void tst_DynamicArrayPolicy::resetResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } + manager.reset(); + for (uint i = 0; i < 5; i++) { + QVERIFY(manager.data(handles.at(i)) == Q_NULLPTR); + } + handles.clear(); + for (uint i = 0; i < 5; i++) + handles << manager.acquire(); + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + QVERIFY(manager.data(handles.at(i))->m_value != 4); + } +} + +void tst_DynamicArrayPolicy::lookupResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } + + tHandle16 t = manager.lookupHandle(2); + QVERIFY(t.handle() == 0); + QVERIFY(manager.data(t) == Q_NULLPTR); + tst_ArrayResource *resource = manager.getOrCreateResource(2); + QVERIFY(resource != Q_NULLPTR); + t = manager.lookupHandle(2); + QVERIFY(manager.data(t) == manager.lookupResource(2)); + QVERIFY(t == manager.getOrAcquireHandle(2)); + QVERIFY(resource == manager.getOrCreateResource(2)); + QVERIFY(manager.data(t) == resource); +} + +void tst_DynamicArrayPolicy::releaseResource() +{ + Qt3D::QResourcesManager manager; + QList resources; + + for (int i = 0; i < 5; i++) { + resources << manager.getOrCreateResource(i); + } + + for (int i = 0; i < 5; i++) { + QVERIFY(resources.at(i) == manager.lookupResource(i)); + } + + for (int i = 0; i < 5; i++) { + manager.releaseResource(i); + QVERIFY(manager.lookupResource(i) == Q_NULLPTR); + } +} + +class tst_Thread : public QThread +{ + Q_OBJECT +public: + + typedef Qt3D::QResourcesManager Manager; + + tst_Thread() + : QThread() + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + i++; + QVERIFY(r != Q_NULLPTR); + Manager::WriteLocker lock(m_manager); + r->m_value++; + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; +}; + +void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccess() +{ + tst_Thread::Manager *manager = new tst_Thread::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int i = 0; i < iterations; i++) { + tst_Thread *thread = new tst_Thread(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) != Q_NULLPTR); + QVERIFY(manager->lookupResource(i)->m_value = iterations); + } + + qDeleteAll(threads); + delete manager; +} + +class tst_Thread2 : public QThread +{ + Q_OBJECT +public: + + typedef Qt3D::QResourcesManager Manager; + + tst_Thread2(int releaseAbove = 7) + : QThread() + , m_releaseAbove(releaseAbove) + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + QVERIFY(r != Q_NULLPTR); + Manager::WriteLocker lock(m_manager); + r->m_value++; + if (r->m_value > m_releaseAbove) + m_manager->releaseResource(i); + i++; + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; + int m_releaseAbove; +}; + +void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccessRelease() +{ + tst_Thread2::Manager *manager = new tst_Thread2::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int u = 0; u < 2; u++) { + + for (int i = 0; i < iterations; i++) { + tst_Thread2 *thread = new tst_Thread2(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) == Q_NULLPTR); + } + + qDeleteAll(threads); + threads.clear(); + } + + delete manager; +} + +void tst_DynamicArrayPolicy::maximumNumberOfResources() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + QCOMPARE(tHandle16::maxIndex(), (uint)manager.maxResourcesEntries()); + + for (int i = 0; i < manager.maxResourcesEntries(); i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } +} + +QTEST_APPLESS_MAIN(tst_DynamicArrayPolicy) + +#include "tst_dynamicarraypolicy.moc" diff --git a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro new file mode 100644 index 000000000..f21c0a140 --- /dev/null +++ b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro @@ -0,0 +1,7 @@ +TARGET = tst_preallocatedarraypolicy +CONFIG += testcase +TEMPLATE = app + +SOURCES += tst_preallocatedarraypolicy.cpp + +QT += testlib 3dcore diff --git a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp new file mode 100644 index 000000000..b9c18d95e --- /dev/null +++ b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp @@ -0,0 +1,440 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +class tst_PreallocatedArrayPolicy : public QObject +{ + Q_OBJECT +public: + tst_PreallocatedArrayPolicy() {} + ~tst_PreallocatedArrayPolicy() {} + +private slots: + void createResourcesManager(); + void acquireResources(); + void getResources(); + void registerResourcesResize(); + void removeResource(); + void resetResource(); + void lookupResource(); + void releaseResource(); + void heavyDutyMultiThreadedAccess(); + void heavyDutyMultiThreadedAccessRelease(); + void maximumNumberOfResources(); +}; + +class tst_ArrayResource +{ +public: + tst_ArrayResource() : m_value(0) + {} + + int m_value; +}; + +typedef Qt3D::QHandle tHandle; +typedef Qt3D::QHandle tHandle4; +typedef Qt3D::QHandle tHandle8; +typedef Qt3D::QHandle tHandle16; + +void tst_PreallocatedArrayPolicy::createResourcesManager() +{ + Qt3D::QResourcesManager manager16; + Qt3D::QResourcesManager manager4; + Qt3D::QResourcesManager manager8; + QVERIFY(manager16.maxResourcesEntries() == 65535); + QVERIFY(manager8.maxResourcesEntries() == 255); + QVERIFY(manager4.maxResourcesEntries() == 15); +} + +/*! + * Check that the handles returned when a registering resources + * have a correct index and counter. + */ +void tst_PreallocatedArrayPolicy::acquireResources() +{ + Qt3D::QResourcesManager manager; + + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + } +} + +/*! + * Test that values can be properly retrieved. + */ +void tst_PreallocatedArrayPolicy::getResources() +{ + + Qt3D::QResourcesManager manager; + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + resources << manager.data(handles.at(i)); + QVERIFY(resources.at(i) != Q_NULLPTR); + resources.at(i)->m_value = i; + } + + for (int i = 0; i < 5; i++) + QVERIFY(manager.data(handles.at(i))->m_value == i); + + // Check that an invalid resource returns NULL + tHandle8 iHandle; + QVERIFY(manager.data(iHandle) == Q_NULLPTR); + +} + +/*! + * Test that when a resize of the data vectors in the manager occurs, + * everything behaves correctly. + */ +void tst_PreallocatedArrayPolicy::registerResourcesResize() +{ + Qt3D::QResourcesManager manager; + QList handles; + + for (uint i = 0; i < 2; i++) { + handles << manager.acquire(); + manager.data(handles.at(i))->m_value = i + 2; + } + + for (uint i = 0; i < 5; i++) { + handles << manager.acquire(); + manager.data(handles.at(i + 2))->m_value = i + 2 + 5; + } + + for (int i = 0; i < 7; i++) { + QVERIFY(handles.at(i).index() == static_cast(i)); + QVERIFY(handles.at(i).counter() == 1); + if (i < 2) + QVERIFY(manager.data(handles.at(i))->m_value == i + 2); + else + QVERIFY(manager.data(handles.at(i))->m_value == i + 5); + } +} + +/*! + * Checks for the removal of resources. + */ +void tst_PreallocatedArrayPolicy::removeResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 32; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + } + + manager.release(handles.at(2)); + QVERIFY(manager.data(handles.at(2)) == Q_NULLPTR); + // Triggers QASSERT so commented + // manager.release(handles.at(2)); + + tHandle nHandle = manager.acquire(); + QVERIFY(manager.data(nHandle) != Q_NULLPTR); +} + +/*! + * Checks that reset behaves correctly. + */ +void tst_PreallocatedArrayPolicy::resetResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } + manager.reset(); + for (uint i = 0; i < 5; i++) { + QVERIFY(manager.data(handles.at(i)) == Q_NULLPTR); + } + handles.clear(); + for (uint i = 0; i < 5; i++) + handles << manager.acquire(); + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + QVERIFY(manager.data(handles.at(i))->m_value != 4); + } +} + +void tst_PreallocatedArrayPolicy::lookupResource() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } + + tHandle16 t = manager.lookupHandle(2); + QVERIFY(t.handle() == 0); + QVERIFY(manager.data(t) == Q_NULLPTR); + tst_ArrayResource *resource = manager.getOrCreateResource(2); + QVERIFY(resource != Q_NULLPTR); + t = manager.lookupHandle(2); + QVERIFY(manager.data(t) == manager.lookupResource(2)); + QVERIFY(t == manager.getOrAcquireHandle(2)); + QVERIFY(resource == manager.getOrCreateResource(2)); + QVERIFY(manager.data(t) == resource); +} + +void tst_PreallocatedArrayPolicy::releaseResource() +{ + Qt3D::QResourcesManager manager; + QList resources; + + for (int i = 0; i < 5; i++) { + resources << manager.getOrCreateResource(i); + } + + for (int i = 0; i < 5; i++) { + QVERIFY(resources.at(i) == manager.lookupResource(i)); + } + + for (int i = 0; i < 5; i++) { + manager.releaseResource(i); + QVERIFY(manager.lookupResource(i) == Q_NULLPTR); + } +} + +class tst_Thread : public QThread +{ + Q_OBJECT +public: + + typedef Qt3D::QResourcesManager Manager; + + tst_Thread() + : QThread() + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + i++; + QVERIFY(r != Q_NULLPTR); + Manager::WriteLocker lock(m_manager); + r->m_value++; + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; +}; + +void tst_PreallocatedArrayPolicy::heavyDutyMultiThreadedAccess() +{ + tst_Thread::Manager *manager = new tst_Thread::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int i = 0; i < iterations; i++) { + tst_Thread *thread = new tst_Thread(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) != Q_NULLPTR); + QVERIFY(manager->lookupResource(i)->m_value = iterations); + } + + qDeleteAll(threads); + delete manager; +} + +class tst_Thread2 : public QThread +{ + Q_OBJECT +public: + + typedef Qt3D::QResourcesManager Manager; + + tst_Thread2(int releaseAbove = 7) + : QThread() + , m_releaseAbove(releaseAbove) + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + QVERIFY(r != Q_NULLPTR); + Manager::WriteLocker lock(m_manager); + r->m_value++; + if (r->m_value > m_releaseAbove) + m_manager->releaseResource(i); + i++; + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; + int m_releaseAbove; +}; + +void tst_PreallocatedArrayPolicy::heavyDutyMultiThreadedAccessRelease() +{ + tst_Thread2::Manager *manager = new tst_Thread2::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int u = 0; u < 2; u++) { + + for (int i = 0; i < iterations; i++) { + tst_Thread2 *thread = new tst_Thread2(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) == Q_NULLPTR); + } + + qDeleteAll(threads); + threads.clear(); + } + + delete manager; +} + +void tst_PreallocatedArrayPolicy::maximumNumberOfResources() +{ + Qt3D::QResourcesManager manager; + + QList resources; + QList handles; + + QCOMPARE(tHandle16::maxIndex(), (uint)manager.maxResourcesEntries()); + + for (int i = 0; i < manager.maxResourcesEntries(); i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } +} + +QTEST_APPLESS_MAIN(tst_PreallocatedArrayPolicy) + +#include "tst_preallocatedarraypolicy.moc" diff --git a/tests/auto/core/arrayresourcesmanager/tst_arrayresourcesmanager.cpp b/tests/auto/core/arrayresourcesmanager/tst_arrayresourcesmanager.cpp deleted file mode 100644 index e57be5746..000000000 --- a/tests/auto/core/arrayresourcesmanager/tst_arrayresourcesmanager.cpp +++ /dev/null @@ -1,440 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt3D module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include -#include - -class tst_ArrayResourcesManager : public QObject -{ - Q_OBJECT -public: - tst_ArrayResourcesManager() {} - ~tst_ArrayResourcesManager() {} - -private slots: - void createResourcesManager(); - void acquireResources(); - void getResources(); - void registerResourcesResize(); - void removeResource(); - void resetResource(); - void lookupResource(); - void releaseResource(); - void heavyDutyMultiThreadedAccess(); - void heavyDutyMultiThreadedAccessRelease(); - void maximumNumberOfResources(); -}; - -class tst_ArrayResource -{ -public: - tst_ArrayResource() : m_value(0) - {} - - int m_value; -}; - -typedef Qt3D::QHandle tHandle; -typedef Qt3D::QHandle tHandle4; -typedef Qt3D::QHandle tHandle8; -typedef Qt3D::QHandle tHandle16; - -void tst_ArrayResourcesManager::createResourcesManager() -{ - Qt3D::QResourcesManager manager16; - Qt3D::QResourcesManager manager4; - Qt3D::QResourcesManager manager8; - QVERIFY(manager16.maxResourcesEntries() == 65535); - QVERIFY(manager8.maxResourcesEntries() == 255); - QVERIFY(manager4.maxResourcesEntries() == 15); -} - -/*! - * Check that the handles returned when a registering resources - * have a correct index and counter. - */ -void tst_ArrayResourcesManager::acquireResources() -{ - Qt3D::QResourcesManager manager; - - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - } -} - -/*! - * Test that values can be properly retrieved. - */ -void tst_ArrayResourcesManager::getResources() -{ - - Qt3D::QResourcesManager manager; - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - resources << manager.data(handles.at(i)); - QVERIFY(resources.at(i) != Q_NULLPTR); - resources.at(i)->m_value = i; - } - - for (int i = 0; i < 5; i++) - QVERIFY(manager.data(handles.at(i))->m_value == i); - - // Check that an invalid resource returns NULL - tHandle8 iHandle; - QVERIFY(manager.data(iHandle) == Q_NULLPTR); - -} - -/*! - * Test that when a resize of the data vectors in the manager occurs, - * everything behaves correctly. - */ -void tst_ArrayResourcesManager::registerResourcesResize() -{ - Qt3D::QResourcesManager manager; - QList handles; - - for (uint i = 0; i < 2; i++) { - handles << manager.acquire(); - manager.data(handles.at(i))->m_value = i + 2; - } - - for (uint i = 0; i < 5; i++) { - handles << manager.acquire(); - manager.data(handles.at(i + 2))->m_value = i + 2 + 5; - } - - for (int i = 0; i < 7; i++) { - QVERIFY(handles.at(i).index() == static_cast(i)); - QVERIFY(handles.at(i).counter() == 1); - if (i < 2) - QVERIFY(manager.data(handles.at(i))->m_value == i + 2); - else - QVERIFY(manager.data(handles.at(i))->m_value == i + 5); - } -} - -/*! - * Checks for the removal of resources. - */ -void tst_ArrayResourcesManager::removeResource() -{ - Qt3D::QResourcesManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 32; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - } - - manager.release(handles.at(2)); - QVERIFY(manager.data(handles.at(2)) == Q_NULLPTR); - // Triggers QASSERT so commented - // manager.release(handles.at(2)); - - tHandle nHandle = manager.acquire(); - QVERIFY(manager.data(nHandle) != Q_NULLPTR); -} - -/*! - * Checks that reset behaves correctly. - */ -void tst_ArrayResourcesManager::resetResource() -{ - Qt3D::QResourcesManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - manager.reset(); - for (uint i = 0; i < 5; i++) { - QVERIFY(manager.data(handles.at(i)) == Q_NULLPTR); - } - handles.clear(); - for (uint i = 0; i < 5; i++) - handles << manager.acquire(); - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - QVERIFY(manager.data(handles.at(i))->m_value != 4); - } -} - -void tst_ArrayResourcesManager::lookupResource() -{ - Qt3D::QResourcesManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - - tHandle16 t = manager.lookupHandle(2); - QVERIFY(t.handle() == 0); - QVERIFY(manager.data(t) == Q_NULLPTR); - tst_ArrayResource *resource = manager.getOrCreateResource(2); - QVERIFY(resource != Q_NULLPTR); - t = manager.lookupHandle(2); - QVERIFY(manager.data(t) == manager.lookupResource(2)); - QVERIFY(t == manager.getOrAcquireHandle(2)); - QVERIFY(resource == manager.getOrCreateResource(2)); - QVERIFY(manager.data(t) == resource); -} - -void tst_ArrayResourcesManager::releaseResource() -{ - Qt3D::QResourcesManager manager; - QList resources; - - for (int i = 0; i < 5; i++) { - resources << manager.getOrCreateResource(i); - } - - for (int i = 0; i < 5; i++) { - QVERIFY(resources.at(i) == manager.lookupResource(i)); - } - - for (int i = 0; i < 5; i++) { - manager.releaseResource(i); - QVERIFY(manager.lookupResource(i) == Q_NULLPTR); - } -} - -class tst_Thread : public QThread -{ - Q_OBJECT -public: - - typedef Qt3D::QResourcesManager Manager; - - tst_Thread() - : QThread() - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - i++; - QVERIFY(r != Q_NULLPTR); - Manager::WriteLocker lock(m_manager); - r->m_value++; - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; -}; - -void tst_ArrayResourcesManager::heavyDutyMultiThreadedAccess() -{ - tst_Thread::Manager *manager = new tst_Thread::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int i = 0; i < iterations; i++) { - tst_Thread *thread = new tst_Thread(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) != Q_NULLPTR); - QVERIFY(manager->lookupResource(i)->m_value = iterations); - } - - qDeleteAll(threads); - delete manager; -} - -class tst_Thread2 : public QThread -{ - Q_OBJECT -public: - - typedef Qt3D::QResourcesManager Manager; - - tst_Thread2(int releaseAbove = 7) - : QThread() - , m_releaseAbove(releaseAbove) - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - QVERIFY(r != Q_NULLPTR); - Manager::WriteLocker lock(m_manager); - r->m_value++; - if (r->m_value > m_releaseAbove) - m_manager->releaseResource(i); - i++; - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; - int m_releaseAbove; -}; - -void tst_ArrayResourcesManager::heavyDutyMultiThreadedAccessRelease() -{ - tst_Thread2::Manager *manager = new tst_Thread2::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int u = 0; u < 2; u++) { - - for (int i = 0; i < iterations; i++) { - tst_Thread2 *thread = new tst_Thread2(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) == Q_NULLPTR); - } - - qDeleteAll(threads); - threads.clear(); - } - - delete manager; -} - -void tst_ArrayResourcesManager::maximumNumberOfResources() -{ - Qt3D::QResourcesManager manager; - - QList resources; - QList handles; - - QCOMPARE(tHandle16::maxIndex(), (uint)manager.maxResourcesEntries()); - - for (int i = 0; i < manager.maxResourcesEntries(); i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } -} - -QTEST_APPLESS_MAIN(tst_ArrayResourcesManager) - -#include "tst_arrayresourcesmanager.moc" -- cgit v1.2.3