-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocksdb-flatbuffer-test-main.cpp
172 lines (140 loc) · 5.88 KB
/
rocksdb-flatbuffer-test-main.cpp
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
#include <iostream>
#include <fstream>
#include "rocksdb/db.h"
#include "schemas/game_generated.h"
using namespace rocksdb;
using namespace std;
double flatBufferTest(int totalTests, DB *pDb)
{
int testCount = 0;
auto beginTime = std::chrono::system_clock::now();
bool printBufSize = true;
while (++testCount != totalTests)
{
// Create a flatbuffer object
flatbuffers::FlatBufferBuilder builder(1024);
string swordName = "Sword " + to_string(testCount);
auto weapon_1_name = builder.CreateString(swordName);
short weapon_1_dmg = 3;
auto sword = game::CreateWeapon(builder, weapon_1_name, weapon_1_dmg);
string monsterName = "monster " + to_string(testCount);
auto name = builder.CreateString(monsterName);
auto weapons = builder.CreateVector(vector<flatbuffers::Offset<game::Weapon>> {sword});
game::MonsterBuilder monsterBuilder(builder);
monsterBuilder.add_weapons(weapons);
monsterBuilder.add_name(name);
auto orc = monsterBuilder.Finish();
builder.Finish(orc);
// get its representation in byte buffer
uint8_t *buf = builder.GetBufferPointer();
int size = builder.GetSize();
if (printBufSize)
{
cout << "Buffer size: " << size << "\n";
printBufSize = false;
}
// put into rocksdb
const Slice monster(reinterpret_cast<const char *>(buf), size);
Status status = pDb->Put(WriteOptions(), "monster" + to_string(testCount), monster);
assert(status.ok());
// read from rocksdb
string readBuffer;
status = pDb->Get(ReadOptions(), "monster" + to_string(testCount), &readBuffer);
assert(status.ok());
// construct monster from read buffer readBuffer
auto retMonster = game::GetMonster(readBuffer.c_str());
// do sanity check
const char *retMonsterName = retMonster->name()->c_str();
auto retWeapon = retMonster->weapons()->Get(0);
const char *retWeaponName = retWeapon->name()->c_str();
assert(strcmp(monsterName.c_str(), retMonsterName) == 0);
assert(strcmp(swordName.c_str(), retWeaponName) == 0);
}
auto endTime = std::chrono::system_clock::now();
auto diff = chrono::duration_cast<chrono::microseconds>(endTime - beginTime).count();
double testDurationMs = diff / 1000.0;
return testDurationMs;
}
double filesystemTest(int totalTests)
{
int testCount = 0;
auto beginTime = std::chrono::system_clock::now();
ofstream outfile("/tmp/test-" + to_string(totalTests) +".bin", ofstream::binary);
ifstream infile("/tmp/test-" + to_string(totalTests) +".bin", ifstream::binary);
vector<size_t> offsets;
offsets.reserve(totalTests);
bool printBufSize = true;
while (testCount++ != totalTests)
{
// Create a flatbuffer object
flatbuffers::FlatBufferBuilder builder(1024);
string swordName = "Sword " + to_string(testCount);
auto weapon_1_name = builder.CreateString(swordName);
short weapon_1_dmg = 3;
auto sword = game::CreateWeapon(builder, weapon_1_name, weapon_1_dmg);
string monsterName = "monster " + to_string(testCount);
auto name = builder.CreateString(monsterName);
auto weapons = builder.CreateVector(vector<flatbuffers::Offset<game::Weapon>> {sword});
game::MonsterBuilder monsterBuilder(builder);
monsterBuilder.add_weapons(weapons);
monsterBuilder.add_name(name);
auto orc = monsterBuilder.Finish();
builder.Finish(orc);
// get its representation in byte buffer
uint8_t *buf = builder.GetBufferPointer();
int size = builder.GetSize();
if (printBufSize)
{
cout << "Buffer size: " << size << "\n";
printBufSize = false;
}
// append into file
offsets[testCount] = size;
outfile.write(reinterpret_cast<const char *>(buf), size);
outfile.flush();
assert(outfile.good());
// read from file
infile.sync();
infile.seekg(0,infile.end); // seek to file end
long end = infile.tellg();
infile.seekg(end - size); // pointer is then moved back `size` characters
char readBuffer[size];
memset(readBuffer, 0, size*sizeof(char));
infile.read(readBuffer, size); // read into readBuffer
assert(!infile.eof()); // we should have read to the end of the file but not past it
infile.clear();
// construct monster from read buffer readBuffer
auto retMonster = game::GetMonster(readBuffer);
// do sanity check
const char *retMonsterName = retMonster->name()->c_str();
auto retWeapon = retMonster->weapons()->Get(0);
const char *retWeaponName = retWeapon->name()->c_str();
assert(strcmp(monsterName.c_str(), retMonsterName) == 0);
assert(strcmp(swordName.c_str(), retWeaponName) == 0);
}
auto endTime = std::chrono::system_clock::now();
auto diff = chrono::duration_cast<chrono::microseconds>(endTime - beginTime).count();
double testDurationMs = diff / 1000.0;
outfile.close();
return testDurationMs;
}
int main(int argc, char *argv[])
{
DB *db;
int totalTests = 1'000'000;
if (argc == 2)
{
totalTests = stoi(argv[1]);
}
cout << "Total read-writes: " << totalTests << "\n";
Options options;
options.create_if_missing = true;
Status status = DB::Open(options,"/tmp/test-" + to_string(totalTests) +".db", &db);
assert(status.ok());
double testFlatBufferDurationMs = flatBufferTest(totalTests, db);
cout << "RocksDb put/get " << totalTests << "x: " << testFlatBufferDurationMs << " ms\n";
double testFilesystemDurationMs = filesystemTest(totalTests);
cout << "Filesystem put/get " << totalTests << "x: " << testFilesystemDurationMs << " ms\n";
delete db;
return 0;
}