-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_flextree.c
592 lines (528 loc) · 18.5 KB
/
test_flextree.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#include "flextree.h"
#define PATH "/tmp/flextree"
volatile u64 __global_r = 0;
u64 seed = 42;
static void setrand(void)
{
seed = 42;
}
static u64 rand_int(void)
{
seed = (seed + 100000037);
return seed;
// a very fake function!
}
static void print_query_result(const struct flextree_query_result *const rr)
{
generic_printf("count %lu loff %lu len %lu\n", rr->count, rr->loff, rr->len);
for (u64 i=0; i<rr->count; i+=1) {
generic_printf("%ld %ld\n", rr->v[i].poff, rr->v[i].len);
}
}
static u8 query_result_equal(
const struct flextree_query_result *const rr1, const struct flextree_query_result *const rr2)
{
if (rr1 == NULL && rr2 == NULL) return 1;
if (rr1 != rr2 && (rr1 == NULL || rr2 == NULL)) return 0;
debug_assert(rr1);
debug_assert(rr2);
if (rr1->count != rr2->count) return 0;
for (u64 i=0; i<rr1->count; i++) {
if (rr1->v[i].poff != rr2->v[i].poff || rr1->v[i].len != rr2->v[i].len) {
return 0;
}
}
return 1;
}
static void random_insert(struct flextree *const ft, struct brute_force *const bf, const u64 count)
{
u64 ts = 0;
if (ft) {
setrand();
ts = time_nsec();
flextree_insert(ft, 0, 0, 4);
u64 max_loff = 4;
for (u64 i=0; i<count; i++) {
u32 len = (u32)(rand_int() % 1000 + 1);
flextree_insert_wtag(ft, max_loff % 1000, max_loff, len, (u16)(i % 0xffff));
max_loff += len;
}
generic_printf("insert to flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
if (bf) {
setrand();
ts = time_nsec();
brute_force_insert(bf, 0, 0, 4);
u64 max_loff = 4;
for (u64 i=0; i<count; i++) {
u32 len = (u32)(rand_int() % 1000 + 1);
brute_force_insert_wtag(bf, max_loff % 1000, max_loff, len, (u16)(i % 0xffff));
max_loff += len;
}
generic_printf("insert to bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
generic_printf("insert finished\n");
}
static void random_append(struct flextree *const ft, struct brute_force *const bf, const u64 count)
{
u64 ts = 0;
if (ft) {
setrand();
ts = time_nsec();
flextree_insert(ft, 0, 0, 4);
u64 max_loff = 4;
for (u64 i=0; i<count; i++) {
u32 len = (u32)(rand_int() % 1000 + 1);
flextree_insert_wtag(ft, max_loff, max_loff, len, (u16)(i % 0xffff));
max_loff += len;
}
generic_printf("append to flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
if (bf) {
setrand();
ts = time_nsec();
brute_force_insert(bf, 0, 0, 4);
u64 max_loff = 4;
for (u64 i=0; i<count; i++) {
u32 len = (u32)(rand_int() % 1000 + 1);
brute_force_insert_wtag(bf, max_loff, max_loff, len, (u16)(i % 0xffff));
max_loff += len;
}
generic_printf("append to bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
generic_printf("append finished\n");
}
static void random_query(struct flextree *const ft, struct brute_force *const bf, u64 total_size)
{
if (!ft || !bf) return;
u64 ts = 0;
u64 *const seq = malloc(sizeof(u64) * total_size);
for (u64 i=0; i<total_size; i++) {
seq[i] = i;
}
shuffle_u64(seq, total_size);
ts = time_nsec();
for (u64 i=0; i<total_size; i++) {
__global_r += flextree_pquery(ft, seq[i]);
}
generic_printf("random lookup flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
ts = time_nsec();
for (u64 i=0; i<total_size; i++) {
__global_r += brute_force_pquery(bf, seq[i]);
}
generic_printf("random lookup bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
generic_printf("total item searched %ld\n", total_size);
u8 correct = 1;
for (u64 i=0; i<total_size; i++) {
u64 fr = flextree_pquery(ft, seq[i]);
u64 br = brute_force_pquery(bf, seq[i]);
if (fr != br) {
generic_printf("Error encourted on %ld %ld %ld\n", i, fr, br);
correct = 0;
break;
}
}
correct ? generic_printf("\033[0;32m[results correct]\033[0m\n") :
generic_printf("\033[0;31m[results wrong]\033[0m\n");
free(seq);
}
static void sequential_query_r(struct flextree *const ft, struct brute_force *const bf, u64 total_size, u8 vo)
{
if (!ft || !bf) return;
u64 ts = 0;
if (!vo) {
ts = time_nsec();
for (u64 i=0; i<total_size; i++) {
__global_r += flextree_pquery(ft, i);
}
generic_printf("sequential lookup flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
ts = time_nsec();
for (u64 i=0; i<total_size; i++) {
__global_r += brute_force_pquery(bf, i);
}
generic_printf("sequential lookup bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
generic_printf("total item searched %ld\n", total_size);
}
u8 correct = 1;
for (u64 i=total_size; i>=0; i--) {
u64 fr = flextree_pquery(ft, i);
u64 br = brute_force_pquery(bf, i);
if (fr != br) {
generic_printf("Error encourted on %ld %ld %ld\n", i, fr, br);
correct = 0;
break;
}
if (i == 0) {
break;
}
}
correct ? generic_printf("\033[0;32m[results correct]\033[0m\n") :
generic_printf("\033[0;31m[results wrong]\033[0m\n");
}
static void sequential_query(struct flextree *const ft, struct brute_force *const bf, u64 total_size)
{
return sequential_query_r(ft, bf, total_size, 0);
}
static void sequential_query_vo(struct flextree *const ft, struct brute_force *const bf, u64 total_size)
{
return sequential_query_r(ft, bf, total_size, 1);
}
static void random_range_query(
struct flextree *const ft, struct brute_force *const bf, u64 total_size, const u64 count)
{
if (!ft || !bf) return;
u64 ts = 0;
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
generic_free(flextree_query(ft, rand_int() % total_size, rand_int() % 100));
}
generic_printf("range lookup flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
generic_free(brute_force_query(bf, rand_int() % total_size, rand_int() % 100));
}
generic_printf("range lookup bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
u8 correct = 1;
setrand();
for (u64 i=0; i<count; i++) {
u64 loff = rand_int() % total_size;
u64 len = rand_int() % 100;
struct flextree_query_result *fr = flextree_query(ft, loff, len);
struct flextree_query_result *br = brute_force_query(bf, loff, len);
if (!query_result_equal(fr, br)) {
print_query_result(fr);
print_query_result(br);
correct = 0;
generic_free(fr);
generic_free(br);
break;
}
generic_free(fr);
generic_free(br);
}
correct ? generic_printf("\033[0;32m[results correct]\033[0m\n") :
generic_printf("\033[0;31m[results wrong]\033[0m\n");
}
static void random_pdelete(
struct flextree *const ft, struct brute_force *const bf, u64 total_size, const u64 count)
{
u64 ts = 0;
if (ft) {
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
u64 tmp = rand_int() % total_size;
flextree_pdelete(ft, tmp);
}
generic_printf("delete flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
if (bf) {
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
u64 tmp = rand_int() % total_size;
brute_force_pdelete(bf, tmp);
}
generic_printf("delete bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
}
static void random_delete(
struct flextree *const ft, struct brute_force *const bf, u64 total_size, const u64 count)
{
if (!ft || !bf) return;
u64 ts = 0;
u64 osize = total_size;
if (ft) {
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
u64 tmp = rand_int() % total_size;
u64 tmp2 = rand_int() % 10 + 1;
flextree_delete(ft, tmp, tmp2);
total_size -= tmp2;
}
generic_printf("delete flextree %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
total_size = osize;
if (bf) {
setrand();
ts = time_nsec();
for (u64 i=0; i<count; i++) {
u64 tmp = rand_int() % total_size;
u64 tmp2 = rand_int() % 10 + 1;
brute_force_delete(bf, tmp, tmp2);
total_size -= tmp2;
}
generic_printf("delete bruteforce %ld milliseconds elapsed\n", time_diff_nsec(ts)/1000000);
}
}
static void sequential_tag_query(struct flextree *const ft, struct brute_force *const bf, u64 total_size)
{
if (!ft || !bf) return;
u16 ft_tag = 0;
u16 bf_tag = 0;
flextree_set_tag(ft, ft->max_loff-1, 0xffff);
brute_force_set_tag(bf, bf->max_loff-1, 0xffff);
u8 correct = 1;
for (u64 i=0; i<total_size; i++) {
int fr = flextree_get_tag(ft, i, &ft_tag);
int br = brute_force_get_tag(bf, i, &bf_tag);
if (fr != br || ft_tag != bf_tag) {
generic_printf("Error encourted on %ld return %d %d tag %u %u\n", i, fr, br, ft_tag, bf_tag);
correct = 0;
break;
}
}
correct ? generic_printf("\033[0;32m[results correct]\033[0m\n") :
generic_printf("\033[0;31m[results wrong]\033[0m\n");
}
static void test1(const u64 count)
{
generic_printf("---test1 insertion and point lookup %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
debug_assert(ft->max_loff == bf->max_loff);
sequential_query(ft, bf, ft->max_loff);
random_query(ft, bf, ft->max_loff);
brute_force_close(bf);
flextree_close(ft);
}
static void test2(const u64 count)
{
generic_printf("---test2 point deletion and point lookup %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
random_pdelete(ft, bf, ft->max_loff, count);
debug_assert(ft->max_loff == bf->max_loff);
sequential_query(ft, bf, ft->max_loff);
brute_force_close(bf);
flextree_close(ft);
}
static void test3(const u64 count)
{
generic_printf("---test3 random range deletion %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
debug_assert(ft->max_loff == bf->max_loff);
random_delete(ft, bf, ft->max_loff, count);
debug_assert(ft->max_loff == bf->max_loff);
sequential_query(ft, bf, ft->max_loff);
flextree_close(ft);
brute_force_close(bf);
}
static void test4(const u64 count)
{
generic_printf("---test4 range query %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
debug_assert(ft->max_loff == bf->max_loff);
random_delete(ft, bf, ft->max_loff, count);
debug_assert(ft->max_loff == bf->max_loff);
random_range_query(ft, bf, ft->max_loff, count);
flextree_close(ft);
brute_force_close(bf);
}
static void count_leaf_nodes(struct flextree_node *const node, u64 *const c)
{
if (node->is_leaf) {
(*c)++;
}
else {
const struct flextree_internal_entry *ie = &node->internal_entry;
for (u64 i=0; i<node->count+1; i++) {
count_leaf_nodes(ie->children[i].node, c);
}
}
}
static void test5(const u64 count)
{
generic_printf("---test5 linked list %lu---\n", count);
struct flextree *ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
u64 c1 = 0, c2 = 0;
struct flextree_node *node = ft->leaf_head;
while (node) {
// generic_printf("%lu ", node->leaf_entry->id);
node = node->leaf_entry.next;
c1++;
}
count_leaf_nodes(ft->root, &c2);
generic_printf("%ld %ld\n", c1, c2);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
c1 = 0;
node = ft->leaf_head;
while (node) {
node = node->leaf_entry.next;
c1++;
}
generic_printf("%ld %ld\n", c1, c2);
flextree_delete(ft, ft->max_loff / 4, ft->max_loff / 4 * 3);
brute_force_delete(bf, bf->max_loff / 4, bf->max_loff / 4 * 3);
c2 = 0;
count_leaf_nodes(ft->root, &c2);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
sequential_query(ft, bf, ft->max_loff);
debug_assert(ft->max_loff == bf->max_loff);
c1 = 0;
node = ft->leaf_head;
while (node) {
node = node->leaf_entry.next;
c1++;
}
generic_printf("%ld %ld\n", c1, c2);
flextree_delete(ft, 0, ft->max_loff);
brute_force_delete(bf, 0, bf->max_loff);
c2 = 0;
count_leaf_nodes(ft->root, &c2);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
generic_printf("final\n");
flextree_print(ft);
debug_assert(ft->max_loff == bf->max_loff);
c1 = 0;
node = ft->leaf_head;
while (node) {
node = node->leaf_entry.next;
c1++;
}
generic_printf("%ld %ld\n", c1, c2);
sequential_query(ft, bf, ft->max_loff);
flextree_close(ft);
brute_force_close(bf);
}
static void flextree_check(struct flextree *ft)
{
u64 total_len = 0;
struct flextree_pos fp = flextree_pos_get_ll(ft, 0);
while (flextree_pos_valid_ll(&fp)) {
struct flextree_extent *ext = &fp.node->leaf_entry.extents[fp.idx];
total_len += ext->len;
flextree_pos_forward_extent_ll(&fp);
}
printf("ft check max_loff %lu, total_len %lu\n", ft->max_loff, total_len);
u8 correct = 0;
if (ft->max_loff == total_len) {
correct = 1;
}
correct ? generic_printf("\033[0;32m[results correct]\033[0m\n") :
generic_printf("\033[0;31m[results wrong]\033[0m\n");
}
static void test0(const u64 count)
{
generic_printf("---test0 insertion and point lookup %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, NULL, count);
random_delete(ft, NULL, ft->max_loff, count);
sequential_query(ft, NULL, ft->max_loff);
u64 loff = 0;
while (ft->max_loff > 100) {
loff = (loff + 0xabcd12) % (ft->max_loff - 100);
flextree_delete(ft, loff, 100);
}
flextree_delete(ft, 0, ft->max_loff-10);
generic_printf("final 10\n");
generic_printf("slab %lu\n", *(u64 *)((char *)ft->node_slab+72));
flextree_print(ft);
flextree_delete(ft, 0, ft->max_loff);
generic_printf("final\n");
generic_printf("slab %lu\n", *(u64 *)((char *)ft->node_slab+72));
flextree_print(ft);
flextree_close(ft);
}
static void test6(const u64 count)
{
generic_printf("---test6 address hole handling %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
// insert holes
generic_printf("%lu\n", ft->max_loff);
int r = flextree_insert(ft, 1lu<<34, 1lu<<40, 50);
debug_assert(r == 0);
r = brute_force_insert(bf, 1lu<<34, 1lu<<40, 50);
debug_assert(r == 0);
generic_printf("%lu\n", ft->max_loff);
random_range_query(ft, bf, ft->max_loff, count);
brute_force_close(bf);
flextree_close(ft);
(void)r;
}
static void test7(const u64 count)
{
generic_printf("---test7 range deletion %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
debug_assert(ft->max_loff == bf->max_loff);
flextree_delete(ft, ft->max_loff / 4, ft->max_loff / 4 * 3);
brute_force_delete(bf, bf->max_loff / 4, bf->max_loff / 4 * 3);
debug_assert(ft->max_loff == bf->max_loff);
sequential_query(ft, bf, ft->max_loff);
flextree_close(ft);
brute_force_close(bf);
}
static void test8(const u64 count)
{
generic_printf("---test8 tags %lu---\n", count);
struct flextree *ft = flextree_open(NULL, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
struct brute_force *bf = brute_force_open(FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, bf, count);
debug_assert(ft->max_loff == bf->max_loff);
random_delete(ft, bf, ft->max_loff, count);
debug_assert(ft->max_loff == bf->max_loff);
sequential_tag_query(ft, bf, ft->max_loff);
flextree_close(ft);
brute_force_close(bf);
}
static void test9(const u64 count)
{
generic_printf("---test9 large persistent tree %lu---\n", count);
struct flextree *ft = flextree_open(PATH, 128lu<<10);
for (u64 i=0; i<2600000000; i++) {
if (i % 100000000 == 0) {
printf("%lu ", i);
flextree_sync(ft);
fflush(stdout);
}
flextree_insert(ft, i*156, i*156, 156);
}
printf("\n");
fflush(stdout);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
for (u32 i=0; i<10000; i++) {
printf("round %u\n", i);
random_append(ft, NULL, count);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
random_insert(ft, NULL, count);
flextree_close(ft);
ft = flextree_open(PATH, FLEXTREE_MAX_EXTENT_SIZE_LIMIT);
flextree_check(ft);
}
flextree_close(ft);
(void)sequential_query_vo;
}
int main(int argc, char ** argv) {
typeof(test0) *tests [11] = {test0, test1, test2, test3, test4, test5, test6, test7, test8, test9, NULL};
char * cmd = argc > 1 ? argv[1] : "0123456789";
for (u32 i = 0; i < 10; i++) {
if (strchr(cmd, (char)((u8)'0'+i)) && tests[i])
tests[i](50000);
}
return 0;
}