X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/f71e0881d2c0a52c5fa020e3fd2b44ca6ab7b2d4..a1c9bc5595a21299dab9260c6a77c0561c95c8ba:/src/tests/kfileitemmodelbenchmark.cpp diff --git a/src/tests/kfileitemmodelbenchmark.cpp b/src/tests/kfileitemmodelbenchmark.cpp index f72e43ede..4eeb9ff55 100644 --- a/src/tests/kfileitemmodelbenchmark.cpp +++ b/src/tests/kfileitemmodelbenchmark.cpp @@ -1,54 +1,39 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * Copyright (C) 2013 by Frank Reininghaus * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#include +/* + * SPDX-FileCopyrightText: 2011 Peter Penz + * SPDX-FileCopyrightText: 2013 Frank Reininghaus + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ -#include "kitemviews/kfileitemmodel.h" -#include "kitemviews/private/kfileitemmodelsortalgorithm.h" +#include +#include +#include -#include "testdir.h" +#include -#include +#include "kitemviews/kfileitemmodel.h" +#include "kitemviews/private/kfileitemmodelsortalgorithm.h" -void myMessageOutput(QtMsgType type, const char* msg) +void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + Q_UNUSED(context) + switch (type) { case QtDebugMsg: break; case QtWarningMsg: break; case QtCriticalMsg: - fprintf(stderr, "Critical: %s\n", msg); + fprintf(stderr, "Critical: %s\n", msg.toLocal8Bit().data()); break; case QtFatalMsg: - fprintf(stderr, "Fatal: %s\n", msg); + fprintf(stderr, "Fatal: %s\n", msg.toLocal8Bit().data()); abort(); default: - break; + break; } } -namespace { - const int DefaultTimeout = 5000; -}; - Q_DECLARE_METATYPE(KFileItemList) Q_DECLARE_METATYPE(KItemRangeList) @@ -59,19 +44,24 @@ class KFileItemModelBenchmark : public QObject public: KFileItemModelBenchmark(); -private slots: +private Q_SLOTS: + void initTestCase(); void insertAndRemoveManyItems_data(); void insertAndRemoveManyItems(); - void insertManyChildItems(); private: - static KFileItemList createFileItemList(const QStringList& fileNames, const QString& urlPrefix = QLatin1String("file:///")); + static KFileItemList createFileItemList(const QStringList &fileNames, const QString &urlPrefix = QLatin1String("file:///")); }; KFileItemModelBenchmark::KFileItemModelBenchmark() { } +void KFileItemModelBenchmark::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); +} + void KFileItemModelBenchmark::insertAndRemoveManyItems_data() { QTest::addColumn("initialItems"); @@ -82,10 +72,9 @@ void KFileItemModelBenchmark::insertAndRemoveManyItems_data() QTest::addColumn("expectedItemsRemoved"); QList sizes; - sizes << 1000 << 4000 << 16000 << 64000 << 256000; - //sizes << 50000 << 100000 << 150000 << 200000 << 250000; + sizes << 100000; - foreach (int n, sizes) { + for (int n : std::as_const(sizes)) { QStringList allStrings; for (int i = 0; i < n; ++i) { allStrings << QString::number(i); @@ -167,10 +156,10 @@ void KFileItemModelBenchmark::insertAndRemoveManyItems() // Avoid overhead caused by natural sorting // and determining the isDir/isLink roles. model.m_naturalSorting = false; - model.setRoles(QSet() << "text"); + model.setRoles({"text"}); - QSignalSpy spyItemsInserted(&model, SIGNAL(itemsInserted(KItemRangeList))); - QSignalSpy spyItemsRemoved(&model, SIGNAL(itemsRemoved(KItemRangeList))); + QSignalSpy spyItemsInserted(&model, &KFileItemModel::itemsInserted); + QSignalSpy spyItemsRemoved(&model, &KFileItemModel::itemsRemoved); QBENCHMARK { model.slotClear(); @@ -185,7 +174,7 @@ void KFileItemModelBenchmark::insertAndRemoveManyItems() QCOMPARE(model.count(), initialItems.count() + newItems.count()); if (!removedItems.isEmpty()) { - model.removeItems(removedItems, KFileItemModel::DeleteItemData); + model.slotItemsDeleted(removedItems); } QCOMPARE(model.count(), initialItems.count() + newItems.count() - removedItems.count()); } @@ -209,126 +198,19 @@ void KFileItemModelBenchmark::insertAndRemoveManyItems() } } -void KFileItemModelBenchmark::insertManyChildItems() -{ - // TODO: this function needs to be adjusted to the changes in KFileItemModel - // (replacement of slotNewItems(KFileItemList) by slotItemsAdded(KUrl,KFileItemList)) - // Currently, this function tries to insert child items of multiple - // directories by invoking the slot only once. -#if 0 - qInstallMsgHandler(myMessageOutput); - - KFileItemModel model; - - // Avoid overhead caused by natural sorting. - model.m_naturalSorting = false; - - QSet modelRoles = model.roles(); - modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount"; - model.setRoles(modelRoles); - model.setSortDirectoriesFirst(false); - - // Create a test folder with a 3-level tree structure of folders. - TestDir testFolder; - int numberOfFolders = 0; - - QStringList subFolderNames; - subFolderNames << "a/" << "b/" << "c/" << "d/"; - - foreach (const QString& s1, subFolderNames) { - ++numberOfFolders; - foreach (const QString& s2, subFolderNames) { - ++numberOfFolders; - foreach (const QString& s3, subFolderNames) { - testFolder.createDir("level-1-" + s1 + "level-2-" + s2 + "level-3-" + s3); - ++numberOfFolders; - } - } - } - - // Open the folder in the model and expand all subfolders. - model.loadDirectory(testFolder.url()); - QVERIFY(QTest::kWaitForSignal(&model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout)); - - int index = 0; - while (index < model.count()) { - if (model.isExpandable(index)) { - model.setExpanded(index, true); - - if (!model.data(index).value("text").toString().startsWith("level-3")) { - // New subfolders will appear unless we are on the final level already. - QVERIFY(QTest::kWaitForSignal(&model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout)); - } - - QVERIFY(model.isExpanded(index)); - } - ++index; - } - - QCOMPARE(model.count(), numberOfFolders); - - // Create a list of many file items, which will be added to each of the - // "level 1", "level 2", and "level 3" folders. - const int filesPerDirectory = 500; - QStringList allStrings; - for (int i = 0; i < filesPerDirectory; ++i) { - allStrings << QString::number(i); - } - allStrings.sort(); - - KFileItemList newItems; - - // Also keep track of all expected items, including the existing - // folders, to verify the final state of the model. - KFileItemList allExpectedItems; - - for (int i = 0; i < model.count(); ++i) { - const KFileItem folderItem = model.fileItem(i); - allExpectedItems << folderItem; - - const KUrl folderUrl = folderItem.url(); - KFileItemList itemsInFolder = createFileItemList(allStrings, folderUrl.url(KUrl::AddTrailingSlash)); - - newItems.append(itemsInFolder); - allExpectedItems.append(itemsInFolder); - } - - // Bring the items into random order. - KRandomSequence randomSequence(0); - randomSequence.randomize(newItems); - - // Measure how long it takes to insert and then remove all files. - QBENCHMARK { - model.slotNewItems(newItems); - model.slotCompleted(); - - QCOMPARE(model.count(), allExpectedItems.count()); - QVERIFY(model.isConsistent()); - for (int i = 0; i < model.count(); ++i) { - QCOMPARE(model.fileItem(i), allExpectedItems.at(i)); - } - - model.slotItemsDeleted(newItems); - QCOMPARE(model.count(), numberOfFolders); - QVERIFY(model.isConsistent()); - } -#endif -} - -KFileItemList KFileItemModelBenchmark::createFileItemList(const QStringList& fileNames, const QString& prefix) +KFileItemList KFileItemModelBenchmark::createFileItemList(const QStringList &fileNames, const QString &prefix) { // Suppress 'file does not exist anymore' messages from KFileItemPrivate::init(). - qInstallMsgHandler(myMessageOutput); + qInstallMessageHandler(myMessageOutput); KFileItemList result; - foreach (const QString& name, fileNames) { - const KUrl url(prefix + name); - const KFileItem item(url, QString(), KFileItem::Unknown); + for (const QString &name : fileNames) { + const KFileItem item(QUrl::fromLocalFile(prefix + name), QString(), KFileItem::Unknown); result << item; } return result; } -QTEST_KDEMAIN(KFileItemModelBenchmark, NoGUI) +QTEST_MAIN(KFileItemModelBenchmark) #include "kfileitemmodelbenchmark.moc"