tst_SubDirFileContainer: Simpify init / cleanup test case

Use For elements instead of constructing big recipes.

Change-Id: I44a01987ed02a0ccb44c31568d12c2aaeeb95f69
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-07-08 15:34:00 +02:00
parent 5aea1a817a
commit 9e3e3241ca

View File

@@ -64,11 +64,6 @@ static int expectedFilesCount()
return expectedFilesCountHelper(s_treeDepth) * s_topLevelSubDirsCount + 1; // +1 -> fileTemplate.txt return expectedFilesCountHelper(s_treeDepth) * s_topLevelSubDirsCount + 1; // +1 -> fileTemplate.txt
} }
static int threadsCount()
{
return qMax(QThread::idealThreadCount() - 1, 1); // "- 1" -> left for main thread
}
static bool generateOriginal(const QString &parentPath, const QString &templateFile, int depth) static bool generateOriginal(const QString &parentPath, const QString &templateFile, int depth)
{ {
const QDir parentDir(parentPath); const QDir parentDir(parentPath);
@@ -104,6 +99,11 @@ static void generateCopy(QPromise<void> &promise, const QString &sourcePath,
promise.future().cancel(); promise.future().cancel();
} }
static void removeTree(const QString &parentPath)
{
FilePath::fromString(parentPath).removeRecursively();
};
class tst_SubDirFileContainer : public QObject class tst_SubDirFileContainer : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -121,11 +121,10 @@ private slots:
qDebug() << "This manual test compares the performance of the SubDirFileContainer with a " qDebug() << "This manual test compares the performance of the SubDirFileContainer with a "
"manually written iterator using QDir::entryInfoList() and with QDirIterator."; "manually written iterator using QDir::entryInfoList() and with QDirIterator.";
QTC_SCOPED_TIMER("GENERATING TEMPORARY FILES TREE"); QTC_SCOPED_TIMER("GENERATING TEMPORARY FILES TREE");
m_threadsCount = threadsCount();
m_filesCount = expectedFilesCount(); m_filesCount = expectedFilesCount();
m_dirsCount = expectedDirsCount(); m_dirsCount = expectedDirsCount();
m_tempDir.reset(new QTemporaryDir); m_tempDir.reset(new QTemporaryDir);
qDebug() << "Generating on" << m_threadsCount << "cores..."; qDebug() << "Generating on" << qMax(QThread::idealThreadCount() - 1, 1) << "cores...";
qDebug() << "Generating" << m_filesCount << "files..."; qDebug() << "Generating" << m_filesCount << "files...";
qDebug() << "Generating" << m_dirsCount << "dirs..."; qDebug() << "Generating" << m_dirsCount << "dirs...";
qDebug() << "Generating inside" << m_tempDir->path() << "dir..."; qDebug() << "Generating inside" << m_tempDir->path() << "dir...";
@@ -141,43 +140,42 @@ private slots:
QVERIFY(parentDir.mkdir(sourceDirName)); QVERIFY(parentDir.mkdir(sourceDirName));
QVERIFY(generateOriginal(parentDir.filePath(sourceDirName), templateFile, s_treeDepth)); QVERIFY(generateOriginal(parentDir.filePath(sourceDirName), templateFile, s_treeDepth));
const auto onCopySetup = [](const QString &sourcePath, const QString &destPath) { const LoopRepeat iterator(s_topLevelSubDirsCount - 1);
return [sourcePath, destPath](Async<void> &async) {
async.setConcurrentCallData(generateCopy, sourcePath, destPath); const auto onCopySetup = [iterator, parentDir, sourceDirName](Async<void> &async) {
}; const QString destDirName = dirName(iterator.iteration() + 1);
QVERIFY(parentDir.mkdir(destDirName));
async.setConcurrentCallData(generateCopy, parentDir.filePath(sourceDirName),
parentDir.filePath(destDirName));
}; };
// Parallelize tree generation const For recipe = {
QList<GroupItem> tasks{parallelLimit(m_threadsCount)}; iterator,
for (int i = 1; i < s_topLevelSubDirsCount; ++i) { parallelIdealThreadCountLimit, // Parallelize tree generation
const QString destDirName = dirName(i); AsyncTask<void>(onCopySetup)
QVERIFY(parentDir.mkdir(destDirName)); };
tasks.append(AsyncTask<void>(onCopySetup(parentDir.filePath(sourceDirName), QCOMPARE(TaskTree::runBlocking(recipe), DoneWith::Success);
parentDir.filePath(destDirName))));
}
QCOMPARE(TaskTree::runBlocking(tasks), DoneWith::Success);
} }
void cleanupTestCase() void cleanupTestCase()
{ {
QTC_SCOPED_TIMER("CLEANING UP"); QTC_SCOPED_TIMER("CLEANING UP");
// Parallelize tree removal const LoopRepeat iterator(s_topLevelSubDirsCount - 1);
const auto removeTree = [](const QString &parentPath) {
FilePath::fromString(parentPath).removeRecursively();
};
const auto onSetup = [removeTree](const QString &parentPath) {
return [parentPath, removeTree](Async<void> &async) {
async.setConcurrentCallData(removeTree, parentPath);
};
};
const QDir parentDir(m_tempDir->path()); const QDir parentDir(m_tempDir->path());
QList<GroupItem> tasks {parallelLimit(m_threadsCount)}; const auto onSetup = [iterator, parentDir](Async<void> &async) {
for (int i = 0; i < s_topLevelSubDirsCount; ++i) async.setConcurrentCallData(removeTree,
tasks.append(AsyncTask<void>(onSetup(parentDir.filePath(dirName(i))))); parentDir.filePath(dirName(iterator.iteration() + 1)));
};
QCOMPARE(TaskTree::runBlocking(tasks), DoneWith::Success); const For recipe = {
iterator,
parallelIdealThreadCountLimit, // Parallelize tree removal
AsyncTask<void>(onSetup)
};
QCOMPARE(TaskTree::runBlocking(recipe), DoneWith::Success);
m_tempDir.reset(); m_tempDir.reset();
Singleton::deleteAll(); Singleton::deleteAll();
@@ -262,7 +260,6 @@ private slots:
} }
private: private:
int m_threadsCount = 1;
int m_dirsCount = 0; int m_dirsCount = 0;
int m_filesCount = 0; int m_filesCount = 0;
std::unique_ptr<QTemporaryDir> m_tempDir; std::unique_ptr<QTemporaryDir> m_tempDir;