File: base64benchmark.cpp

package info (click to toggle)
kf6-kcodecs 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,784 kB
  • sloc: cpp: 8,843; sh: 14; makefile: 5
file content (99 lines) | stat: -rw-r--r-- 2,627 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
    SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "../src/kcodecsbase64.h"

#include <KCodecs>

#include <QByteArray>
#include <QObject>
#include <QTest>

class Base64Benchmark : public QObject
{
    Q_OBJECT
private:
    static QByteArray fillByteArray(int size)
    {
        char c = 0;
        QByteArray result;
        result.reserve(size);
        while (result.size() < size) {
            result.append(c++);
        }
        return result;
    }

    void createTestSet()
    {
        QTest::addColumn<QByteArray>("output");
        QTest::addColumn<QByteArray>("input");
        QTest::newRow("empty") << QByteArray() << QByteArray();
        QTest::newRow("128") << fillByteArray(128) << KCodecs::base64Encode(fillByteArray(128));
        QTest::newRow("1k") << fillByteArray(1 << 10) << KCodecs::base64Encode(fillByteArray(1 << 10));
        QTest::newRow("1M") << fillByteArray(1 << 20) << KCodecs::base64Encode(fillByteArray(1 << 20));
    }
private Q_SLOTS:
    void benchmarkKCodecDecode_data()
    {
        createTestSet();
    }

    void benchmarkKCodecDecode()
    {
        QFETCH(QByteArray, input);
        QFETCH(QByteArray, output);
        QByteArray result;
        QBENCHMARK {
            result = KCodecs::base64Decode(input);
        }
        QCOMPARE(result, output);
    }

    void benchmarkQByteArrayDecode_data()
    {
        createTestSet();
    }

    void benchmarkQByteArrayDecode()
    {
        QFETCH(QByteArray, input);
        QFETCH(QByteArray, output);
        QByteArray result;
        QBENCHMARK {
            result = QByteArray::fromBase64(input);
        }
        QCOMPARE(result, output);
    }

    void benchmarkKMimeBase64Decoder_data()
    {
        createTestSet();
    }

    void benchmarkKMimeBase64Decoder()
    {
        QFETCH(QByteArray, input);
        QFETCH(QByteArray, output);
        QByteArray result;
        QBENCHMARK {
            KCodecs::Codec *codec = KCodecs::Codec::codecForName("base64");
            QVERIFY(codec);
            result.resize(codec->maxDecodedSizeFor(input.size()));
            KCodecs::Decoder *decoder = codec->makeDecoder();
            QByteArray::const_iterator inputIt = input.constBegin();
            QByteArray::iterator resultIt = result.begin();
            decoder->decode(inputIt, input.constEnd(), resultIt, result.constEnd());
            result.truncate(resultIt - result.begin());
            delete decoder;
        }
        QCOMPARE(result, output);
    }
};

QTEST_MAIN(Base64Benchmark)

#include "base64benchmark.moc"