-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
354 lines (323 loc) · 9.67 KB
/
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "rhamt.hpp"
#include <cassert>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unordered_map>
#include <string>
#include <chrono>
#include <iostream>
#define FAIL(msg) { \
printf("ERROR: %s, %d: %s\n", __FILE__, __LINE__, msg); \
return false; \
} while (0)
#define FT 1
typedef std::chrono::duration<long int, std::ratio<1, 1000000000> > nanos;
struct TimingTest {
std::string name;
nanos (*test)(void);
size_t numops;
};
bool unit_test(bool (*f)(void), std::string name,
bool performance=false, TimingTest *ttest=nullptr) {
if (!performance) {
printf("\033[39;1;4mRunning\033[0m \033[96;1;1m%s\033[0m...\n",
name.c_str());
bool passed = (*f)();
if (!passed) {
printf("\033[96;1;1m%s\033[0m \033[31;1;4mfailed\033[0m\n",
name.c_str());
return false;
}
printf("\033[96;1;1m%s \033[32;1;4mpassed\033[0m\n", name.c_str());
}
else if (performance) {
printf("\033[39;1;4mRunning\033[0m \033[96;1;1m%s\033[0m...\n",
ttest->name.c_str());
nanos dur = ttest->test();
printf("\033[96;1;1m%s \033[32;1;4mfinished in %lu ns / per op\033[0m\n",
ttest->name.c_str(), dur.count() / ttest->numops);
}
return true;
}
bool test_random_sparse()
{
std::unordered_map<int, int> golden;
ReliableHAMT<int, int, FT> rhamt;
int k, v;
for (int i = 0; i < 1000000; ++i) {
k = rand();
v = rand();
golden[k] = v;
rhamt.insert(k, v);
}
// if (rhamt.size() != golden.size()) {
// printf("ERROR: %s %d: size mismatch (%lu != %lu)\n", __FILE__, __LINE__,
// rhamt.size(), golden.size());
// return false;
// }
for (auto it : golden) {
const int *rv = rhamt.read(it.first);
if (nullptr == rv) {
FAIL("unexpected nullptr");
}
if (*rv != it.second) {
FAIL("unexpected values");
}
rhamt.remove(it.first);
}
// if (rhamt.size()) {
// printf("ERROR: %s %d: expected size 0, instead size %lu\n",
// __FILE__, __LINE__, rhamt.size());
// return false;
// }
return true;
}
//
// bool test_small_rhamt()
// {
// ReliableHAMT<int, int, FT, uint8_t> rhamt;
//
// // Fill it up, check that the size matches
// for (int i = 0; i < 256; i++) {
// rhamt.insert(i, i);
// }
// size_t size = rhamt.size();
// if (size != 256) {
// printf("ERROR: %s %d: size is %ld, expected %d\n", __FILE__, __LINE__, size, 256);
// return false;
// }
//
// // Remove a few keys
// for (int i = 0; i < 50; i++) {
// rhamt.remove(i);
// }
// size = rhamt.size();
// if (size != 206) {
// printf("ERROR: %s %d: size is %ld, expected %d\n", __FILE__, __LINE__, size, 206);
// return false;
// }
//
// // Insert keys bigger than the hash size, forcing collisions
// // Nodes 0-49 will contain single keys, while the rest hold two
// for (int i = 256; i < 512; i++) {
// rhamt.insert(i, i);
// }
// size = rhamt.size();
// if (size != 462) {
// printf("ERROR: %s %d: size is %ld, expected %d\n", __FILE__, __LINE__, size, 462);
// return false;
// }
//
// for (int i = 50; i < 512; ++i) {
// int val = *rhamt.read(i);
// if (i != val) {
// printf("ERROR: %s %d: read %d, expected %d\n", __FILE__, __LINE__, val, i);
// return false;
// }
// }
//
// return true;
// }
//
// bool test_overwrite()
// {
// ReliableHAMT<int, int, FT, uint8_t> rhamt;
//
// for (int i = 0; i < 1024; ++i)
// rhamt.insert(i, i);
// for (int i = 0; i < 1024; ++i)
// rhamt.insert(i, i << 10);
//
// for (int i = 0; i < 1024; ++i) {
// const int *rv = rhamt.read(i);
// if (nullptr == rv) {
// FAIL("unexpected nullptr");
// }
// if ((i << 10) != *rv) {
// printf("ERROR: %s %d: Expected (%d, %d) but found (%d, %d)\n",
// __FILE__, __LINE__, i, i << 10, i, *rv);
// return false;
// }
// }
//
// return true;
// }
//
bool test_random_dense()
{
ReliableHAMT<uint8_t, uint8_t, FT, uint32_t> rhamt;
std::unordered_map<uint8_t, uint8_t> golden;
for (int i = 0; i < 100; ++i) {
int k = rand();
int v = rand();
golden[k] = v;
rhamt.insert(k, v);
}
for (auto it : golden) {
const uint8_t * rv = rhamt.read(it.first);
if (nullptr == rv) {
FAIL("unexpected nullptr");
}
if (*rv != it.second) {
printf("ERROR: %s %d: Expected (%d, %d) but found (%d, %d)\n",
__FILE__, __LINE__, it.first, it.second, it.first, *rv);
return false;
}
}
return true;
}
// bool test_string_key()
// {
// ReliableHAMT<std::string, int, FT> hamt;
//
// hamt.insert("Yabadabadoo!", 5132);
// const int * rv = hamt.read("Yabadabadoo!");
// if (nullptr == rv) {
// FAIL("unexpected nullptr");
// }
// if (*rv != 5132) {
// FAIL("mismatch key-value pair");
// }
//
// return true;
// }
//
// bool test_missing_read()
// {
// ReliableHAMT<int, int, FT, uint8_t> hamt;
//
// hamt.insert(0, 0);
// const int * rv = hamt.read(256); // hash collision with key 0
// if (nullptr != rv) {
// FAIL("expected nullptr");
// }
//
// rv = hamt.read(1);
// if (nullptr != rv) {
// FAIL("expected nullptr");
// }
//
// rv = hamt.read(2);
// if (nullptr != rv) {
// FAIL("expected nullptr");
// }
//
// return true;
// }
//
// bool test_missing_remove()
// {
// ReliableHAMT<int, int, FT, uint8_t> hamt;
//
// hamt.insert(0, 0);
// int rv = hamt.remove(512); // remove non-existant key from existing leaf
// if (0 != rv) {
// FAIL("removed non-existant key");
// }
// if (1 != hamt.size()) {
// FAIL("lost stored value")
// }
//
// rv = hamt.remove(1); // remove key from non-existant node
// if (0 != rv) {
// FAIL("removed non-existant key");
// }
// if (1 != hamt.size()) {
// FAIL("lost stored value");
// }
//
// if (0 != *hamt.read(0)) {
// FAIL("incorrect read value");
// }
//
// return true;
// }
nanos test_timing_access_built()
{
// Get Duration of 1,000,000 accesses to an already built Trie
ReliableHAMT<int, int, FT> rhamt;
static constexpr int s = 1000000;
for (int i = 0; i < s; ++i) {
rhamt.insert(s, s);
}
auto lstime = std::chrono::high_resolution_clock::now();
for (volatile int i = 0; i < s; ++i) ;
auto letime = std::chrono::high_resolution_clock::now();
auto ldur = letime - lstime;
// printf("loop duration: %lu nanos / iteration\n", ldur.count() / s);
// Loop duration is ~ 1 ns / iter
auto stime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < s; ++i) {
volatile int k = *rhamt.read(s);
(void)k;
}
auto etime = std::chrono::high_resolution_clock::now();
auto dur = etime - stime;
return dur - ldur;;
}
nanos test_timing_build_trie_random()
{
ReliableHAMT<int, int, FT> rhamt;
static constexpr int s = 1000000;
volatile int k;
auto lstime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < s; ++i) {
k = rand();
// rhamt.insert(s, s);
}
(void)k;
auto letime = std::chrono::high_resolution_clock::now();
auto ldur = letime - lstime;
auto stime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < s; ++i) {
rhamt.insert(rand(), s);
}
auto etime = std::chrono::high_resolution_clock::now();
auto dur = etime - stime;
return dur - ldur;
}
nanos test_timing_build_trie_sequential()
{
ReliableHAMT<int, int, FT> rhamt;
static constexpr int s = 1000000;
volatile int k;
auto lstime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < s; ++i) k = i;
(void)k;
auto letime = std::chrono::high_resolution_clock::now();
auto ldur = letime - lstime;
auto stime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < s; ++i) {
rhamt.insert(s, s);
}
auto etime = std::chrono::high_resolution_clock::now();
auto dur = etime - stime;
return dur - ldur;
}
int main(void)
{
printf("Beginning Testing...\n");
srand(time(NULL));
TimingTest ttest;
// unit_test(test_small_rhamt, "test_small_rhamt");
// unit_test(test_random_sparse, "test_random_sparse");
// unit_test(test_overwrite, "test_overwrite");
// unit_test(test_random_dense, "test_random_dense");
// unit_test(test_string_key, "test_string_key");
// unit_test(test_missing_read, "test_missing_read");
// unit_test(test_missing_remove, "test_missing_remove");
ttest.name = "test_timing_access_to_built_rhamt";
ttest.test = test_timing_access_built;
ttest.numops = 1000000;
unit_test(nullptr, "test_timing_fast_reads", true, &ttest);
ttest.test = test_timing_build_trie_random;
ttest.name = "test_timing_build_trie_random";
unit_test(nullptr, "test_timing_build_trie_random", true, &ttest);
ttest.test = test_timing_build_trie_sequential;
ttest.name = "test_timing_build_trie_sequential";
unit_test(nullptr, "test_timing_build_trie_sequential", true, &ttest);
printf("...Tests Complete\n");
return 0;
}