/**************************************************************************** ** ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). ** 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 #include #include #include #include #include #include class tst_QResourceManager : public QObject { Q_OBJECT private Q_SLOTS: void benchmarkAllocateSmallResources(); void benchmarkAccessSmallResources(); void benchmarkLookupSmallResources(); void benchmarRandomAccessSmallResources(); void benchmarkRandomLookupSmallResources(); void benchmarkReleaseSmallResources(); void benchmarkAllocateBigResources(); void benchmarkAccessBigResources(); void benchmarRandomAccessBigResources(); void benchmarkLookupBigResources(); void benchmarkRandomLookupBigResources(); void benchmarkReleaseBigResources(); }; class tst_SmallArrayResource { public: tst_SmallArrayResource() : m_value(0) {} int m_value; }; class tst_BigArrayResource { public: QMatrix4x4 m_matrix; }; template void benchmarkAllocateResources() { Qt3DCore::QResourceManager manager; volatile Resource *c; QBENCHMARK_ONCE { const int max = (1 << 16) - 1; for (int i = 0; i < max; i++) c = manager.getOrCreateResource(i); } Q_UNUSED(c); } template void benchmarkAccessResources() { Qt3DCore::QResourceManager manager; const int max = (1 << 16) - 1; QList > handles(max); for (int i = 0; i < max; i++) handles[i] = manager.acquire(); volatile Resource *c; QBENCHMARK { for (int i = 0; i < max; i++) c = manager.data(handles[i]); } Q_UNUSED(c); } template void benchmarkRandomAccessResource() { Qt3DCore::QResourceManager manager; const int max = (1 << 16) - 1; QList > handles(max); for (int i = 0; i < max; i++) handles[i] = manager.acquire(); std::random_device rd; std::mt19937 g(rd()); std::shuffle(handles.begin(), handles.end(), g); volatile Resource *c; QBENCHMARK { for (int i = 0; i < max; i++) c = manager.data(handles[i]); } Q_UNUSED(c); } template void benchmarkLookupResources() { Qt3DCore::QResourceManager manager; const int max = (1 << 16) - 1; for (int i = 0; i < max; i++) manager.getOrCreateResource(i); volatile Resource *c; QBENCHMARK { for (int i = 0; i < max; i++) c = manager.lookupResource(i); } Q_UNUSED(c); } template void benchmarkRandomLookupResources() { Qt3DCore::QResourceManager manager; const int max = (1 << 16) - 1; QList resourcesIndices(max); for (int i = 0; i < max; i++) { manager.getOrCreateResource(i); resourcesIndices[i] = i; } std::random_device rd; std::mt19937 g(rd()); std::shuffle(resourcesIndices.begin(), resourcesIndices.end(), g); volatile Resource *c; QBENCHMARK { for (int i = 0; i < max; i++) c = manager.lookupResource(resourcesIndices[i]); } Q_UNUSED(c); } template void benchmarkReleaseResources() { Qt3DCore::QResourceManager manager; const int max = (1 << 16) - 1; QList > handles(max); for (int i = 0; i < max; i++) handles[i] = manager.acquire(); for (int i = 0; i < max; i++) manager.release(handles.at(i)); handles.clear(); QBENCHMARK_ONCE { // the release/clear should have left many unused handled in the resourcemanager, // so the next acquire will trigger a collection of all freed resources manager.acquire(); } } void tst_QResourceManager::benchmarkAllocateSmallResources() { benchmarkAllocateResources(); } void tst_QResourceManager::benchmarkAccessSmallResources() { benchmarkAccessResources(); } void tst_QResourceManager::benchmarkLookupSmallResources() { benchmarkLookupResources(); } void tst_QResourceManager::benchmarRandomAccessSmallResources() { benchmarkRandomAccessResource(); } void tst_QResourceManager::benchmarkRandomLookupSmallResources() { benchmarkRandomLookupResources(); } void tst_QResourceManager::benchmarkReleaseSmallResources() { benchmarkReleaseResources(); } void tst_QResourceManager::benchmarkAllocateBigResources() { benchmarkAllocateResources(); } void tst_QResourceManager::benchmarkAccessBigResources() { benchmarkAccessResources(); } void tst_QResourceManager::benchmarRandomAccessBigResources() { benchmarkRandomAccessResource(); } void tst_QResourceManager::benchmarkLookupBigResources() { benchmarkLookupResources(); } void tst_QResourceManager::benchmarkRandomLookupBigResources() { benchmarkRandomLookupResources(); } void tst_QResourceManager::benchmarkReleaseBigResources() { benchmarkReleaseResources(); } QTEST_APPLESS_MAIN(tst_QResourceManager) #include "tst_bench_qresourcesmanager.moc"