quick-test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "print-tree.h"
  8. /* for testing only */
  9. int next_key(int i, int max_key) {
  10. return rand() % max_key;
  11. //return i;
  12. }
  13. int main(int ac, char **av) {
  14. struct key ins;
  15. struct key last = { (u64)-1, 0, 0};
  16. char *buf;
  17. int i;
  18. int num;
  19. int ret;
  20. int run_size = 100000;
  21. int max_key = 100000000;
  22. int tree_size = 0;
  23. struct ctree_path path;
  24. struct ctree_super_block super;
  25. struct ctree_root *root;
  26. radix_tree_init();
  27. root = open_ctree("dbfile", &super);
  28. srand(55);
  29. for (i = 0; i < run_size; i++) {
  30. buf = malloc(64);
  31. num = next_key(i, max_key);
  32. // num = i;
  33. sprintf(buf, "string-%d", num);
  34. if (i % 10000 == 0)
  35. fprintf(stderr, "insert %d:%d\n", num, i);
  36. ins.objectid = num;
  37. ins.offset = 0;
  38. ins.flags = 0;
  39. ret = insert_item(root, &ins, buf, strlen(buf));
  40. if (!ret)
  41. tree_size++;
  42. free(buf);
  43. }
  44. write_ctree_super(root, &super);
  45. close_ctree(root);
  46. root = open_ctree("dbfile", &super);
  47. printf("starting search\n");
  48. srand(55);
  49. for (i = 0; i < run_size; i++) {
  50. num = next_key(i, max_key);
  51. ins.objectid = num;
  52. init_path(&path);
  53. if (i % 10000 == 0)
  54. fprintf(stderr, "search %d:%d\n", num, i);
  55. ret = search_slot(root, &ins, &path, 0);
  56. if (ret) {
  57. print_tree(root, root->node);
  58. printf("unable to find %d\n", num);
  59. exit(1);
  60. }
  61. release_path(root, &path);
  62. }
  63. write_ctree_super(root, &super);
  64. close_ctree(root);
  65. root = open_ctree("dbfile", &super);
  66. printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
  67. node_level(root->node->node.header.flags),
  68. root->node->node.header.nritems,
  69. NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
  70. printf("all searches good, deleting some items\n");
  71. i = 0;
  72. srand(55);
  73. for (i = 0 ; i < run_size/4; i++) {
  74. num = next_key(i, max_key);
  75. ins.objectid = num;
  76. init_path(&path);
  77. ret = search_slot(root, &ins, &path, -1);
  78. if (!ret) {
  79. if (i % 10000 == 0)
  80. fprintf(stderr, "del %d:%d\n", num, i);
  81. ret = del_item(root, &path);
  82. if (ret != 0)
  83. BUG();
  84. tree_size--;
  85. }
  86. release_path(root, &path);
  87. }
  88. write_ctree_super(root, &super);
  89. close_ctree(root);
  90. root = open_ctree("dbfile", &super);
  91. srand(128);
  92. for (i = 0; i < run_size; i++) {
  93. buf = malloc(64);
  94. num = next_key(i, max_key);
  95. sprintf(buf, "string-%d", num);
  96. ins.objectid = num;
  97. if (i % 10000 == 0)
  98. fprintf(stderr, "insert %d:%d\n", num, i);
  99. ret = insert_item(root, &ins, buf, strlen(buf));
  100. if (!ret)
  101. tree_size++;
  102. free(buf);
  103. }
  104. write_ctree_super(root, &super);
  105. close_ctree(root);
  106. root = open_ctree("dbfile", &super);
  107. srand(128);
  108. printf("starting search2\n");
  109. for (i = 0; i < run_size; i++) {
  110. num = next_key(i, max_key);
  111. ins.objectid = num;
  112. init_path(&path);
  113. if (i % 10000 == 0)
  114. fprintf(stderr, "search %d:%d\n", num, i);
  115. ret = search_slot(root, &ins, &path, 0);
  116. if (ret) {
  117. print_tree(root, root->node);
  118. printf("unable to find %d\n", num);
  119. exit(1);
  120. }
  121. release_path(root, &path);
  122. }
  123. printf("starting big long delete run\n");
  124. while(root->node && root->node->node.header.nritems > 0) {
  125. struct leaf *leaf;
  126. int slot;
  127. ins.objectid = (u64)-1;
  128. init_path(&path);
  129. ret = search_slot(root, &ins, &path, -1);
  130. if (ret == 0)
  131. BUG();
  132. leaf = &path.nodes[0]->leaf;
  133. slot = path.slots[0];
  134. if (slot != leaf->header.nritems)
  135. BUG();
  136. while(path.slots[0] > 0) {
  137. path.slots[0] -= 1;
  138. slot = path.slots[0];
  139. leaf = &path.nodes[0]->leaf;
  140. memcpy(&last, &leaf->items[slot].key, sizeof(last));
  141. if (tree_size % 10000 == 0)
  142. printf("big del %d:%d\n", tree_size, i);
  143. ret = del_item(root, &path);
  144. if (ret != 0) {
  145. printf("del_item returned %d\n", ret);
  146. BUG();
  147. }
  148. tree_size--;
  149. }
  150. release_path(root, &path);
  151. }
  152. printf("tree size is now %d\n", tree_size);
  153. printf("map tree\n");
  154. print_tree(root->extent_root, root->extent_root->node);
  155. write_ctree_super(root, &super);
  156. close_ctree(root);
  157. return 0;
  158. }