kdirwatch_benchmarktest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. This file is part of the KDE libraries
  3. SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org>
  4. SPDX-License-Identifier: LGPL-2.0-or-later
  5. */
  6. #include <kdirwatch.h>
  7. #include <QDebug>
  8. #include <QDir>
  9. #include <QFileInfo>
  10. #include <QSignalSpy>
  11. #include <QTemporaryDir>
  12. #include <QTest>
  13. #include <QThread>
  14. #include <sys/stat.h>
  15. #ifdef Q_OS_UNIX
  16. #include <unistd.h> // ::link()
  17. #endif
  18. #include "config-tests.h"
  19. #include "kcoreaddons_debug.h"
  20. #include "kdirwatch_test_utils.h"
  21. using namespace KDirWatchTestUtils;
  22. class KDirWatch_UnitTest : public QObject
  23. {
  24. Q_OBJECT
  25. public:
  26. KDirWatch_UnitTest()
  27. {
  28. // Speed up the test by making the kdirwatch timer (to compress changes) faster
  29. qputenv("KDIRWATCH_POLLINTERVAL", "50");
  30. qputenv("KDIRWATCH_METHOD", KDIRWATCH_TEST_METHOD);
  31. s_staticObjectUsingSelf();
  32. m_path = m_tempDir.path() + QLatin1Char('/');
  33. KDirWatch *dirW = &s_staticObject()->m_dirWatch;
  34. qCDebug(KCOREADDONS_DEBUG) << "Using method" << methodToString(dirW->internalMethod());
  35. }
  36. private Q_SLOTS: // test methods
  37. void initTestCase()
  38. {
  39. QFileInfo pathInfo(m_path);
  40. QVERIFY(pathInfo.isDir() && pathInfo.isWritable());
  41. // By creating the files upfront, we save waiting a full second for an mtime change
  42. createFile(m_path + QLatin1String("ExistingFile"));
  43. createFile(m_path + QLatin1String("TestFile"));
  44. createFile(m_path + QLatin1String("nested_0"));
  45. createFile(m_path + QLatin1String("nested_1"));
  46. s_staticObject()->m_dirWatch.addFile(m_path + QLatin1String("ExistingFile"));
  47. }
  48. void benchCreateTree();
  49. void benchCreateWatcher();
  50. void benchNotifyWatcher();
  51. private:
  52. QTemporaryDir m_tempDir;
  53. QString m_path;
  54. };
  55. QTEST_MAIN(KDirWatch_UnitTest)
  56. void KDirWatch_UnitTest::benchCreateTree()
  57. {
  58. #if !ENABLE_BENCHMARKS
  59. QSKIP("Benchmarks are disabled in debug mode");
  60. #endif
  61. QTemporaryDir dir;
  62. QBENCHMARK {
  63. createDirectoryTree(dir.path());
  64. }
  65. }
  66. void KDirWatch_UnitTest::benchCreateWatcher()
  67. {
  68. #if !ENABLE_BENCHMARKS
  69. QSKIP("Benchmarks are disabled in debug mode");
  70. #endif
  71. QTemporaryDir dir;
  72. createDirectoryTree(dir.path());
  73. QBENCHMARK {
  74. KDirWatch watch;
  75. watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch::WatchFiles);
  76. }
  77. }
  78. void KDirWatch_UnitTest::benchNotifyWatcher()
  79. {
  80. #if !ENABLE_BENCHMARKS
  81. QSKIP("Benchmarks are disabled in debug mode");
  82. #endif
  83. QTemporaryDir dir;
  84. // create the dir once upfront
  85. auto numFiles = createDirectoryTree(dir.path());
  86. waitUntilMTimeChange(dir.path());
  87. KDirWatch watch;
  88. watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch::WatchFiles);
  89. // now touch all the files repeatedly and wait for the dirty updates to come in
  90. QSignalSpy spy(&watch, &KDirWatch::dirty);
  91. QBENCHMARK {
  92. createDirectoryTree(dir.path());
  93. QTRY_COMPARE_WITH_TIMEOUT(spy.count(), numFiles, 30000);
  94. spy.clear();
  95. }
  96. }
  97. #include "kdirwatch_benchmarktest.moc"