-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbbench_test.cpp
161 lines (138 loc) · 4.59 KB
/
dbbench_test.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
#define BOOST_TEST_MODULE dbbench
#include <boost/test/included/unit_test.hpp>
#include <string>
#include <filesystem>
#include <chrono>
#include <random>
#include "database.h"
#include "./test_helpers.h"
static const int nr = 1000000;
static const int vSize = 100;
static const int kSize = 16;
static const int batchSize = 1000;
static const std::string dbname = "testdb/mydb";
static uint8_t value[vSize];
static void _testWrite(bool sync,bool remove) {
if(remove) {
Database::remove(dbname);
}
ByteBuffer val(vSize);
memcpy(val,value,vSize);
int n = nr;
if(sync) n = n / 1000;
Options options(true);
options.enableSyncWrite=sync;
auto db = Database::open(dbname,options);
auto start = std::chrono::system_clock::now();
auto format = "%0"+std::to_string(kSize)+"d";
for(int i=0;i<n;i++) {
char tmp[kSize+1];
snprintf(tmp,kSize+1,format.c_str(),i);
db->put(tmp,val);
}
auto end = std::chrono::system_clock::now();
std::string mode = std::string(sync ? "sync" : "no-sync") + (remove ? "" : " overwrite");
auto ms = millis(end,start);
auto usecPerOp = (ms*1000)/(double)n;
std::cout << "write " << mode << " time " << ms << " records " << n << " usec per ops " << usecPerOp << "\n";
start = std::chrono::system_clock::now();
db->closeWithMerge(0);
end = std::chrono::system_clock::now();
std::cout << "close time " << millis(end,start) << "\n";
std::cout << "database size " << dbsize(dbname) << "\n";
}
static void _testBatch() {
ByteBuffer val(vSize);
memcpy(val,value,vSize);
Database::remove(dbname);
Options options(true);
options.maxSegments=64;
options.disableAutoMerge=true;
auto db = Database::open(dbname,options);
auto start = std::chrono::system_clock::now();
auto format = ("%0"+std::to_string(kSize)+"d");
for(int i=0;i<nr;) {
WriteBatch wb;
for(int j=0;j<batchSize;j++) {
char tmp[kSize+1];
snprintf(tmp,kSize+1,format.c_str(),i+j);
wb.put(tmp,val);
}
db->write(wb);
i+=batchSize;
}
auto end = std::chrono::system_clock::now();
auto ms = millis(end,start);
auto usecPerOp = (ms*1000)/(double)nr;
std::cout << "batch insert time " << nr << " records = " << ms << " ms, usec per ops " << usecPerOp << "\n";
start = std::chrono::system_clock::now();
db->close();
end = std::chrono::system_clock::now();
ms = millis(end,start);
std::cout << "close time " << ms << " ms\n";
std::cout << "database size " << dbsize(dbname) << "\n";
}
static void _testCompact() {
Options options(true);
options.maxSegments=64;
auto db = Database::open(dbname,options);
auto start = std::chrono::system_clock::now();
db->closeWithMerge(1);
auto end = std::chrono::system_clock::now();
auto ms = millis(end,start);
std::cout << "compact time " << ms << " ms\n";
std::cout << "database size " << dbsize(dbname) << "\n";
}
static void _testReadRandom(DatabaseRef &db) {
std::vector<int> keys(nr);
for(int i=0;i<nr;i++) {
keys.push_back(i);
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(keys.begin(),keys.end(),g);
auto start = std::chrono::system_clock::now();
ByteBuffer value;
auto format = ("%0"+std::to_string(kSize)+"d");
for(int index : keys) {
char tmp[kSize+1];
snprintf(tmp,kSize+1,format.c_str(),index);
auto val = db->get(tmp,value);
}
auto end = std::chrono::system_clock::now();
auto ms = millis(end,start);
std::cout << "read random time " << (ms*1000)/(double)(nr) << " us per get\n";
}
static void _testReadSequential(DatabaseRef &db) {
auto start = std::chrono::system_clock::now();
auto itr = db->lookup("","");
int count = 0;
KeyValue kv;
while(!(itr->next(kv).key.empty())) count++;
if(count!=nr) {
std::cout << "count is " << count << "\n";
throw IllegalState("incorrect count");
}
auto end = std::chrono::system_clock::now();
auto ms = millis(end,start);
std::cout << "read seq time " << ms << " ms, us per op " << (ms*1000)/(double)nr << "\n";
}
static void _testRead() {
auto db = Database::open(dbname,Options());
_testReadRandom(db);
_testReadRandom(db);
_testReadSequential(db);
db->close();
}
BOOST_AUTO_TEST_CASE( dbbench ) {
for(int i=0;i<vSize;i++) {
value[i] = (uint8_t)rand();
}
_testWrite(false,true);
_testWrite(true,true);
_testBatch();
_testWrite(false,false);
_testRead();
_testCompact();
_testRead();
}