File indexing completed on 2023-09-24 11:39:26
0001 /* 0002 This file is part of the KDE libraries 0003 0004 SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include <kdirwatch.h> 0010 0011 #include <QDebug> 0012 #include <QDir> 0013 #include <QFileInfo> 0014 #include <QSignalSpy> 0015 #include <QTemporaryDir> 0016 #include <QTest> 0017 #include <QThread> 0018 #include <sys/stat.h> 0019 #ifdef Q_OS_UNIX 0020 #include <unistd.h> // ::link() 0021 #endif 0022 0023 #include "config-tests.h" 0024 #include "kcoreaddons_debug.h" 0025 0026 #include "kdirwatch_test_utils.h" 0027 0028 using namespace KDirWatchTestUtils; 0029 0030 class KDirWatch_UnitTest : public QObject 0031 { 0032 Q_OBJECT 0033 public: 0034 KDirWatch_UnitTest() 0035 { 0036 // Speed up the test by making the kdirwatch timer (to compress changes) faster 0037 qputenv("KDIRWATCH_POLLINTERVAL", "50"); 0038 qputenv("KDIRWATCH_METHOD", KDIRWATCH_TEST_METHOD); 0039 s_staticObjectUsingSelf(); 0040 0041 m_path = m_tempDir.path() + QLatin1Char('/'); 0042 KDirWatch *dirW = &s_staticObject()->m_dirWatch; 0043 qCDebug(KCOREADDONS_DEBUG) << "Using method" << methodToString(dirW->internalMethod()); 0044 } 0045 0046 private Q_SLOTS: // test methods 0047 void initTestCase() 0048 { 0049 QFileInfo pathInfo(m_path); 0050 QVERIFY(pathInfo.isDir() && pathInfo.isWritable()); 0051 0052 // By creating the files upfront, we save waiting a full second for an mtime change 0053 createFile(m_path + QLatin1String("ExistingFile")); 0054 createFile(m_path + QLatin1String("TestFile")); 0055 createFile(m_path + QLatin1String("nested_0")); 0056 createFile(m_path + QLatin1String("nested_1")); 0057 0058 s_staticObject()->m_dirWatch.addFile(m_path + QLatin1String("ExistingFile")); 0059 } 0060 void benchCreateTree(); 0061 void benchCreateWatcher(); 0062 void benchNotifyWatcher(); 0063 0064 private: 0065 QTemporaryDir m_tempDir; 0066 QString m_path; 0067 }; 0068 0069 QTEST_MAIN(KDirWatch_UnitTest) 0070 0071 void KDirWatch_UnitTest::benchCreateTree() 0072 { 0073 #if !ENABLE_BENCHMARKS 0074 QSKIP("Benchmarks are disabled in debug mode"); 0075 #endif 0076 QTemporaryDir dir; 0077 0078 QBENCHMARK { 0079 createDirectoryTree(dir.path()); 0080 } 0081 } 0082 0083 void KDirWatch_UnitTest::benchCreateWatcher() 0084 { 0085 #if !ENABLE_BENCHMARKS 0086 QSKIP("Benchmarks are disabled in debug mode"); 0087 #endif 0088 QTemporaryDir dir; 0089 createDirectoryTree(dir.path()); 0090 0091 QBENCHMARK { 0092 KDirWatch watch; 0093 watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch::WatchFiles); 0094 } 0095 } 0096 0097 void KDirWatch_UnitTest::benchNotifyWatcher() 0098 { 0099 #if !ENABLE_BENCHMARKS 0100 QSKIP("Benchmarks are disabled in debug mode"); 0101 #endif 0102 QTemporaryDir dir; 0103 // create the dir once upfront 0104 auto numFiles = createDirectoryTree(dir.path()); 0105 waitUntilMTimeChange(dir.path()); 0106 0107 KDirWatch watch; 0108 watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch::WatchFiles); 0109 0110 // now touch all the files repeatedly and wait for the dirty updates to come in 0111 QSignalSpy spy(&watch, &KDirWatch::dirty); 0112 QBENCHMARK { 0113 createDirectoryTree(dir.path()); 0114 QTRY_COMPARE_WITH_TIMEOUT(spy.count(), numFiles, 30000); 0115 spy.clear(); 0116 } 0117 } 0118 0119 #include "kdirwatch_benchmarktest.moc"