summaryrefslogtreecommitdiffstats
path: root/tests/storagebenchmark.cpp
blob: eef360ef297d8d4a7ff6629f0857c1482d72651f (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <QtTest>

#include "event_generated.h"

#include "hawd/dataset.h"
#include "hawd/formatter.h"
#include "common/storage.h"
#include "common/log.h"

#include <fstream>

#include <QDebug>
#include <QString>
#include <QTime>

using namespace Sink::ApplicationDomain::Buffer;
using namespace flatbuffers;

static QByteArray createEvent()
{
    static const size_t attachmentSize = 1024 * 2; // 2KB
    static uint8_t rawData[attachmentSize];
    static FlatBufferBuilder fbb;
    fbb.Clear();
    {
        uint8_t *rawDataPtr = Q_NULLPTR;
        auto summary = fbb.CreateString("summary");
        EventBuilder eventBuilder(fbb);
        eventBuilder.add_summary(summary);
        auto eventLocation = eventBuilder.Finish();
        FinishEventBuffer(fbb, eventLocation);
        memcpy((void *)rawDataPtr, rawData, attachmentSize);
    }

    return QByteArray::fromRawData(reinterpret_cast<const char *>(fbb.GetBufferPointer()), fbb.GetSize());
}

/**
 * Benchmark the storage implementation.
 */
class StorageBenchmark : public QObject
{
    Q_OBJECT
private:
    // This should point to a directory on disk and not a ramdisk (since we're measuring performance)
    QString testDataPath;
    QString dbName;
    QString filePath;
    const int count = 50000;

private slots:
    void initTestCase()
    {
        Sink::Log::setDebugOutputLevel(Sink::Log::Warning);
        testDataPath = "./testdb";
        dbName = "test";
        filePath = testDataPath + "buffer.fb";
    }

    void cleanupTestCase()
    {
        Sink::Storage::DataStore store(testDataPath, dbName);
        store.removeFromDisk();
    }

    void testWriteRead()
    {
        auto event = createEvent();

        QScopedPointer<Sink::Storage::DataStore> store(new Sink::Storage::DataStore(testDataPath, dbName, Sink::Storage::DataStore::ReadWrite));

        const char *keyPrefix = "key";

        QTime time;
        time.start();
        // Test db write time
        {
            auto transaction = store->createTransaction(Sink::Storage::DataStore::ReadWrite);
            for (int i = 0; i < count; i++) {
                transaction.openDatabase().write(keyPrefix + QByteArray::number(i), event);
                if ((i % 10000) == 0) {
                    transaction.commit();
                    transaction = store->createTransaction(Sink::Storage::DataStore::ReadWrite);
                }
            }
            transaction.commit();
        }
        qreal dbWriteDuration = time.restart();
        qreal dbWriteOpsPerMs = count / dbWriteDuration;

        // Test file write time
        {
            std::ofstream myfile;
            myfile.open(filePath.toStdString());
            for (int i = 0; i < count; i++) {
                myfile << event.toStdString();
            }
            myfile.close();
        }
        qreal fileWriteDuration = time.restart();
        qreal fileWriteOpsPerMs = count / fileWriteDuration;

        // Db read time
        {
            auto transaction = store->createTransaction(Sink::Storage::DataStore::ReadOnly);
            auto db = transaction.openDatabase();
            for (int i = 0; i < count; i++) {
                db.scan(keyPrefix + QByteArray::number(i), [](const QByteArray &key, const QByteArray &value) -> bool { return true; });
            }
        }
        qreal readDuration = time.restart();
        qreal readOpsPerMs = count / readDuration;

        HAWD::Dataset dataset("storage_readwrite", m_hawdState);
        HAWD::Dataset::Row row = dataset.row();
        row.setValue("rows", count);
        row.setValue("dbWrite", dbWriteOpsPerMs);
        row.setValue("fileWrite", fileWriteOpsPerMs);
        row.setValue("dbRead", readOpsPerMs);
        dataset.insertRow(row);
        HAWD::Formatter::print(dataset);

        {
            Sink::Storage::DataStore store(testDataPath, dbName);
            QFileInfo fileInfo(filePath);

            HAWD::Dataset dataset("storage_sizes", m_hawdState);
            HAWD::Dataset::Row row = dataset.row();
            row.setValue("rows", count);
            row.setValue("dbSize", store.diskUsage() / 1024);
            row.setValue("fileSize", fileInfo.size() / 1024);
            dataset.insertRow(row);
            HAWD::Formatter::print(dataset);
        }
    }

    void testScan()
    {
        QScopedPointer<Sink::Storage::DataStore> store(new Sink::Storage::DataStore(testDataPath, dbName, Sink::Storage::DataStore::ReadOnly));

        QBENCHMARK {
            int hit = 0;
            store->createTransaction(Sink::Storage::DataStore::ReadOnly)
                .openDatabase()
                .scan("", [&](const QByteArray &key, const QByteArray &value) -> bool {
                    if (key == "key10000") {
                        // qDebug() << "hit";
                        hit++;
                    }
                    return true;
                });
            QCOMPARE(hit, 1);
        }
    }

    void testKeyLookup()
    {
        QScopedPointer<Sink::Storage::DataStore> store(new Sink::Storage::DataStore(testDataPath, dbName, Sink::Storage::DataStore::ReadOnly));
        auto transaction = store->createTransaction(Sink::Storage::DataStore::ReadOnly);
        auto db = transaction.openDatabase();

        QBENCHMARK {
            int hit = 0;
            db.scan("key40000", [&](const QByteArray &key, const QByteArray &value) -> bool {
                hit++;
                return true;
            });
            QCOMPARE(hit, 1);
        }
    }

    void testFindLatest()
    {
        QScopedPointer<Sink::Storage::DataStore> store(new Sink::Storage::DataStore(testDataPath, dbName, Sink::Storage::DataStore::ReadOnly));
        auto transaction = store->createTransaction(Sink::Storage::DataStore::ReadOnly);
        auto db = transaction.openDatabase();

        QBENCHMARK {
            int hit = 0;
            db.findLatest("key40000", [&](const QByteArray &key, const QByteArray &value) -> bool {
                hit++;
                return true;
            });
            QCOMPARE(hit, 1);
        }
    }

    void testBufferCreation()
    {
        HAWD::Dataset dataset("buffer_creation", m_hawdState);
        HAWD::Dataset::Row row = dataset.row();

        QTime time;
        time.start();

        for (int i = 0; i < count; i++) {
            auto event = createEvent();
        }

        qreal bufferDuration = time.restart();
        qreal opsPerMs = count / bufferDuration;
        row.setValue("numBuffers", count);
        row.setValue("time", bufferDuration);
        row.setValue("ops", opsPerMs);
        dataset.insertRow(row);
        HAWD::Formatter::print(dataset);
    }


private:
    HAWD::State m_hawdState;
};

QTEST_MAIN(StorageBenchmark)
#include "storagebenchmark.moc"