AutoTest: Further optimize TestCodeParser::scanForTests()

When loading a Qt project, after the Scanning For Tests
finished, the scanForTests() blocks the main thread for
about 3.5 seconds on the calls to parser->init().

Refactor the code so that it operates on QSet<FilePath>
instead of QList<FilePaths>.

This patch constraints the freeze to about 40 ms.

Change-Id: I219b3e2abf2b7e5166eec08d83f4cdcb8e4a8098
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Jarek Kobus
2023-04-23 17:25:46 +02:00
parent 72bddf9f51
commit b9ca680b03
12 changed files with 47 additions and 46 deletions

View File

@@ -24,7 +24,7 @@ CppParser::CppParser(ITestFramework *framework)
{
}
void CppParser::init(const FilePaths &filesToParse, bool fullParse)
void CppParser::init(const QSet<FilePath> &filesToParse, bool fullParse)
{
Q_UNUSED(filesToParse)
Q_UNUSED(fullParse)

View File

@@ -45,7 +45,7 @@ class ITestParser
public:
explicit ITestParser(ITestFramework *framework) : m_framework(framework) {}
virtual ~ITestParser() { }
virtual void init(const Utils::FilePaths &filesToParse, bool fullParse) = 0;
virtual void init(const QSet<Utils::FilePath> &filesToParse, bool fullParse) = 0;
virtual bool processDocument(QPromise<TestParseResultPtr> &futureInterface,
const Utils::FilePath &fileName) = 0;
@@ -63,7 +63,7 @@ class CppParser : public ITestParser
{
public:
explicit CppParser(ITestFramework *framework);
void init(const Utils::FilePaths &filesToParse, bool fullParse) override;
void init(const QSet<Utils::FilePath> &filesToParse, bool fullParse) override;
static bool selectedForBuilding(const Utils::FilePath &fileName);
QByteArray getFileContent(const Utils::FilePath &filePath) const;
void release() override;

View File

@@ -4,7 +4,6 @@
#include "qttest_utils.h"
#include "qttesttreeitem.h"
#include "../autotestplugin.h"
#include "../testframeworkmanager.h"
#include "../testsettings.h"
#include <utils/algorithm.h>
@@ -27,7 +26,8 @@ bool isQTestMacro(const QByteArray &macro)
return valid.contains(macro);
}
QHash<FilePath, TestCases> testCaseNamesForFiles(ITestFramework *framework, const FilePaths &files)
QHash<FilePath, TestCases> testCaseNamesForFiles(ITestFramework *framework,
const QSet<FilePath> &files)
{
QHash<FilePath, TestCases> result;
TestTreeItem *rootNode = framework->rootNode();
@@ -49,7 +49,8 @@ QHash<FilePath, TestCases> testCaseNamesForFiles(ITestFramework *framework, cons
return result;
}
QMultiHash<FilePath, FilePath> alternativeFiles(ITestFramework *framework, const FilePaths &files)
QMultiHash<FilePath, FilePath> alternativeFiles(ITestFramework *framework,
const QSet<FilePath> &files)
{
QMultiHash<FilePath, FilePath> result;
TestTreeItem *rootNode = framework->rootNode();

View File

@@ -26,9 +26,9 @@ namespace QTestUtils {
bool isQTestMacro(const QByteArray &macro);
QHash<Utils::FilePath, TestCases> testCaseNamesForFiles(ITestFramework *framework,
const Utils::FilePaths &files);
const QSet<Utils::FilePath> &files);
QMultiHash<Utils::FilePath, Utils::FilePath> alternativeFiles(ITestFramework *framework,
const Utils::FilePaths &files);
const QSet<Utils::FilePath> &files);
QStringList filterInterfering(const QStringList &provided, QStringList *omitted, bool isQuickTest);
Utils::Environment prepareBasicEnvironment(const Utils::Environment &env);

View File

@@ -414,7 +414,7 @@ QtTestParseResult *QtTestParser::createParseResult(
return parseResult;
}
void QtTestParser::init(const FilePaths &filesToParse, bool fullParse)
void QtTestParser::init(const QSet<FilePath> &filesToParse, bool fullParse)
{
if (!fullParse) { // in a full parse cached information might lead to wrong results
m_testCases = QTestUtils::testCaseNamesForFiles(framework(), filesToParse);

View File

@@ -34,7 +34,7 @@ class QtTestParser : public CppParser
public:
explicit QtTestParser(ITestFramework *framework) : CppParser(framework) {}
void init(const Utils::FilePaths &filesToParse, bool fullParse) override;
void init(const QSet<Utils::FilePath> &filesToParse, bool fullParse) override;
void release() override;
bool processDocument(QPromise<TestParseResultPtr> &promise,
const Utils::FilePath &fileName) override;

View File

@@ -22,7 +22,8 @@ bool isQuickTestMacro(const QByteArray &macro)
return valid.contains(macro);
}
QHash<FilePath, FilePath> proFilesForQmlFiles(ITestFramework *framework, const FilePaths &files)
QHash<FilePath, FilePath> proFilesForQmlFiles(ITestFramework *framework,
const QSet<FilePath> &files)
{
QHash<FilePath, FilePath> result;
TestTreeItem *rootNode = framework->rootNode();

View File

@@ -16,7 +16,7 @@ namespace QuickTestUtils {
bool isQuickTestMacro(const QByteArray &macro);
QHash<Utils::FilePath, Utils::FilePath> proFilesForQmlFiles(ITestFramework *framework,
const Utils::FilePaths &files);
const QSet<Utils::FilePath> &files);
} // namespace QuickTestUtils
} // namespace Internal

View File

@@ -338,7 +338,7 @@ QuickTestParser::QuickTestParser(ITestFramework *framework)
this, &QuickTestParser::handleDirectoryChanged);
}
void QuickTestParser::init(const FilePaths &filesToParse, bool fullParse)
void QuickTestParser::init(const QSet<FilePath> &filesToParse, bool fullParse)
{
m_qmlSnapshot = QmlJSTools::Internal::ModelManager::instance()->snapshot();
if (!fullParse) {

View File

@@ -24,7 +24,7 @@ class QuickTestParser : public QObject, public CppParser
Q_OBJECT
public:
explicit QuickTestParser(ITestFramework *framework);
void init(const Utils::FilePaths &filesToParse, bool fullParse) override;
void init(const QSet<Utils::FilePath> &filesToParse, bool fullParse) override;
void release() override;
bool processDocument(QPromise<TestParseResultPtr> &promise,
const Utils::FilePath &fileName) override;

View File

@@ -87,7 +87,7 @@ void TestCodeParser::setState(State state)
m_postponedUpdateType = UpdateType::NoUpdate;
qCDebug(LOG) << "calling scanForTests with postponed files (setState)";
if (!m_reparseTimer.isActive())
scanForTests(Utils::toList(m_postponedFiles));
scanForTests(m_postponedFiles);
}
}
}
@@ -213,27 +213,28 @@ void TestCodeParser::aboutToShutdown()
m_futureSynchronizer.waitForFinished();
}
bool TestCodeParser::postponed(const FilePaths &fileList)
bool TestCodeParser::postponed(const QSet<FilePath> &filePaths)
{
switch (m_parserState) {
case Idle:
if (fileList.size() == 1) {
if (filePaths.size() == 1) {
if (m_reparseTimerTimedOut)
return false;
const FilePath filePath = *filePaths.begin();
switch (m_postponedFiles.size()) {
case 0:
m_postponedFiles.insert(fileList.first());
m_postponedFiles.insert(filePath);
m_reparseTimer.setInterval(1000);
m_reparseTimer.start();
return true;
case 1:
if (m_postponedFiles.contains(fileList.first())) {
if (m_postponedFiles.contains(filePath)) {
m_reparseTimer.start();
return true;
}
Q_FALLTHROUGH();
default:
m_postponedFiles.insert(fileList.first());
m_postponedFiles.insert(filePath);
m_reparseTimer.stop();
m_reparseTimer.setInterval(0);
m_reparseTimerTimedOut = false;
@@ -245,7 +246,7 @@ bool TestCodeParser::postponed(const FilePaths &fileList)
case PartialParse:
case FullParse:
// parse is running, postponing a full parse
if (fileList.isEmpty()) {
if (filePaths.isEmpty()) {
m_postponedFiles.clear();
m_postponedUpdateType = UpdateType::FullUpdate;
qCDebug(LOG) << "Canceling scanForTest (full parse triggered while running a scan)";
@@ -255,8 +256,7 @@ bool TestCodeParser::postponed(const FilePaths &fileList)
if (m_postponedUpdateType == UpdateType::FullUpdate)
return true;
// partial parse triggered, postpone or add current files to already postponed partial
for (const FilePath &file : fileList)
m_postponedFiles.insert(file);
m_postponedFiles += filePaths;
m_postponedUpdateType = UpdateType::PartialUpdate;
}
return true;
@@ -277,31 +277,32 @@ static void parseFileForTests(QPromise<TestParseResultPtr> &promise,
}
}
void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestParser *> &parsers)
void TestCodeParser::scanForTests(const QSet<FilePath> &filePaths,
const QList<ITestParser *> &parsers)
{
if (m_parserState == Shutdown || m_testCodeParsers.isEmpty())
return;
if (postponed(fileList))
if (postponed(filePaths))
return;
QSet<FilePath> files = filePaths; // avoid getting cleared if m_postponedFiles have been passed
m_reparseTimer.stop();
m_reparseTimerTimedOut = false;
m_postponedFiles.clear();
bool isFullParse = fileList.isEmpty();
const bool isFullParse = files.isEmpty();
Project *project = ProjectManager::startupProject();
if (!project)
return;
FilePaths list;
if (isFullParse) {
list = project->files(Project::SourceFiles);
if (list.isEmpty()) {
files = Utils::toSet(project->files(Project::SourceFiles));
if (files.isEmpty()) {
// at least project file should be there, but might happen if parsing current project
// takes too long, especially when opening sessions holding multiple projects
qCDebug(LOG) << "File list empty (FullParse) - trying again in a sec";
emitUpdateTestTree();
return;
} else if (list.size() == 1 && list.first() == project->projectFilePath()) {
} else if (files.size() == 1 && *files.constBegin() == project->projectFilePath()) {
qCDebug(LOG) << "File list contains only the project file.";
return;
}
@@ -309,7 +310,6 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
qCDebug(LOG) << "setting state to FullParse (scanForTests)";
m_parserState = FullParse;
} else {
list << fileList;
qCDebug(LOG) << "setting state to PartialParse (scanForTests)";
m_parserState = PartialParse;
}
@@ -318,7 +318,7 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
TestTreeModel::instance()->updateCheckStateCache();
if (isFullParse) {
// remove qml files as they will be found automatically by the referencing cpp file
list = Utils::filtered(list, [](const FilePath &fn) { return !fn.endsWith(".qml"); });
files = Utils::filtered(files, [](const FilePath &fn) { return !fn.endsWith(".qml"); });
if (!parsers.isEmpty()) {
for (ITestParser *parser : parsers)
parser->framework()->rootNode()->markForRemovalRecursively(true);
@@ -326,15 +326,14 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
emit requestRemoveAllFrameworkItems();
}
} else if (!parsers.isEmpty()) {
const auto set = Utils::toSet(list);
for (ITestParser *parser: parsers) {
parser->framework()->rootNode()->markForRemovalRecursively(set);
parser->framework()->rootNode()->markForRemovalRecursively(files);
}
} else {
emit requestRemoval(Utils::toSet(list));
emit requestRemoval(files);
}
QTC_ASSERT(!(isFullParse && list.isEmpty()), onFinished(true); return);
QTC_ASSERT(!(isFullParse && files.isEmpty()), onFinished(true); return);
// use only a single parser or all current active?
const QList<ITestParser *> codeParsers = parsers.isEmpty() ? m_testCodeParsers : parsers;
@@ -343,14 +342,14 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
const auto cppSnapshot = CppEditor::CppModelManager::instance()->snapshot();
for (ITestParser *parser : codeParsers) {
parser->init(list, isFullParse);
parser->init(files, isFullParse);
for (const QString &ext : parser->supportedExtensions())
extensions.insert(ext);
}
// We are only interested in files that have been either parsed by the c++ parser,
// or have an extension that one of the parsers is specifically interested in.
const FilePaths filteredList
= Utils::filtered(list, [&extensions, &cppSnapshot](const FilePath &fn) {
const QSet<FilePath> filteredFiles
= Utils::filtered(files, [&extensions, &cppSnapshot](const FilePath &fn) {
const bool isSupportedExtension = Utils::anyOf(extensions, [&fn](const QString &ext) {
return fn.suffix() == ext;
});
@@ -359,13 +358,13 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
return cppSnapshot.contains(fn);
});
qCDebug(LOG) << "Starting scan of" << filteredList.size() << "(" << list.size() << ")"
qCDebug(LOG) << "Starting scan of" << filteredFiles.size() << "(" << files.size() << ")"
<< "files with" << codeParsers.size() << "parsers";
using namespace Tasking;
QList<TaskItem> tasks{parallel}; // TODO: use ParallelLimit(N) and add to settings?
for (const FilePath &file : filteredList) {
for (const FilePath &file : filteredFiles) {
const auto setup = [this, codeParsers, file](AsyncTask<TestParseResultPtr> &async) {
async.setConcurrentCallData(parseFileForTests, codeParsers, file);
async.setThreadPool(m_threadPool);
@@ -384,7 +383,7 @@ void TestCodeParser::scanForTests(const FilePaths &fileList, const QList<ITestPa
connect(m_taskTree.get(), &TaskTree::started, this, &TestCodeParser::parsingStarted);
connect(m_taskTree.get(), &TaskTree::done, this, onDone);
connect(m_taskTree.get(), &TaskTree::errorOccurred, this, onError);
if (filteredList.size() > 5) {
if (filteredFiles.size() > 5) {
auto progress = new TaskProgress(m_taskTree.get());
progress->setDisplayName(Tr::tr("Scanning for Tests"));
progress->setId(Constants::TASK_PARSE);
@@ -467,7 +466,7 @@ void TestCodeParser::onPartialParsingFinished()
case UpdateType::PartialUpdate:
qCDebug(LOG) << "calling scanForTests with postponed files (onPartialParsingFinished)";
if (!m_reparseTimer.isActive())
scanForTests(Utils::toList(m_postponedFiles));
scanForTests(m_postponedFiles);
break;
case UpdateType::NoUpdate:
m_dirty |= m_codeModelParsing;
@@ -491,7 +490,7 @@ void TestCodeParser::onPartialParsingFinished()
void TestCodeParser::parsePostponedFiles()
{
m_reparseTimerTimedOut = true;
scanForTests(Utils::toList(m_postponedFiles));
scanForTests(m_postponedFiles);
}
void TestCodeParser::releaseParserInternals()

View File

@@ -66,8 +66,8 @@ public:
void aboutToShutdown();
private:
bool postponed(const Utils::FilePaths &fileList);
void scanForTests(const Utils::FilePaths &fileList = Utils::FilePaths(),
bool postponed(const QSet<Utils::FilePath> &fileList);
void scanForTests(const QSet<Utils::FilePath> &filePaths = {},
const QList<ITestParser *> &parsers = {});
// qml files must be handled slightly different