diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tests/calendar.fbs | 12 | ||||
-rw-r--r-- | tests/storagebenchmark.cpp | 157 |
3 files changed, 185 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..23776a1 --- /dev/null +++ b/tests/CMakeLists.txt | |||
@@ -0,0 +1,16 @@ | |||
1 | set(CMAKE_AUTOMOC ON) | ||
2 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) | ||
3 | |||
4 | generate_flatbuffers(calendar) | ||
5 | |||
6 | macro(manual_tests) | ||
7 | foreach(_testname ${ARGN}) | ||
8 | add_executable(${_testname} ${_testname}.cpp) | ||
9 | qt5_use_modules(${_testname} Core Test) | ||
10 | target_link_libraries(${_testname} akonadinextcommon) | ||
11 | endforeach(_testname) | ||
12 | endmacro(manual_tests) | ||
13 | |||
14 | manual_tests ( | ||
15 | storagebenchmark | ||
16 | ) | ||
diff --git a/tests/calendar.fbs b/tests/calendar.fbs new file mode 100644 index 0000000..203ee43 --- /dev/null +++ b/tests/calendar.fbs | |||
@@ -0,0 +1,12 @@ | |||
1 | // example IDL file | ||
2 | |||
3 | namespace Calendar; | ||
4 | |||
5 | table Event { | ||
6 | summary:string; | ||
7 | description:string; | ||
8 | attachment:[byte]; | ||
9 | } | ||
10 | |||
11 | root_type Event; | ||
12 | file_identifier "AKFB"; | ||
diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp new file mode 100644 index 0000000..15da9da --- /dev/null +++ b/tests/storagebenchmark.cpp | |||
@@ -0,0 +1,157 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include "calendar_generated.h" | ||
4 | |||
5 | #include <iostream> | ||
6 | #include <fstream> | ||
7 | |||
8 | #include <QDebug> | ||
9 | #include <QDir> | ||
10 | #include <QString> | ||
11 | #include <QTime> | ||
12 | |||
13 | #include "common/storage.h" | ||
14 | |||
15 | using namespace Calendar; | ||
16 | using namespace flatbuffers; | ||
17 | |||
18 | static std::string createEvent() | ||
19 | { | ||
20 | FlatBufferBuilder fbb; | ||
21 | { | ||
22 | auto summary = fbb.CreateString("summary"); | ||
23 | |||
24 | const int attachmentSize = 1024*2; // 2KB | ||
25 | int8_t rawData[attachmentSize]; | ||
26 | auto data = fbb.CreateVector(rawData, attachmentSize); | ||
27 | |||
28 | Calendar::EventBuilder eventBuilder(fbb); | ||
29 | eventBuilder.add_summary(summary); | ||
30 | eventBuilder.add_attachment(data); | ||
31 | auto eventLocation = eventBuilder.Finish(); | ||
32 | FinishEventBuffer(fbb, eventLocation); | ||
33 | } | ||
34 | return std::string(reinterpret_cast<const char *>(fbb.GetBufferPointer()), fbb.GetSize()); | ||
35 | } | ||
36 | |||
37 | // static void readEvent(const std::string &data) | ||
38 | // { | ||
39 | // auto readEvent = GetEvent(data.c_str()); | ||
40 | // std::cout << readEvent->summary()->c_str() << std::endl; | ||
41 | // } | ||
42 | |||
43 | class StorageBenchmark : public QObject | ||
44 | { | ||
45 | Q_OBJECT | ||
46 | private: | ||
47 | //This should point to a directory on disk and not a ramdisk (since we're measuring performance) | ||
48 | QString testDataPath; | ||
49 | QString dbName; | ||
50 | QString filePath; | ||
51 | const int count = 50000; | ||
52 | |||
53 | private Q_SLOTS: | ||
54 | void initTestCase() | ||
55 | { | ||
56 | testDataPath = "./testdb"; | ||
57 | dbName = "test"; | ||
58 | filePath = testDataPath + "buffer.fb"; | ||
59 | } | ||
60 | |||
61 | void testWriteRead_data() | ||
62 | { | ||
63 | QTest::addColumn<bool>("useDb"); | ||
64 | QTest::addColumn<int>("count"); | ||
65 | |||
66 | QTest::newRow("db, 50k") << true << count; | ||
67 | QTest::newRow("file, 50k") << false << count; | ||
68 | } | ||
69 | |||
70 | void testWriteRead() | ||
71 | { | ||
72 | QFETCH(bool, useDb); | ||
73 | QFETCH(int, count); | ||
74 | |||
75 | Storage *store = 0; | ||
76 | if (useDb) { | ||
77 | store = new Storage(testDataPath, dbName); | ||
78 | } | ||
79 | |||
80 | std::ofstream myfile; | ||
81 | myfile.open(filePath.toStdString()); | ||
82 | const char *keyPrefix = "key"; | ||
83 | |||
84 | QTime time; | ||
85 | |||
86 | time.start(); | ||
87 | { | ||
88 | auto event = createEvent(); | ||
89 | for (int i = 0; i < count; i++) { | ||
90 | if (store) { | ||
91 | if (i % 10000 == 0) { | ||
92 | if (i > 0) { | ||
93 | store->commitTransaction(); | ||
94 | } | ||
95 | store->startTransaction(); | ||
96 | } | ||
97 | |||
98 | store->write(keyPrefix + std::to_string(i), event); | ||
99 | } else { | ||
100 | myfile << event; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | if (store) { | ||
105 | store->commitTransaction(); | ||
106 | } else { | ||
107 | myfile.close(); | ||
108 | } | ||
109 | } | ||
110 | const int writeDuration = time.restart(); | ||
111 | qDebug() << "Writing took[ms]: " << writeDuration; | ||
112 | |||
113 | { | ||
114 | for (int i = 0; i < count; i++) { | ||
115 | if (store) { | ||
116 | store->read(keyPrefix + std::to_string(i), [](std::string value){}); | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | const int readDuration = time.restart(); | ||
121 | |||
122 | if (store) { | ||
123 | qDebug() << "Reading took[ms]: " << readDuration; | ||
124 | } else { | ||
125 | qDebug() << "File reading is not implemented."; | ||
126 | } | ||
127 | |||
128 | delete store; | ||
129 | } | ||
130 | |||
131 | void testBufferCreation() | ||
132 | { | ||
133 | QTime time; | ||
134 | |||
135 | time.start(); | ||
136 | { | ||
137 | for (int i = 0; i < count; i++) { | ||
138 | auto event = createEvent(); | ||
139 | } | ||
140 | } | ||
141 | const int bufferDuration = time.elapsed(); | ||
142 | qDebug() << "Creating buffers took[ms]: " << bufferDuration; | ||
143 | } | ||
144 | |||
145 | void testSizes() | ||
146 | { | ||
147 | Storage store(testDataPath, dbName); | ||
148 | qDebug() << "Database size [kb]: " << store.diskUsage()/1024; | ||
149 | store.removeFromDisk(); | ||
150 | |||
151 | QFileInfo fileInfo(filePath); | ||
152 | qDebug() << "File size [kb]: " << fileInfo.size()/1024; | ||
153 | } | ||
154 | }; | ||
155 | |||
156 | QTEST_MAIN(StorageBenchmark) | ||
157 | #include "storagebenchmark.moc" | ||