tst_bench_QCryptographicHash: add benchmarks for QMessageAuthenticationCode

We could add a tst_QMessageAuthenticationCode, but it would have to
duplicate a lot of the tst_QCryptographicHash machinery, so just add
it here.

Pick-to: 6.5 6.2 5.15
Change-Id: Icc60de865c72c5e423cb3be57f58297c522791f7
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-03-22 09:02:45 +01:00
parent 3cf1a10e4f
commit 3aaae083f7

View File

@ -1,3 +1,4 @@
// Copyright (C) 2023 The Qt Company Ltd.
// Copyright (C) 2017 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
@ -5,11 +6,13 @@
#include <QCryptographicHash>
#include <QFile>
#include <QMetaEnum>
#include <QMessageAuthenticationCode>
#include <QRandomGenerator>
#include <QString>
#include <QTest>
#include <functional>
#include <numeric>
#include <time.h>
@ -30,6 +33,14 @@ private Q_SLOTS:
void addData();
void addDataChunked_data() { hash_data(); }
void addDataChunked();
// QMessageAuthenticationCode:
void hmac_hash_data() { hash_data(); }
void hmac_hash();
void hmac_addData_data() { hash_data(); }
void hmac_addData();
void hmac_setKey_data();
void hmac_setKey();
};
const int MaxBlockSize = 65536;
@ -120,6 +131,75 @@ void tst_QCryptographicHash::addDataChunked()
}
}
static QByteArray hmacKey() {
static QByteArray key = [] {
QByteArray result(277, Qt::Uninitialized);
std::iota(result.begin(), result.end(), uchar(0)); // uchar so wraps after UCHAR_MAX
return result;
}();
return key;
}
void tst_QCryptographicHash::hmac_hash()
{
QFETCH(const Algorithm, algo);
QFETCH(const QByteArray, data);
const auto key = hmacKey();
QBENCHMARK {
[[maybe_unused]]
auto r = QMessageAuthenticationCode::hash(data, key, algo);
}
}
void tst_QCryptographicHash::hmac_addData()
{
QFETCH(const Algorithm, algo);
QFETCH(const QByteArray, data);
const auto key = hmacKey();
QMessageAuthenticationCode mac(algo, key);
QBENCHMARK {
mac.reset();
mac.addData(data);
[[maybe_unused]]
auto r = mac.result();
}
}
void tst_QCryptographicHash::hmac_setKey_data()
{
QTest::addColumn<Algorithm>("algo");
for_each_algorithm([] (Algorithm algo, const char *name) {
if (algo == Algorithm::NumAlgorithms)
return;
QTest::addRow("%s", name) << algo;
});
}
void tst_QCryptographicHash::hmac_setKey()
{
QFETCH(const Algorithm, algo);
const QByteArrayList keys = [] {
QByteArrayList result;
const auto fullKey = hmacKey();
result.reserve(fullKey.size());
for (auto i = fullKey.size(); i > 0; --i)
result.push_back(fullKey.sliced(i));
return result;
}();
QMessageAuthenticationCode mac(algo);
QBENCHMARK {
for (const auto &key : keys) {
mac.setKey(key);
mac.addData("abc", 3); // avoid lazy setKey()
}
}
}
QTEST_APPLESS_MAIN(tst_QCryptographicHash)
#include "tst_bench_qcryptographichash.moc"