quick-test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (i == run_size - 5) {
  44. commit_transaction(root, &super);
  45. }
  46. }
  47. close_ctree(root, &super);
  48. root = open_ctree("dbfile", &super);
  49. printf("starting search\n");
  50. srand(55);
  51. for (i = 0; i < run_size; i++) {
  52. num = next_key(i, max_key);
  53. ins.objectid = num;
  54. init_path(&path);
  55. if (i % 10000 == 0)
  56. fprintf(stderr, "search %d:%d\n", num, i);
  57. ret = search_slot(root, &ins, &path, 0, 0);
  58. if (ret) {
  59. print_tree(root, root->node);
  60. printf("unable to find %d\n", num);
  61. exit(1);
  62. }
  63. release_path(root, &path);
  64. }
  65. close_ctree(root, &super);
  66. root = open_ctree("dbfile", &super);
  67. printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
  68. node_level(root->node->node.header.flags),
  69. root->node->node.header.nritems,
  70. NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
  71. printf("all searches good, deleting some items\n");
  72. i = 0;
  73. srand(55);
  74. for (i = 0 ; i < run_size/4; i++) {
  75. num = next_key(i, max_key);
  76. ins.objectid = num;
  77. init_path(&path);
  78. ret = search_slot(root, &ins, &path, -1, 1);
  79. if (!ret) {
  80. if (i % 10000 == 0)
  81. fprintf(stderr, "del %d:%d\n", num, i);
  82. ret = del_item(root, &path);
  83. if (ret != 0)
  84. BUG();
  85. tree_size--;
  86. }
  87. release_path(root, &path);
  88. }
  89. close_ctree(root, &super);
  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. close_ctree(root, &super);
  105. root = open_ctree("dbfile", &super);
  106. srand(128);
  107. printf("starting search2\n");
  108. for (i = 0; i < run_size; i++) {
  109. num = next_key(i, max_key);
  110. ins.objectid = num;
  111. init_path(&path);
  112. if (i % 10000 == 0)
  113. fprintf(stderr, "search %d:%d\n", num, i);
  114. ret = search_slot(root, &ins, &path, 0, 0);
  115. if (ret) {
  116. print_tree(root, root->node);
  117. printf("unable to find %d\n", num);
  118. exit(1);
  119. }
  120. release_path(root, &path);
  121. }
  122. printf("starting big long delete run\n");
  123. while(root->node && root->node->node.header.nritems > 0) {
  124. struct leaf *leaf;
  125. int slot;
  126. ins.objectid = (u64)-1;
  127. init_path(&path);
  128. ret = search_slot(root, &ins, &path, -1, 1);
  129. if (ret == 0)
  130. BUG();
  131. leaf = &path.nodes[0]->leaf;
  132. slot = path.slots[0];
  133. if (slot != leaf->header.nritems)
  134. BUG();
  135. while(path.slots[0] > 0) {
  136. path.slots[0] -= 1;
  137. slot = path.slots[0];
  138. leaf = &path.nodes[0]->leaf;
  139. memcpy(&last, &leaf->items[slot].key, sizeof(last));
  140. if (tree_size % 10000 == 0)
  141. printf("big del %d:%d\n", tree_size, i);
  142. ret = del_item(root, &path);
  143. if (ret != 0) {
  144. printf("del_item returned %d\n", ret);
  145. BUG();
  146. }
  147. tree_size--;
  148. }
  149. release_path(root, &path);
  150. }
  151. /*
  152. printf("previous tree:\n");
  153. print_tree(root, root->commit_root);
  154. printf("map before commit\n");
  155. print_tree(root->extent_root, root->extent_root->node);
  156. */
  157. commit_transaction(root, &super);
  158. printf("tree size is now %d\n", tree_size);
  159. printf("root %p commit root %p\n", root->node, root->commit_root);
  160. printf("map tree\n");
  161. print_tree(root->extent_root, root->extent_root->node);
  162. close_ctree(root, &super);
  163. return 0;
  164. }