Skip to content

Commit

Permalink
tst_bench_qdiriterator: add benchmark for QDirListing
Browse files Browse the repository at this point in the history
Make the benchmarks more comparable:
- Store the QDir::Filters in one central var, this way it's the same in
  all the call sites

- Add a `bool forceStat`, when true force calling stat(), either
  explicitly in posix_helper(), or implicitly in Qt classes by e.g.
  calling a QFileInfo method that would have to call system stat()
  internally. Otherwise benchmarking readdir()/dirent showed bigger
  times, which was mostly due to the explicit stat() calls, whereas we
  can use dirent::d_type (on the platforms where it's available)

Drive by change: for std::filesystem::recursive_directory_iterator, set
skip_permission_denied option and use the non-throwing constructor.

Change-Id: Icf138a5dc41d32741c1be611d664b01008b2f3fe
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
Ahmad Samir committed Feb 29, 2024
1 parent c39a0d1 commit 5e9de27
Showing 1 changed file with 65 additions and 14 deletions.
79 changes: 65 additions & 14 deletions tests/benchmarks/corelib/io/qdiriterator/tst_bench_qdiriterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QDebug>
#include <QDirIterator>
#include <QDirListing>
#include <QString>
#include <qplatformdefs.h>

Expand All @@ -23,16 +24,30 @@
#include <filesystem>
#endif

using namespace Qt::StringLiterals;

constexpr bool forceStat = false;

class tst_QDirIterator : public QObject
{
Q_OBJECT

void data();

const QDir::Filters dirFilters =
// QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot
QDir::AllEntries | QDir::Hidden
//QDir::Files | QDir::NoDotAndDotDot,
// QDir::Files,
;

private slots:
void posix();
void posix_data() { data(); }
void diriterator();
void diriterator_data() { data(); }
void dirlisting();
void dirlisting_data() { data(); }
void fsiterator();
void fsiterator_data() { data(); }
void stdRecursiveDirectoryIterator();
Expand Down Expand Up @@ -118,9 +133,20 @@ static int posix_helper(const char *dirpath)
QByteArray ba = dirpath;
ba += '/';
ba += entry->d_name;
bool isDir = false;
#if defined(_DIRENT_HAVE_D_TYPE) || defined(Q_OS_BSD4)
isDir = entry->d_type == DT_DIR;
if (forceStat) {
QT_STATBUF st;
QT_LSTAT(ba.constData(), &st);
}
#else // d_type not available >>> must stat() to see if it's a dir
QT_STATBUF st;
QT_LSTAT(ba.constData(), &st);
if (S_ISDIR(st.st_mode))
isDir = S_ISDIR(st.st_mode);
#endif

if (isDir)
count += posix_helper(ba.constData());
}

Expand Down Expand Up @@ -157,14 +183,12 @@ void tst_QDirIterator::diriterator()
QBENCHMARK {
int c = 0;

QDirIterator dir(dirpath,
//QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot,
//QDir::AllEntries | QDir::Hidden,
QDir::Files,
QDirIterator::Subdirectories);
QDirIterator dir(dirpath, dirFilters, QDirIterator::Subdirectories);

while (dir.hasNext()) {
const auto fi = dir.nextFileInfo();
if (forceStat)
fi.size();
//printf("%s\n", qPrintable(dir.fileName()));
0 && printf("%d %s\n",
fi.isDir(),
Expand All @@ -178,6 +202,28 @@ void tst_QDirIterator::diriterator()
qDebug() << count;
}

void tst_QDirIterator::dirlisting()
{
QFETCH(QByteArray, dirpath);

int count = 0;

QBENCHMARK {
int c = 0;

QDirListing dir(dirpath, dirFilters, QDirListing::IteratorFlag::Recursive);

for (const auto &dirEntry : dir) {
const auto path = dirEntry.filePath();
if (forceStat)
dirEntry.size();
++c;
}
count = c;
}
qDebug() << count;
}

void tst_QDirIterator::fsiterator()
{
QFETCH(QByteArray, dirpath);
Expand All @@ -189,14 +235,12 @@ void tst_QDirIterator::fsiterator()
int c = 0;

dump && printf("\n\n\n\n");
QDirIteratorTest::QFileSystemIterator dir(dirpath,
//QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot,
//QDir::AllEntries | QDir::Hidden,
//QDir::Files | QDir::NoDotAndDotDot,
QDir::Files,
QDirIteratorTest::QFileSystemIterator::Subdirectories);
QDirIteratorTest::QFileSystemIterator dir(
dirpath, dirFilters, QDirIteratorTest::QFileSystemIterator::Subdirectories);

for (; !dir.atEnd(); dir.next()) {
if (forceStat)
dir.fileInfo().size();
dump && printf("%d %s\n",
dir.fileInfo().isDir(),
//qPrintable(dir.fileInfo().absoluteFilePath()),
Expand All @@ -217,10 +261,17 @@ void tst_QDirIterator::stdRecursiveDirectoryIterator()

int count = 0;

namespace fs = std::filesystem;
std::error_code ec;

// Note that fs::recursive_directory_iterator may be calling stat() internally,
// that depends on the implementation. So the benchmark times might "seem" higher
// than the other methods in this source file.
QBENCHMARK {
int c = 0;
for (auto obj : std::filesystem::recursive_directory_iterator(dirpath.data())) {
if (obj.is_directory())
for (const auto &dirEntry : fs::recursive_directory_iterator(
dirpath.data(), fs::directory_options::skip_permission_denied, ec)) {
if (dirEntry.is_directory())
continue;
c++;
}
Expand Down

0 comments on commit 5e9de27

Please sign in to comment.