File indexing completed on 2024-04-14 14:19:43
0001 /* 0002 This file is part of the KDE libraries 0003 0004 Copyright (C) 2011 Volker Krause <vkrause@kde.org> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include <kmd5.h> 0023 #include <qtest_kde.h> 0024 0025 #include <QCryptographicHash> 0026 0027 class KMd5Benchmark : public QObject 0028 { 0029 Q_OBJECT 0030 0031 private: 0032 static QByteArray makeByteArray(int size) 0033 { 0034 QByteArray b; 0035 b.resize(size); 0036 for (int i = 0; i < size; ++i) { 0037 b[i] = static_cast<char>(i % 255); 0038 } 0039 return b; 0040 } 0041 0042 static void makeData() 0043 { 0044 QTest::addColumn<QByteArray>("input"); 0045 QTest::newRow("empty") << QByteArray(); 0046 QTest::newRow("32") << makeByteArray(32); 0047 QTest::newRow("128") << makeByteArray(128); 0048 QTest::newRow("1024") << makeByteArray(1 << 10); 0049 QTest::newRow("1M") << makeByteArray(1 << 20); 0050 QTest::newRow("16M") << makeByteArray(1 << 24); 0051 } 0052 0053 private Q_SLOTS: 0054 void benchmarkKMd5_data() 0055 { 0056 makeData(); 0057 } 0058 0059 void benchmarkKMd5() 0060 { 0061 QFETCH(QByteArray, input); 0062 QBENCHMARK { 0063 KMD5 md5(input); 0064 md5.rawDigest(); 0065 } 0066 } 0067 0068 void benchmarkKMd5WithReset_data() 0069 { 0070 makeData(); 0071 } 0072 0073 void benchmarkKMd5WithReset() 0074 { 0075 QFETCH(QByteArray, input); 0076 KMD5 md5; 0077 QBENCHMARK { 0078 md5.reset(); 0079 md5.update(input); 0080 md5.rawDigest(); 0081 } 0082 } 0083 0084 void benchmarkQCH_data() 0085 { 0086 makeData(); 0087 } 0088 0089 void benchmarkQCH() 0090 { 0091 QFETCH(QByteArray, input); 0092 QBENCHMARK { 0093 QCryptographicHash h(QCryptographicHash::Md5); 0094 h.addData(input); 0095 h.result(); 0096 } 0097 } 0098 0099 void benchmarkQCHWithReset_data() 0100 { 0101 makeData(); 0102 } 0103 0104 void benchmarkQCHWithReset() 0105 { 0106 QFETCH(QByteArray, input); 0107 QCryptographicHash h(QCryptographicHash::Md5); 0108 QBENCHMARK { 0109 h.reset(); 0110 h.addData(input); 0111 h.result(); 0112 } 0113 } 0114 0115 }; 0116 0117 QTEST_KDEMAIN_CORE(KMd5Benchmark) 0118 0119 #include "kmd5benchmark.moc"