Fix QDir benchmark to do meaningful things

* Include the creation of the QDir inside QBENCHMARK, otherwise
  the it can hit the cached code path where subsequent runs return
  results much faster.

* Same for the opendir()/readdir() test: if opendir() isn't called
  again, readdir() will just return null right away.

These two issues led to nonsense results like 0.00025 msecs per
iteration, doing nothing is really quick.

While at it, port the cleanup code to QDir::removeRecursively()

Pick-to: 6.5 6.4 6.2
Change-Id: Ic1bdd92d41efe1f6d0eaaa33eca066cb7d19fc93
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2023-02-02 19:19:07 +01:00
parent 84fb0de413
commit eae031d5c3

View File

@ -32,24 +32,14 @@ public slots:
} }
void cleanupTestCase() void cleanupTestCase()
{ {
{ QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); QVERIFY(testdir.removeRecursively());
testdir.setSorting(QDir::Unsorted);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
foreach (const QString &filename, testdir.entryList()) {
testdir.remove(filename);
}
}
const QDir temp = QDir(QDir::tempPath());
temp.rmdir(QLatin1String("test_speed"));
} }
private slots: private slots:
void baseline() {}
void sizeSpeed() void sizeSpeed()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QFileInfoList fileInfoList = testdir.entryInfoList(QDir::Files, QDir::Unsorted); QFileInfoList fileInfoList = testdir.entryInfoList(QDir::Files, QDir::Unsorted);
foreach (const QFileInfo &fileInfo, fileInfoList) { foreach (const QFileInfo &fileInfo, fileInfoList) {
fileInfo.isDir(); fileInfo.isDir();
@ -59,8 +49,8 @@ private slots:
} }
void sizeSpeedIterator() void sizeSpeedIterator()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QDirIterator dit(testdir.path(), QDir::Files); QDirIterator dit(testdir.path(), QDir::Files);
while (dit.hasNext()) { while (dit.hasNext()) {
const auto fi = dit.nextFileInfo(); const auto fi = dit.nextFileInfo();
@ -72,8 +62,8 @@ private slots:
void sizeSpeedWithoutFilter() void sizeSpeedWithoutFilter()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QFileInfoList fileInfoList = testdir.entryInfoList(QDir::NoFilter, QDir::Unsorted); QFileInfoList fileInfoList = testdir.entryInfoList(QDir::NoFilter, QDir::Unsorted);
foreach (const QFileInfo &fileInfo, fileInfoList) { foreach (const QFileInfo &fileInfo, fileInfoList) {
fileInfo.size(); fileInfo.size();
@ -82,8 +72,8 @@ private slots:
} }
void sizeSpeedWithoutFilterIterator() void sizeSpeedWithoutFilterIterator()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
QDirIterator dit(testdir.path()); QDirIterator dit(testdir.path());
while (dit.hasNext()) { while (dit.hasNext()) {
const auto fi = dit.nextFileInfo(); const auto fi = dit.nextFileInfo();
@ -95,9 +85,9 @@ private slots:
void sizeSpeedWithoutFileInfoList() void sizeSpeedWithoutFileInfoList()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Unsorted);
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Unsorted);
QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted); QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted);
foreach (const QString &filename, fileList) { foreach (const QString &filename, fileList) {
QFileInfo fileInfo(filename); QFileInfo fileInfo(filename);
@ -108,10 +98,10 @@ private slots:
void iDontWantAnyStat() void iDontWantAnyStat()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Unsorted);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Unsorted);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted); QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted);
foreach (const QString &filename, fileList) { foreach (const QString &filename, fileList) {
Q_UNUSED(filename); Q_UNUSED(filename);
@ -130,10 +120,10 @@ private slots:
void sorted_byTime() void sorted_byTime()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Time);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setSorting(QDir::Time);
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Time); QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Time);
foreach (const QString &filename, fileList) { foreach (const QString &filename, fileList) {
Q_UNUSED(filename); Q_UNUSED(filename);
@ -143,9 +133,9 @@ private slots:
void sorted_byName() void sorted_byName()
{ {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
QBENCHMARK { QBENCHMARK {
QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
[[maybe_unused]] auto r = testdir.entryInfoList(QDir::NoFilter, QDir::Name); [[maybe_unused]] auto r = testdir.entryInfoList(QDir::NoFilter, QDir::Name);
} }
} }
@ -159,22 +149,22 @@ private slots:
wcscpy(appendedPath, dirpath); wcscpy(appendedPath, dirpath);
wcscat(appendedPath, L"\\*"); wcscat(appendedPath, L"\\*");
WIN32_FIND_DATA fd;
HANDLE hSearch = FindFirstFileW(appendedPath, &fd);
QVERIFY(hSearch != INVALID_HANDLE_VALUE);
QBENCHMARK { QBENCHMARK {
WIN32_FIND_DATA fd;
HANDLE hSearch = FindFirstFileW(appendedPath, &fd);
QVERIFY(hSearch != INVALID_HANDLE_VALUE);
do { do {
} while (FindNextFile(hSearch, &fd)); } while (FindNextFile(hSearch, &fd));
FindClose(hSearch);
} }
FindClose(hSearch);
#else #else
DIR *dir = opendir(qPrintable(testdir.absolutePath()));
QVERIFY(dir);
QVERIFY(!chdir(qPrintable(testdir.absolutePath()))); QVERIFY(!chdir(qPrintable(testdir.absolutePath())));
QBENCHMARK { QBENCHMARK {
DIR *dir = opendir(qPrintable(testdir.absolutePath()));
QVERIFY(dir);
struct dirent *item = readdir(dir); struct dirent *item = readdir(dir);
while (item) { while (item) {
char *fileName = item->d_name; char *fileName = item->d_name;
@ -184,8 +174,8 @@ private slots:
item = readdir(dir); item = readdir(dir);
} }
closedir(dir);
} }
closedir(dir);
#endif #endif
} }
}; };