File indexing completed on 2025-04-20 03:38:12
0001 /* 0002 SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "../src/kcodecsbase64.h" 0008 0009 #include <KCodecs> 0010 0011 #include <QByteArray> 0012 #include <QObject> 0013 #include <QTest> 0014 0015 class Base64Benchmark : public QObject 0016 { 0017 Q_OBJECT 0018 private: 0019 static QByteArray fillByteArray(int size) 0020 { 0021 char c = 0; 0022 QByteArray result; 0023 result.reserve(size); 0024 while (result.size() < size) { 0025 result.append(c++); 0026 } 0027 return result; 0028 } 0029 0030 void createTestSet() 0031 { 0032 QTest::addColumn<QByteArray>("output"); 0033 QTest::addColumn<QByteArray>("input"); 0034 QTest::newRow("empty") << QByteArray() << QByteArray(); 0035 QTest::newRow("128") << fillByteArray(128) << KCodecs::base64Encode(fillByteArray(128)); 0036 QTest::newRow("1k") << fillByteArray(1 << 10) << KCodecs::base64Encode(fillByteArray(1 << 10)); 0037 QTest::newRow("1M") << fillByteArray(1 << 20) << KCodecs::base64Encode(fillByteArray(1 << 20)); 0038 } 0039 private Q_SLOTS: 0040 void benchmarkKCodecDecode_data() 0041 { 0042 createTestSet(); 0043 } 0044 0045 void benchmarkKCodecDecode() 0046 { 0047 QFETCH(QByteArray, input); 0048 QFETCH(QByteArray, output); 0049 QByteArray result; 0050 QBENCHMARK { 0051 result = KCodecs::base64Decode(input); 0052 } 0053 QCOMPARE(result, output); 0054 } 0055 0056 void benchmarkQByteArrayDecode_data() 0057 { 0058 createTestSet(); 0059 } 0060 0061 void benchmarkQByteArrayDecode() 0062 { 0063 QFETCH(QByteArray, input); 0064 QFETCH(QByteArray, output); 0065 QByteArray result; 0066 QBENCHMARK { 0067 result = QByteArray::fromBase64(input); 0068 } 0069 QCOMPARE(result, output); 0070 } 0071 0072 void benchmarkKMimeBase64Decoder_data() 0073 { 0074 createTestSet(); 0075 } 0076 0077 void benchmarkKMimeBase64Decoder() 0078 { 0079 QFETCH(QByteArray, input); 0080 QFETCH(QByteArray, output); 0081 QByteArray result; 0082 QBENCHMARK { 0083 KCodecs::Codec *codec = KCodecs::Codec::codecForName("base64"); 0084 QVERIFY(codec); 0085 result.resize(codec->maxDecodedSizeFor(input.size())); 0086 KCodecs::Decoder *decoder = codec->makeDecoder(); 0087 QByteArray::const_iterator inputIt = input.constBegin(); 0088 QByteArray::iterator resultIt = result.begin(); 0089 decoder->decode(inputIt, input.constEnd(), resultIt, result.constEnd()); 0090 result.truncate(resultIt - result.begin()); 0091 delete decoder; 0092 } 0093 QCOMPARE(result, output); 0094 } 0095 }; 0096 0097 QTEST_MAIN(Base64Benchmark) 0098 0099 #include "base64benchmark.moc"