speed.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* Simple speed test for NTDB */
  2. #include <ccan/err/err.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <sys/time.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. #include "ntdb.h"
  12. /* Nanoseconds per operation */
  13. static size_t normalize(const struct timeval *start,
  14. const struct timeval *stop,
  15. unsigned int num)
  16. {
  17. struct timeval diff;
  18. timersub(stop, start, &diff);
  19. /* Floating point is more accurate here. */
  20. return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
  21. / num * 1000;
  22. }
  23. static size_t file_size(void)
  24. {
  25. struct stat st;
  26. if (stat("/tmp/speed.ntdb", &st) != 0)
  27. return -1;
  28. return st.st_size;
  29. }
  30. static int count_record(struct ntdb_context *ntdb,
  31. NTDB_DATA key, NTDB_DATA data, void *p)
  32. {
  33. int *total = p;
  34. *total += *(int *)data.dptr;
  35. return 0;
  36. }
  37. static void dump_and_clear_stats(struct ntdb_context **ntdb,
  38. int flags,
  39. union ntdb_attribute *attr)
  40. {
  41. union ntdb_attribute stats;
  42. enum NTDB_ERROR ecode;
  43. stats.base.attr = NTDB_ATTRIBUTE_STATS;
  44. stats.stats.size = sizeof(stats.stats);
  45. ecode = ntdb_get_attribute(*ntdb, &stats);
  46. if (ecode != NTDB_SUCCESS)
  47. errx(1, "Getting stats: %s", ntdb_errorstr(ecode));
  48. printf("allocs = %llu\n",
  49. (unsigned long long)stats.stats.allocs);
  50. printf(" alloc_subhash = %llu\n",
  51. (unsigned long long)stats.stats.alloc_subhash);
  52. printf(" alloc_chain = %llu\n",
  53. (unsigned long long)stats.stats.alloc_chain);
  54. printf(" alloc_bucket_exact = %llu\n",
  55. (unsigned long long)stats.stats.alloc_bucket_exact);
  56. printf(" alloc_bucket_max = %llu\n",
  57. (unsigned long long)stats.stats.alloc_bucket_max);
  58. printf(" alloc_leftover = %llu\n",
  59. (unsigned long long)stats.stats.alloc_leftover);
  60. printf(" alloc_coalesce_tried = %llu\n",
  61. (unsigned long long)stats.stats.alloc_coalesce_tried);
  62. printf(" alloc_coalesce_iterate_clash = %llu\n",
  63. (unsigned long long)stats.stats.alloc_coalesce_iterate_clash);
  64. printf(" alloc_coalesce_lockfail = %llu\n",
  65. (unsigned long long)stats.stats.alloc_coalesce_lockfail);
  66. printf(" alloc_coalesce_race = %llu\n",
  67. (unsigned long long)stats.stats.alloc_coalesce_race);
  68. printf(" alloc_coalesce_succeeded = %llu\n",
  69. (unsigned long long)stats.stats.alloc_coalesce_succeeded);
  70. printf(" alloc_coalesce_num_merged = %llu\n",
  71. (unsigned long long)stats.stats.alloc_coalesce_num_merged);
  72. printf("compares = %llu\n",
  73. (unsigned long long)stats.stats.compares);
  74. printf(" compare_wrong_offsetbits = %llu\n",
  75. (unsigned long long)stats.stats.compare_wrong_offsetbits);
  76. printf(" compare_wrong_keylen = %llu\n",
  77. (unsigned long long)stats.stats.compare_wrong_keylen);
  78. printf(" compare_wrong_rechash = %llu\n",
  79. (unsigned long long)stats.stats.compare_wrong_rechash);
  80. printf(" compare_wrong_keycmp = %llu\n",
  81. (unsigned long long)stats.stats.compare_wrong_keycmp);
  82. printf("transactions = %llu\n",
  83. (unsigned long long)stats.stats.transactions);
  84. printf(" transaction_cancel = %llu\n",
  85. (unsigned long long)stats.stats.transaction_cancel);
  86. printf(" transaction_nest = %llu\n",
  87. (unsigned long long)stats.stats.transaction_nest);
  88. printf(" transaction_expand_file = %llu\n",
  89. (unsigned long long)stats.stats.transaction_expand_file);
  90. printf(" transaction_read_direct = %llu\n",
  91. (unsigned long long)stats.stats.transaction_read_direct);
  92. printf(" transaction_read_direct_fail = %llu\n",
  93. (unsigned long long)stats.stats.transaction_read_direct_fail);
  94. printf(" transaction_write_direct = %llu\n",
  95. (unsigned long long)stats.stats.transaction_write_direct);
  96. printf(" transaction_write_direct_fail = %llu\n",
  97. (unsigned long long)stats.stats.transaction_write_direct_fail);
  98. printf("expands = %llu\n",
  99. (unsigned long long)stats.stats.expands);
  100. printf("frees = %llu\n",
  101. (unsigned long long)stats.stats.frees);
  102. printf("locks = %llu\n",
  103. (unsigned long long)stats.stats.locks);
  104. printf(" lock_lowlevel = %llu\n",
  105. (unsigned long long)stats.stats.lock_lowlevel);
  106. printf(" lock_nonblock = %llu\n",
  107. (unsigned long long)stats.stats.lock_nonblock);
  108. printf(" lock_nonblock_fail = %llu\n",
  109. (unsigned long long)stats.stats.lock_nonblock_fail);
  110. /* Now clear. */
  111. ntdb_close(*ntdb);
  112. *ntdb = ntdb_open("/tmp/speed.ntdb", flags, O_RDWR, 0, attr);
  113. }
  114. static void ntdb_log(struct ntdb_context *ntdb,
  115. enum ntdb_log_level level,
  116. enum NTDB_ERROR ecode,
  117. const char *message,
  118. void *data)
  119. {
  120. fprintf(stderr, "ntdb:%s:%s:%s\n",
  121. ntdb_name(ntdb), ntdb_errorstr(ecode), message);
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. unsigned int i, j, num = 1000, stage = 0, stopat = -1;
  126. int flags = NTDB_DEFAULT;
  127. bool transaction = false, summary = false;
  128. NTDB_DATA key, data;
  129. struct ntdb_context *ntdb;
  130. struct timeval start, stop;
  131. union ntdb_attribute seed, log;
  132. bool do_stats = false;
  133. enum NTDB_ERROR ecode;
  134. /* Try to keep benchmarks even. */
  135. seed.base.attr = NTDB_ATTRIBUTE_SEED;
  136. seed.base.next = NULL;
  137. seed.seed.seed = 0;
  138. log.base.attr = NTDB_ATTRIBUTE_LOG;
  139. log.base.next = &seed;
  140. log.log.fn = ntdb_log;
  141. if (argv[1] && strcmp(argv[1], "--internal") == 0) {
  142. flags = NTDB_INTERNAL;
  143. argc--;
  144. argv++;
  145. }
  146. if (argv[1] && strcmp(argv[1], "--transaction") == 0) {
  147. transaction = true;
  148. argc--;
  149. argv++;
  150. }
  151. if (argv[1] && strcmp(argv[1], "--no-sync") == 0) {
  152. flags |= NTDB_NOSYNC;
  153. argc--;
  154. argv++;
  155. }
  156. if (argv[1] && strcmp(argv[1], "--summary") == 0) {
  157. summary = true;
  158. argc--;
  159. argv++;
  160. }
  161. if (argv[1] && strcmp(argv[1], "--stats") == 0) {
  162. do_stats = true;
  163. argc--;
  164. argv++;
  165. }
  166. ntdb = ntdb_open("/tmp/speed.ntdb", flags, O_RDWR|O_CREAT|O_TRUNC,
  167. 0600, &log);
  168. if (!ntdb)
  169. err(1, "Opening /tmp/speed.ntdb");
  170. key.dptr = (void *)&i;
  171. key.dsize = sizeof(i);
  172. data = key;
  173. if (argv[1]) {
  174. num = atoi(argv[1]);
  175. argv++;
  176. argc--;
  177. }
  178. if (argv[1]) {
  179. stopat = atoi(argv[1]);
  180. argv++;
  181. argc--;
  182. }
  183. /* Add 1000 records. */
  184. printf("Adding %u records: ", num); fflush(stdout);
  185. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  186. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  187. gettimeofday(&start, NULL);
  188. for (i = 0; i < num; i++)
  189. if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
  190. errx(1, "Inserting key %u in ntdb: %s",
  191. i, ntdb_errorstr(ecode));
  192. gettimeofday(&stop, NULL);
  193. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  194. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  195. printf(" %zu ns (%zu bytes)\n",
  196. normalize(&start, &stop, num), file_size());
  197. if (ntdb_check(ntdb, NULL, NULL))
  198. errx(1, "ntdb_check failed!");
  199. if (summary) {
  200. char *sumstr = NULL;
  201. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  202. printf("%s\n", sumstr);
  203. free(sumstr);
  204. }
  205. if (do_stats)
  206. dump_and_clear_stats(&ntdb, flags, &log);
  207. if (++stage == stopat)
  208. exit(0);
  209. /* Finding 1000 records. */
  210. printf("Finding %u records: ", num); fflush(stdout);
  211. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  212. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  213. gettimeofday(&start, NULL);
  214. for (i = 0; i < num; i++) {
  215. NTDB_DATA dbuf;
  216. if ((ecode = ntdb_fetch(ntdb, key, &dbuf)) != NTDB_SUCCESS
  217. || *(int *)dbuf.dptr != i) {
  218. errx(1, "Fetching key %u in ntdb gave %u",
  219. i, ecode ? ecode : *(int *)dbuf.dptr);
  220. }
  221. }
  222. gettimeofday(&stop, NULL);
  223. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  224. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  225. printf(" %zu ns (%zu bytes)\n",
  226. normalize(&start, &stop, num), file_size());
  227. if (ntdb_check(ntdb, NULL, NULL))
  228. errx(1, "ntdb_check failed!");
  229. if (summary) {
  230. char *sumstr = NULL;
  231. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  232. printf("%s\n", sumstr);
  233. free(sumstr);
  234. }
  235. if (do_stats)
  236. dump_and_clear_stats(&ntdb, flags, &log);
  237. if (++stage == stopat)
  238. exit(0);
  239. /* Missing 1000 records. */
  240. printf("Missing %u records: ", num); fflush(stdout);
  241. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  242. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  243. gettimeofday(&start, NULL);
  244. for (i = num; i < num*2; i++) {
  245. NTDB_DATA dbuf;
  246. ecode = ntdb_fetch(ntdb, key, &dbuf);
  247. if (ecode != NTDB_ERR_NOEXIST)
  248. errx(1, "Fetching key %u in ntdb gave %s",
  249. i, ntdb_errorstr(ecode));
  250. }
  251. gettimeofday(&stop, NULL);
  252. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  253. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  254. printf(" %zu ns (%zu bytes)\n",
  255. normalize(&start, &stop, num), file_size());
  256. if (ntdb_check(ntdb, NULL, NULL))
  257. errx(1, "ntdb_check failed!");
  258. if (summary) {
  259. char *sumstr = NULL;
  260. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  261. printf("%s\n", sumstr);
  262. free(sumstr);
  263. }
  264. if (do_stats)
  265. dump_and_clear_stats(&ntdb, flags, &log);
  266. if (++stage == stopat)
  267. exit(0);
  268. /* Traverse 1000 records. */
  269. printf("Traversing %u records: ", num); fflush(stdout);
  270. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  271. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  272. i = 0;
  273. gettimeofday(&start, NULL);
  274. if (ntdb_traverse(ntdb, count_record, &i) != num)
  275. errx(1, "Traverse returned wrong number of records");
  276. if (i != (num - 1) * (num / 2))
  277. errx(1, "Traverse tallied to %u", i);
  278. gettimeofday(&stop, NULL);
  279. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  280. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  281. printf(" %zu ns (%zu bytes)\n",
  282. normalize(&start, &stop, num), file_size());
  283. if (ntdb_check(ntdb, NULL, NULL))
  284. errx(1, "ntdb_check failed!");
  285. if (summary) {
  286. char *sumstr = NULL;
  287. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  288. printf("%s\n", sumstr);
  289. free(sumstr);
  290. }
  291. if (do_stats)
  292. dump_and_clear_stats(&ntdb, flags, &log);
  293. if (++stage == stopat)
  294. exit(0);
  295. /* Delete 1000 records (not in order). */
  296. printf("Deleting %u records: ", num); fflush(stdout);
  297. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  298. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  299. gettimeofday(&start, NULL);
  300. for (j = 0; j < num; j++) {
  301. i = (j + 100003) % num;
  302. if ((ecode = ntdb_delete(ntdb, key)) != NTDB_SUCCESS)
  303. errx(1, "Deleting key %u in ntdb: %s",
  304. i, ntdb_errorstr(ecode));
  305. }
  306. gettimeofday(&stop, NULL);
  307. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  308. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  309. printf(" %zu ns (%zu bytes)\n",
  310. normalize(&start, &stop, num), file_size());
  311. if (ntdb_check(ntdb, NULL, NULL))
  312. errx(1, "ntdb_check failed!");
  313. if (summary) {
  314. char *sumstr = NULL;
  315. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  316. printf("%s\n", sumstr);
  317. free(sumstr);
  318. }
  319. if (do_stats)
  320. dump_and_clear_stats(&ntdb, flags, &log);
  321. if (++stage == stopat)
  322. exit(0);
  323. /* Re-add 1000 records (not in order). */
  324. printf("Re-adding %u records: ", num); fflush(stdout);
  325. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  326. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  327. gettimeofday(&start, NULL);
  328. for (j = 0; j < num; j++) {
  329. i = (j + 100003) % num;
  330. if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
  331. errx(1, "Inserting key %u in ntdb: %s",
  332. i, ntdb_errorstr(ecode));
  333. }
  334. gettimeofday(&stop, NULL);
  335. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  336. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  337. printf(" %zu ns (%zu bytes)\n",
  338. normalize(&start, &stop, num), file_size());
  339. if (ntdb_check(ntdb, NULL, NULL))
  340. errx(1, "ntdb_check failed!");
  341. if (summary) {
  342. char *sumstr = NULL;
  343. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  344. printf("%s\n", sumstr);
  345. free(sumstr);
  346. }
  347. if (do_stats)
  348. dump_and_clear_stats(&ntdb, flags, &log);
  349. if (++stage == stopat)
  350. exit(0);
  351. /* Append 1000 records. */
  352. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  353. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  354. printf("Appending %u records: ", num); fflush(stdout);
  355. gettimeofday(&start, NULL);
  356. for (i = 0; i < num; i++)
  357. if ((ecode = ntdb_append(ntdb, key, data)) != NTDB_SUCCESS)
  358. errx(1, "Appending key %u in ntdb: %s",
  359. i, ntdb_errorstr(ecode));
  360. gettimeofday(&stop, NULL);
  361. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  362. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  363. printf(" %zu ns (%zu bytes)\n",
  364. normalize(&start, &stop, num), file_size());
  365. if (ntdb_check(ntdb, NULL, NULL))
  366. errx(1, "ntdb_check failed!");
  367. if (summary) {
  368. char *sumstr = NULL;
  369. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  370. printf("%s\n", sumstr);
  371. free(sumstr);
  372. }
  373. if (++stage == stopat)
  374. exit(0);
  375. /* Churn 1000 records: not in order! */
  376. if (transaction && (ecode = ntdb_transaction_start(ntdb)))
  377. errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
  378. printf("Churning %u records: ", num); fflush(stdout);
  379. gettimeofday(&start, NULL);
  380. for (j = 0; j < num; j++) {
  381. i = (j + 1000019) % num;
  382. if ((ecode = ntdb_delete(ntdb, key)) != NTDB_SUCCESS)
  383. errx(1, "Deleting key %u in ntdb: %s",
  384. i, ntdb_errorstr(ecode));
  385. i += num;
  386. if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
  387. errx(1, "Inserting key %u in ntdb: %s",
  388. i, ntdb_errorstr(ecode));
  389. }
  390. gettimeofday(&stop, NULL);
  391. if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
  392. errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
  393. printf(" %zu ns (%zu bytes)\n",
  394. normalize(&start, &stop, num), file_size());
  395. if (ntdb_check(ntdb, NULL, NULL))
  396. errx(1, "ntdb_check failed!");
  397. if (summary) {
  398. char *sumstr = NULL;
  399. ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
  400. printf("%s\n", sumstr);
  401. free(sumstr);
  402. }
  403. if (do_stats)
  404. dump_and_clear_stats(&ntdb, flags, &log);
  405. if (++stage == stopat)
  406. exit(0);
  407. return 0;
  408. }