diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2014-11-30 17:57:31 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2014-11-30 17:57:31 +0100 |
commit | 7cbf25f2f6d38efa384c7f8e0a5b2b2d274ebb00 (patch) | |
tree | 583e0a4bdebe15c73d9860e78480517034e6ca9f /store/test/storagebenchmark.cpp | |
parent | 7373f1a92c2d6d0790f6a47a011c107c32064cc8 (diff) | |
download | sink-7cbf25f2f6d38efa384c7f8e0a5b2b2d274ebb00.tar.gz sink-7cbf25f2f6d38efa384c7f8e0a5b2b2d274ebb00.zip |
Moved buffertest to store/test and turned it into a reproducible benchmark.
Diffstat (limited to 'store/test/storagebenchmark.cpp')
-rw-r--r-- | store/test/storagebenchmark.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/store/test/storagebenchmark.cpp b/store/test/storagebenchmark.cpp new file mode 100644 index 0000000..939ae0b --- /dev/null +++ b/store/test/storagebenchmark.cpp | |||
@@ -0,0 +1,132 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include "calendar_generated.h" | ||
4 | #include <iostream> | ||
5 | #include <fstream> | ||
6 | #include <QDir> | ||
7 | #include <QString> | ||
8 | #include <QTime> | ||
9 | #include <qdebug.h> | ||
10 | |||
11 | #include "store/database.h" | ||
12 | |||
13 | using namespace Calendar; | ||
14 | using namespace flatbuffers; | ||
15 | |||
16 | static std::string createEvent() | ||
17 | { | ||
18 | FlatBufferBuilder fbb; | ||
19 | { | ||
20 | auto summary = fbb.CreateString("summary"); | ||
21 | |||
22 | const int attachmentSize = 1024*2; // 2KB | ||
23 | int8_t rawData[attachmentSize]; | ||
24 | auto data = fbb.CreateVector(rawData, attachmentSize); | ||
25 | |||
26 | Calendar::EventBuilder eventBuilder(fbb); | ||
27 | eventBuilder.add_summary(summary); | ||
28 | eventBuilder.add_attachment(data); | ||
29 | auto eventLocation = eventBuilder.Finish(); | ||
30 | FinishEventBuffer(fbb, eventLocation); | ||
31 | } | ||
32 | return std::string(reinterpret_cast<const char *>(fbb.GetBufferPointer()), fbb.GetSize()); | ||
33 | } | ||
34 | |||
35 | static void readEvent(const std::string &data) | ||
36 | { | ||
37 | auto readEvent = GetEvent(data.c_str()); | ||
38 | std::cout << readEvent->summary()->c_str() << std::endl; | ||
39 | } | ||
40 | |||
41 | class StorageBenchmark : public QObject | ||
42 | { | ||
43 | Q_OBJECT | ||
44 | private: | ||
45 | //This should point to a directory on disk and not a ramdisk (since we're measuring performance) | ||
46 | QString testDataPath; | ||
47 | QString dbPath; | ||
48 | QString filePath; | ||
49 | |||
50 | private Q_SLOTS: | ||
51 | void initTestCase() | ||
52 | { | ||
53 | testDataPath = "./"; | ||
54 | dbPath = testDataPath + "testdb"; | ||
55 | filePath = testDataPath + "buffer.fb"; | ||
56 | |||
57 | QDir dir(testDataPath); | ||
58 | dir.remove("testdb/data.mdb"); | ||
59 | dir.remove("testdb/lock.mdb"); | ||
60 | dir.remove(filePath); | ||
61 | } | ||
62 | |||
63 | void testWriteRead_data() | ||
64 | { | ||
65 | QTest::addColumn<bool>("useDb"); | ||
66 | QTest::addColumn<int>("count"); | ||
67 | |||
68 | const int count = 50000; | ||
69 | QTest::newRow("db, 50k") << true << count; | ||
70 | QTest::newRow("file, 50k") << false << count; | ||
71 | } | ||
72 | |||
73 | void testWriteRead() | ||
74 | { | ||
75 | QFETCH(bool, useDb); | ||
76 | QFETCH(int, count); | ||
77 | |||
78 | Database db(dbPath); | ||
79 | |||
80 | std::ofstream myfile; | ||
81 | myfile.open(filePath.toStdString()); | ||
82 | |||
83 | QTime time; | ||
84 | time.start(); | ||
85 | { | ||
86 | auto transaction = db.startTransaction(); | ||
87 | for (int i = 0; i < count; i++) { | ||
88 | const auto key = QString("key%1").arg(i); | ||
89 | auto event = createEvent(); | ||
90 | if (useDb) { | ||
91 | db.write(key.toStdString(), event, transaction); | ||
92 | } else { | ||
93 | myfile << event; | ||
94 | } | ||
95 | } | ||
96 | if (useDb) { | ||
97 | db.endTransaction(transaction); | ||
98 | } else { | ||
99 | myfile.close(); | ||
100 | } | ||
101 | } | ||
102 | const int writeDuration = time.elapsed(); | ||
103 | qDebug() << "Writing took[ms]: " << writeDuration; | ||
104 | |||
105 | time.start(); | ||
106 | { | ||
107 | for (int i = 0; i < count; i++) { | ||
108 | const auto key = QString("key%1").arg(i); | ||
109 | if (useDb) { | ||
110 | db.read(key.toStdString()); | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | const int readDuration = time.elapsed(); | ||
115 | if (useDb) { | ||
116 | qDebug() << "Reading took[ms]: " << readDuration; | ||
117 | } else { | ||
118 | qDebug() << "File reading is not implemented."; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | void testSizes() | ||
123 | { | ||
124 | QFileInfo dbInfo(dbPath, "data.mdb"); | ||
125 | QFileInfo fileInfo(filePath); | ||
126 | qDebug() << "Database size [kb]: " << dbInfo.size()/1024; | ||
127 | qDebug() << "File size [kb]: " << fileInfo.size()/1024; | ||
128 | } | ||
129 | }; | ||
130 | |||
131 | QTEST_MAIN(StorageBenchmark) | ||
132 | #include "storagebenchmark.moc" | ||