Add a benchmark to compare the performance of our libarchive wrapper against KArchive

parent 42dba416
add_subdirectory(database)
\ No newline at end of file
find_package(KF5Archive)
set(CMAKE_AUTOMOC ON)
add_executable(archiving_benchmark archiving.cpp)
target_link_libraries(archiving_benchmark akabeicore KF5::Archive Qt5::Test)
add_subdirectory(database)
// This is a benchmark to compare the performance of our libarchive-based
// implementation to handle archives and KArchive.
#include <QChar>
#include <QString>
#include <QTemporaryFile>
#include <QTest>
#include <KF5/KArchive/KTar>
#include <libarchive++/archivehandler.h>
class ArchivingBenchmark : public QObject
{
Q_OBJECT
private:
QMap<QString, QStringList> fileSets;
const QString libarchive = QStringLiteral("libarchive");
const QString KArchive = QStringLiteral("KArchive");
private Q_SLOTS:
void initTestCase() {
qDebug() << "Start preparing files for the benchmark";
qsrand(1);
QList<QPair<int, int>> pairs{{1, 5000000}, {10000, 10}};
for (auto pair : pairs) {
int count(pair.first);
int size(pair.second);
QString label(QStringLiteral("%1 files of %2 bytes each").arg(
count).arg(size));
qDebug() << "Generating" << label;
QStringList paths;
for (int j = 0; j < count; ++j) {
QTemporaryFile file;
file.setAutoRemove(false);
file.open();
for (int k = 0; k < size; ++k) {
file.write(QString(QChar(32 + qrand() % 95)).toLatin1());
}
file.close();
paths.append(file.fileName());
}
fileSets.insert(label, paths);
}
qDebug() << "Finished preparing files for the benchmark";
}
void tets_data()
{
QTest::addColumn<QString>("backend");
QTest::addColumn<QString>("fileSet");
for (auto backend : {libarchive, KArchive}) {
for (auto fileSet : fileSets.keys()) {
QTest::newRow(QStringLiteral(
"Archive and extract %1 using %2").arg(
fileSet).arg(backend).toStdString().c_str())
<< backend << fileSet;
}
}
}
void tets() {
QFETCH(QString, backend);
QFETCH(QString, fileSet);
QStringList files = fileSets[fileSet];
QTemporaryFile archiveFile;
archiveFile.open();
archiveFile.close();
QString archiveFilePath(archiveFile.fileName());
QTemporaryDir extractedFolder;
QVERIFY(extractedFolder.isValid());
QString extractedFolderPath(extractedFolder.path());
QHash<QString, QString> entries;
int length(files.length());
for (int i = 0; i < length; ++i) {
QString file(files.at(i));
QString relativeFile(file.remove(QRegExp("^(\\w+:)?[/\\]")));
entries.insert(relativeFile, file);
}
if (backend == libarchive) {
QBENCHMARK_ONCE {
ArchiveHandler::write(archiveFilePath, entries);
ArchiveHandler handler(archiveFilePath);
handler.extractAll(extractedFolderPath, true);
}
} else { // backend == KArchive
QBENCHMARK_ONCE {
KTar tarWriter(archiveFilePath,
QStringLiteral("application/x-xz"));
tarWriter.open(QIODevice::WriteOnly);
for (auto relativeFile : entries.keys()) {
tarWriter.addLocalFile(entries[relativeFile], relativeFile);
}
tarWriter.close();
}
QBENCHMARK_ONCE {
// Use a separate instance for archiving and extracting, not to
// cheat.
KTar tarReader(archiveFilePath,
QStringLiteral("application/x-xz"));
tarReader.open(QIODevice::ReadOnly);
tarReader.directory()->copyTo(extractedFolderPath);
tarReader.close();
}
}
};
void cleanupTestCase() {
for (auto paths : fileSets.values()) {
for (auto path : paths) {
QFile file (path);
file.remove();
}
}
}
};
QTEST_MAIN(ArchivingBenchmark)
#include "archiving.moc"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment