-
Notifications
You must be signed in to change notification settings - Fork 0
/
delay.cpp
263 lines (225 loc) · 7.53 KB
/
delay.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
#include "kvstore.h"
#include "utils.h"
using namespace std;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
using std::chrono::milliseconds;
using std::chrono::seconds;
KVStore store("../data");
class PutDelayTest {
private:
int max;
random_device rd;
mt19937 mt;
bool verbose;
public:
explicit PutDelayTest(int max_, bool verbose_)
: max(max_), rd(), mt(rd()), verbose(verbose_) {}
vector<int> startTest() {
cout << ">>>>> Put Test <<<<<" << endl;
uniform_int_distribution<uint64_t> dist(1, 1024 * 1024 + 1000 * 1024);
vector<int> time(100);
vector<double> avgTime(100);
vector<double> throughput(100);
vector<int> count(100);
vector<int> keys;
auto totalSize = 0;
unsigned long long totalTime = 0;
for (int i = 0; i < max; i++) {
if (i % 100 == 0) {
cout << i << "...";
cout.flush();
}
uint64_t key = dist(mt);
keys.push_back(key);
totalSize += key;
string value(key, 's');
auto start = high_resolution_clock::now();
store.put(key, value);
auto end = high_resolution_clock::now();
auto ms = duration_cast<milliseconds>(end - start).count();
if (verbose) cout << ms << endl;
if (key < 256 * 1024) {
count[0]++;
time[0] += ms;
} else if (key < 512 * 1024) {
count[1]++;
time[1] += ms;
} else if (key < 1024 * 1024) {
count[2]++;
time[2] += ms;
} else if (key < 1536 * 1024) {
count[3]++;
time[3] += ms;
} else {
count[4]++;
time[4] += ms;
}
totalTime += ms;
}
cout << endl;
for (int i = 0; i < 5; i++) {
avgTime[i] = time[i] / (double)count[i];
throughput[i] = 1 / avgTime[i] * 1000;
}
cout << "Average Delay For Different Size Data: " << endl;
cout << fixed << setprecision(2);
cout << "0 ~ 0.25MB : " << avgTime[0] << "ms "
<< "throughput: " << throughput[0] << "/s" << endl;
cout << "0.25MB ~ 0.5MB : " << avgTime[1] << "ms "
<< "throughput: " << throughput[1] << "/s" << endl;
cout << "0.5MB ~ 1MB : " << avgTime[2] << "ms "
<< "throughput: " << throughput[2] << "/s" << endl;
cout << "1MB ~ 1.5MB : " << avgTime[3] << "ms "
<< "throughput: " << throughput[3] << "/s" << endl;
cout << "1.5MB ~ 2MB : " << avgTime[4] << "ms "
<< "throughput: " << throughput[4] << "/s" << endl;
cout << endl;
cout << "Average Delay: " << totalTime / (double)max << "ms, "
<< "Average Throughput: " << 1 / (totalTime / (double)max) * 1000
<< "/s" << endl;
cout << "Total Size Insert: " << totalSize / 1024 / 1024 << "MB" << endl;
cout << endl;
return keys;
}
};
class GetDelayTest {
public:
static void startTest(vector<int> &keys) {
random_shuffle(keys.begin(), keys.end());
vector<int> time(100);
vector<double> avgTime(100);
vector<double> throughput(100);
vector<int> count(100);
unsigned long long totalTime = 0;
for (auto key : keys) {
auto start = high_resolution_clock::now();
store.get(key);
auto end = high_resolution_clock::now();
auto ms = duration_cast<microseconds>(end - start).count();
if (key < 256 * 1024) {
count[0]++;
time[0] += ms;
} else if (key < 512 * 1024) {
count[1]++;
time[1] += ms;
} else if (key < 1024 * 1024) {
count[2]++;
time[2] += ms;
} else if (key < 1536 * 1024) {
count[3]++;
time[3] += ms;
} else {
count[4]++;
time[4] += ms;
}
totalTime += ms;
}
for (int i = 0; i < 5; i++) {
avgTime[i] = time[i] / (double)count[i];
throughput[i] = 1 / avgTime[i] * 1000 * 1000;
}
cout << ">>>>> Get Test <<<<<" << endl;
cout << "Average Delay And Throughput For Different Size Data: " << endl;
cout << fixed << setprecision(2);
cout << "0 ~ 0.25MB : " << avgTime[0] << "µs "
<< "throughput: " << throughput[0] << "/s" << endl;
cout << "0.25MB ~ 0.5MB : " << avgTime[1] << "µs "
<< "throughput: " << throughput[1] << "/s" << endl;
cout << "0.5MB ~ 1MB : " << avgTime[2] << "µs "
<< "throughput: " << throughput[2] << "/s" << endl;
cout << "1MB ~ 1.5MB : " << avgTime[3] << "µs "
<< "throughput: " << throughput[3] << "/s" << endl;
cout << "1.5MB ~ 2MB : " << avgTime[4] << "µs "
<< "throughput: " << throughput[4] << "/s" << endl;
cout << endl;
cout << "Average Delay: " << totalTime / (double)keys.size() << "µs, "
<< "Average Throughput: "
<< 1 / (totalTime / (double)keys.size()) * 1000 * 1000 << "/s" << endl;
cout << endl;
}
};
class DeleteDelayTest {
public:
static void startTest(vector<int> &keys) {
random_shuffle(keys.begin(), keys.end());
vector<int> time(100);
vector<double> avgTime(100);
vector<double> throughput(100);
vector<int> count(100);
unsigned long long totalTime = 0;
for (auto key : keys) {
auto start = high_resolution_clock::now();
store.del(key);
auto end = high_resolution_clock::now();
auto ms = duration_cast<microseconds>(end - start).count();
if (key < 256 * 1024) {
count[0]++;
time[0] += ms;
} else if (key < 512 * 1024) {
count[1]++;
time[1] += ms;
} else if (key < 1024 * 1024) {
count[2]++;
time[2] += ms;
} else if (key < 1536 * 1024) {
count[3]++;
time[3] += ms;
} else {
count[4]++;
time[4] += ms;
}
totalTime += ms;
}
for (int i = 0; i < 5; i++) {
avgTime[i] = time[i] / (double)count[i];
throughput[i] = 1 / avgTime[i] * 1000 * 1000;
}
cout << ">>>>> Delete Test <<<<<" << endl;
cout << "Average Delay And Throughput For Different Size Data: " << endl;
cout << fixed << setprecision(2);
cout << "0 ~ 0.25MB : " << avgTime[0] << "µs "
<< "throughput: " << throughput[0] << "/s" << endl;
cout << "0.25MB ~ 0.5MB : " << avgTime[1] << "µs "
<< "throughput: " << throughput[1] << "/s" << endl;
cout << "0.5MB ~ 1MB : " << avgTime[2] << "µs "
<< "throughput: " << throughput[2] << "/s" << endl;
cout << "1MB ~ 1.5MB : " << avgTime[3] << "µs "
<< "throughput: " << throughput[3] << "/s" << endl;
cout << "1.5MB ~ 2MB : " << avgTime[4] << "µs "
<< "throughput: " << throughput[4] << "/s" << endl;
cout << endl;
cout << "Average Delay: " << totalTime / (double)keys.size() << "µs, "
<< "Average Throughput: "
<< 1 / (totalTime / (double)keys.size()) * 1000 * 1000 << "/s" << endl;
cout << endl;
}
};
int main() {
string path = "../data";
if (utils::dirExists(path)) {
string cmd = "rm -rf " + path;
system(cmd.c_str());
}
auto start = high_resolution_clock::now();
PutDelayTest putDelayTest(1000, false);
auto keys = putDelayTest.startTest();
GetDelayTest::startTest(keys);
DeleteDelayTest::startTest(keys);
if (utils::dirExists(path)) {
string cmd = "rm -rf " + path;
system(cmd.c_str());
}
auto end = high_resolution_clock::now();
auto totalTime = duration_cast<seconds>(end - start).count();
cout << ">>>>> Summary <<<<<" << endl;
cout << "It takes " << totalTime << "s to complete the test." << endl;
}