File Metadata

Author
bruns
Created
Jun 14 2019, 11:01 PM

qre_bench.cpp

/*
* Compile with:
* moc-qt5 qre_bench.cpp > qre_bench.moc
* g++ -fPIC -I /usr/include/qt5/ -I /usr/include/qt5/QtCore/ -I /usr/include/qt5/QtTest/ -O2 -lQt5Core -lQt5Test -o qre_bench qre_bench.cpp
*/
#include <qtest.h>
#include <QRegularExpression>
class TestBenchmark : public QObject
{
Q_OBJECT
private slots:
void bench_data();
void bench();
};
void TestBenchmark::bench_data()
{
QTest::addColumn<int>("type");
QTest::addColumn<int>("matches");
QTest::newRow("functional 1") << 0 << 1;
QTest::newRow("functional 10") << 0 << 10;
QTest::newRow("bench static 1") << 1 << 1;
QTest::newRow("bench local 1") << 2 << 1;
QTest::newRow("bench static 100") << 1 << 100;
QTest::newRow("bench local 100") << 2 << 100;
}
void TestBenchmark::bench()
{
QFETCH(int, type);
QFETCH(int, matches);
QString pattern = QLatin1String("id\\s*?=\\s*?(['\"])(\\d+?-\\d+?-.*?)\\1");
QString needle = QLatin1String("some text id=\"22-33-asdf\" more text id = '11-22-l' last text ");
QString hay = QLatin1String("some text id=\"1234\" more text id = 'xxx' last text ");
QString haystack;
haystack.reserve(matches * hay.size());
for (int i = 0; i < matches; i++) {
haystack.append(hay);
}
haystack = haystack + needle + haystack;
int result;
if (type == 0) {
QStringList results;
const static QRegularExpression re(pattern);
auto matchIt = re.globalMatch(haystack);
while (matchIt.hasNext()) {
auto id = matchIt.next().captured(2);
qDebug() << "found" << id;
results.append(id);
}
QCOMPARE(results.size(), 2);
QCOMPARE(results.at(0), QLatin1String("22-33-asdf"));
QCOMPARE(results.at(1), QLatin1String("11-22-l"));
} else if (type == 1) {
QBENCHMARK {
QStringList results;
const static QRegularExpression re(pattern);
auto matchIt = re.globalMatch(haystack);
while (matchIt.hasNext()) {
results.append(matchIt.next().captured(2));
}
}
} else {
QBENCHMARK {
QStringList results;
QRegularExpression re(pattern);
auto matchIt = re.globalMatch(haystack);
while (matchIt.hasNext()) {
results.append(matchIt.next().captured(2));
}
}
}
Q_UNUSED(result);
}
QTEST_MAIN(TestBenchmark)
#include "qre_bench.moc"