1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: MIT
*/
#include "test-config.h"
#include <KSyntaxHighlighting/AbstractHighlighter>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Format>
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/State>
#include <QDir>
#include <QObject>
#include <QTest>
using namespace KSyntaxHighlighting;
class NullHighlighter : public AbstractHighlighter
{
public:
/**
* Read in the given file and cache it for the highlighting benchmarking
* @param inFileName file to read
*/
NullHighlighter(const QString &inFileName)
{
QFile f(inFileName);
if (!f.open(QFile::ReadOnly)) {
qWarning() << "Failed to open input file" << inFileName << ":" << f.errorString();
return;
}
QTextStream in(&f);
while (!in.atEnd()) {
m_fileContents.append(in.readLine());
}
}
/**
* highlight the in-memory stored file
* @return number of highlighted lines
*/
int highlightFile()
{
State state;
for (const auto &line : std::as_const(m_fileContents)) {
state = highlightLine(line, state);
}
return m_fileContents.size();
}
protected:
void applyFormat(int, int, const Format &) override
{
}
QStringList m_fileContents;
};
class HighlighterBenchmark : public QObject
{
Q_OBJECT
public:
explicit HighlighterBenchmark(QObject *parent = nullptr)
: QObject(parent)
{
}
private:
Repository m_repo;
private Q_SLOTS:
void initTestCase()
{
initRepositorySearchPaths(m_repo);
}
void cleanupTestCase()
{
}
void benchmarkHighlight_data()
{
QTest::addColumn<QString>("inFile");
QTest::addColumn<QString>("syntax");
const QDir dir(QStringLiteral(TESTSRCDIR "/input"));
for (const auto &fileName : dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable | QDir::Hidden, QDir::Name)) {
if (fileName == QLatin1String(".clang-format")) {
continue;
}
const auto inFile = dir.absoluteFilePath(fileName);
if (inFile.endsWith(QLatin1String(".syntax"))) {
continue;
}
QString syntax;
QFile syntaxOverride(inFile + QStringLiteral(".syntax"));
if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) {
syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed();
}
QTest::newRow(fileName.toUtf8().constData()) << inFile << syntax;
}
}
void benchmarkHighlight()
{
QFETCH(QString, inFile);
QFETCH(QString, syntax);
NullHighlighter highlighter(inFile);
auto def = m_repo.definitionForFileName(inFile);
if (!syntax.isEmpty()) {
def = m_repo.definitionForName(syntax);
}
QVERIFY(def.isValid());
highlighter.setDefinition(def);
// trigger loading of definition per benchmarking loop
QVERIFY(!def.formats().isEmpty());
// benchmark the highlighting
// try to highlight ~ 20000 lines per file
// bail out, if file is empty, else we are stuck
for (int i = 0; i <= 20000;) {
int lines = highlighter.highlightFile();
if (lines <= 0) {
break;
}
i += lines;
}
}
};
QTEST_GUILESS_MAIN(HighlighterBenchmark)
#include "highlighter_benchmark.moc"
|