summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
Diffstat (limited to 'store')
-rw-r--r--store/CMakeLists.txt1
-rw-r--r--store/database.cpp6
-rw-r--r--store/database.h3
-rw-r--r--store/test/CMakeLists.txt21
-rw-r--r--store/test/calendar.fbs12
-rw-r--r--store/test/storagebenchmark.cpp132
6 files changed, 171 insertions, 4 deletions
diff --git a/store/CMakeLists.txt b/store/CMakeLists.txt
new file mode 100644
index 0000000..552439e
--- /dev/null
+++ b/store/CMakeLists.txt
@@ -0,0 +1 @@
add_subdirectory(test)
diff --git a/store/database.cpp b/store/database.cpp
index d6fe7c0..c16a077 100644
--- a/store/database.cpp
+++ b/store/database.cpp
@@ -6,16 +6,16 @@
6#include <QTime> 6#include <QTime>
7#include <qdebug.h> 7#include <qdebug.h>
8 8
9Database::Database() 9Database::Database(const QString &path)
10{ 10{
11 int rc; 11 int rc;
12 12
13 QDir dir; 13 QDir dir;
14 dir.mkdir("./testdb"); 14 dir.mkdir(path);
15 15
16 //create file 16 //create file
17 rc = mdb_env_create(&env); 17 rc = mdb_env_create(&env);
18 rc = mdb_env_open(env, "./testdb", 0, 0664); 18 rc = mdb_env_open(env, path.toStdString().data(), 0, 0664);
19 const int dbSize = 10485760*100; //10MB * 100 19 const int dbSize = 10485760*100; //10MB * 100
20 mdb_env_set_mapsize(env, dbSize); 20 mdb_env_set_mapsize(env, dbSize);
21 21
diff --git a/store/database.h b/store/database.h
index b42b9f7..1a124be 100644
--- a/store/database.h
+++ b/store/database.h
@@ -1,9 +1,10 @@
1#include <lmdb.h> 1#include <lmdb.h>
2#include <string> 2#include <string>
3#include <QString>
3 4
4class Database { 5class Database {
5public: 6public:
6 Database(); 7 Database(const QString &path);
7 ~Database(); 8 ~Database();
8 MDB_txn *startTransaction(); 9 MDB_txn *startTransaction();
9 void endTransaction(MDB_txn *transaction); 10 void endTransaction(MDB_txn *transaction);
diff --git a/store/test/CMakeLists.txt b/store/test/CMakeLists.txt
new file mode 100644
index 0000000..4743cfb
--- /dev/null
+++ b/store/test/CMakeLists.txt
@@ -0,0 +1,21 @@
1set(CMAKE_AUTOMOC ON)
2include_directories(${CMAKE_CURRENT_BINARY_DIR})
3
4set(store_path "../")
5set(store_SRCS
6 ${store_path}/database.cpp
7)
8
9generate_flatbuffers(calendar)
10
11macro(manual_tests)
12 foreach(_testname ${ARGN})
13 add_executable(${_testname} ${_testname}.cpp ${store_SRCS})
14 qt5_use_modules(${_testname} Core Test)
15 target_link_libraries(${_testname} lmdb)
16 endforeach(_testname)
17endmacro(auto_tests)
18
19manual_tests (
20 storagebenchmark
21)
diff --git a/store/test/calendar.fbs b/store/test/calendar.fbs
new file mode 100644
index 0000000..203ee43
--- /dev/null
+++ b/store/test/calendar.fbs
@@ -0,0 +1,12 @@
1// example IDL file
2
3namespace Calendar;
4
5table Event {
6 summary:string;
7 description:string;
8 attachment:[byte];
9}
10
11root_type Event;
12file_identifier "AKFB";
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
13using namespace Calendar;
14using namespace flatbuffers;
15
16static 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
35static 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
41class StorageBenchmark : public QObject
42{
43 Q_OBJECT
44private:
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
50private 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
131QTEST_MAIN(StorageBenchmark)
132#include "storagebenchmark.moc"