File indexing completed on 2023-10-01 03:59:08
0001 /* 0002 This file is part of the KDE Baloo project. 0003 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "positioncodec.h" 0009 #include "positioninfo.h" 0010 0011 #include <QTest> 0012 0013 using namespace Baloo; 0014 0015 class PositionCodecBenchmark : public QObject 0016 { 0017 Q_OBJECT 0018 private Q_SLOTS: 0019 void initTestCase(); 0020 // data 1 - small number of documents, each with many positions 0021 void benchEncodeData1(); 0022 void benchDecodeData1(); 0023 // data 2 - large number of documents, few positions (10) each 0024 void benchEncodeData2(); 0025 void benchDecodeData2(); 0026 // data 3 - small number of documents, many positions with large increment 0027 void benchEncodeData3(); 0028 void benchDecodeData3(); 0029 private: 0030 QVector<PositionInfo> m_benchmarkData1; 0031 QVector<PositionInfo> m_benchmarkData2; 0032 QVector<PositionInfo> m_benchmarkData3; 0033 }; 0034 0035 void PositionCodecBenchmark::initTestCase() 0036 { 0037 /* 0038 * Same dataset as in autotests/unit/codecs/positioncodectest.cpp 0039 * Correctness of encoding/decoding is checked there. 0040 */ 0041 m_benchmarkData1.clear(); 0042 m_benchmarkData1.reserve(100); 0043 for(int i = 0; i < 100; ++i) 0044 { 0045 PositionInfo info; 0046 info.docId = (i + 1) * 4711; 0047 info.positions.reserve(3000); 0048 for (int j = 0; j < 3000; j++) { 0049 info.positions.append(((j + 1) * (i + 2))); 0050 } 0051 m_benchmarkData1.append(info); 0052 } 0053 0054 m_benchmarkData2.clear(); 0055 m_benchmarkData2.reserve(5000); 0056 for (int i = 0; i < 5000; i++) { 0057 PositionInfo info; 0058 info.docId = i; 0059 info.positions = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 0060 0061 m_benchmarkData2.append(info); 0062 } 0063 0064 m_benchmarkData3.clear(); 0065 m_benchmarkData3.reserve(200); 0066 for (int i = 0; i < 200; i++) { 0067 PositionInfo info; 0068 info.docId = i; 0069 info.positions.reserve(30000); // > 2^14 -> 3 byte VarInt32 0070 for (int j = 0; j < 30000; j++) { 0071 info.positions.append((j + 1) * 200); // increment 200 -> 2 byte DiffVarInt32 0072 } 0073 0074 m_benchmarkData3.append(info); 0075 } 0076 } 0077 0078 void PositionCodecBenchmark::benchEncodeData1() 0079 { 0080 QBENCHMARK { PositionCodec::encode(m_benchmarkData1); } 0081 } 0082 0083 void PositionCodecBenchmark::benchDecodeData1() 0084 { 0085 const QByteArray ba = PositionCodec::encode(m_benchmarkData1); 0086 QBENCHMARK { PositionCodec::decode(ba); } 0087 } 0088 0089 void PositionCodecBenchmark::benchEncodeData2() 0090 { 0091 QBENCHMARK { PositionCodec::encode(m_benchmarkData2); } 0092 } 0093 0094 void PositionCodecBenchmark::benchDecodeData2() 0095 { 0096 const QByteArray ba = PositionCodec::encode(m_benchmarkData2); 0097 QBENCHMARK { PositionCodec::decode(ba); } 0098 } 0099 0100 void PositionCodecBenchmark::benchEncodeData3() 0101 { 0102 QBENCHMARK { PositionCodec::encode(m_benchmarkData3); } 0103 } 0104 0105 void PositionCodecBenchmark::benchDecodeData3() 0106 { 0107 const QByteArray ba = PositionCodec::encode(m_benchmarkData3); 0108 QBENCHMARK { PositionCodec::decode(ba); } 0109 } 0110 0111 QTEST_MAIN(PositionCodecBenchmark) 0112 0113 #include "positioncodecbenchmark.moc"