2 * Copyright (c) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <ccan/avl/avl.h>
19 #define remove remove_
20 #include <ccan/avl/avl.c>
23 #include <ccan/tap/tap.h>
30 #define ANIMATE_RANDOM_ACCESS 0
32 #if ANIMATE_RANDOM_ACCESS
36 typedef int64_t msec_t;
38 static msec_t time_ms(void)
41 gettimeofday(&tv, NULL);
42 return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
47 uint32_t rand32_state = 0;
50 * Finds a pseudorandom 32-bit number from 0 to 2^32-1 .
51 * Uses the BCPL linear congruential generator method.
53 * Note: this method (or maybe just this implementation) seems to
54 * go back and forth between odd and even exactly, which can
55 * affect low-cardinality testing if the cardinality given is even.
57 static uint32_t rand32(void)
59 rand32_state *= (uint32_t)0x7FF8A3ED;
60 rand32_state += (uint32_t)0x2AA01D31;
64 static void scramble(void *base, size_t nmemb, size_t size)
69 for (;nmemb>1;nmemb--) {
70 o = i + size*(rand32()%nmemb);
81 size_t passed_invariants_checks;
88 size_t failed_invariants_checks;
91 static void clear_stats(void)
93 memset(&stats, 0, sizeof(stats));
96 static bool print_stats(const char *success_label, size_t expected_success)
100 printf(" %s: \t%zu\n", success_label, stats.success);
101 if (stats.success != expected_success)
104 if (stats.passed_invariants_checks)
105 printf(" Passed invariants checks: %zu\n", stats.passed_invariants_checks);
108 failed = 1, printf(" Excess: \t%zu\n", stats.excess);
110 failed = 1, printf(" Duplicate: \t%zu\n", stats.duplicate);
112 failed = 1, printf(" Missing: \t%zu\n", stats.missing);
114 failed = 1, printf(" Incorrect: \t%zu\n", stats.incorrect);
116 failed = 1, printf(" Failed: \t%zu\n", stats.failed);
117 if (stats.failed_invariants_checks)
118 failed = 1, printf(" Failed invariants checks: %zu\n", stats.failed_invariants_checks);
126 size_t insert_id; /* needed because qsort is not a stable sort */
129 static int compare_uint32_t(const void *ap, const void *bp)
131 uint32_t a = *(const uint32_t *)ap;
132 uint32_t b = *(const uint32_t *)bp;
141 static int compare_test_item(const void *ap, const void *bp)
143 const struct test_item *a = *(void**)ap, *b = *(void**)bp;
150 if (a->insert_id < b->insert_id)
152 if (a->insert_id > b->insert_id)
158 static bool test_insert_item(AVL *avl, struct test_item *item)
160 avl_insert(avl, &item->key, &item->value);
164 static bool test_lookup_item(const AVL *avl, const struct test_item *item)
166 return avl_member(avl, &item->key) && avl_lookup(avl, &item->key) == &item->value;
169 static bool test_remove_item(AVL *avl, struct test_item *item)
171 return avl_remove(avl, &item->key);
174 static void test_foreach(AVL *avl, struct test_item **test_items, int count)
180 avl_foreach(iter, avl) {
185 if (iter.key == &test_items[i]->key && iter.value == &test_items[i]->value)
193 static void test_foreach_reverse(AVL *avl, struct test_item **test_items, int count)
199 avl_foreach_reverse(iter, avl) {
204 if (iter.key == &test_items[i]->key && iter.value == &test_items[i]->value)
212 static void benchmark(size_t max_per_trial, size_t round_count, bool random_counts, int cardinality)
214 struct test_item **test_item;
215 struct test_item *test_item_array;
219 test_item = malloc(max_per_trial * sizeof(*test_item));
220 test_item_array = malloc(max_per_trial * sizeof(*test_item_array));
222 if (!test_item || !test_item_array) {
223 fail("Not enough memory for %zu keys per trial\n",
228 /* Initialize test_item pointers. */
229 for (i=0; i<max_per_trial; i++)
230 test_item[i] = &test_item_array[i];
233 * If round_count is not zero, run round_count trials.
234 * Otherwise, run forever.
236 for (round = 1; round_count==0 || round <= round_count; round++) {
240 printf("Round %zu (cardinality = %d)\n", round, cardinality);
242 printf("Round %zu\n", round);
245 count = rand32() % (max_per_trial+1);
247 count = max_per_trial;
250 * Initialize test items by giving them sequential keys and
251 * random values. Scramble them so the order of insertion
254 for (i=0; i<count; i++) {
255 test_item[i]->key = rand32();
256 test_item[i]->value = rand32();
259 test_item[i]->key %= cardinality;
261 scramble(test_item, count, sizeof(*test_item));
263 avl = avl_new(compare_uint32_t);
266 printf(" Inserting %zu items...\n", count);
267 for (i=0; i<count; i++) {
268 if (test_insert_item(avl, test_item[i])) {
270 test_item[i]->insert_id = i;
272 printf("invariants check failed at insertion %zu\n", i);
276 /* Periodically do an invariants check */
277 if (count / 10 > 0 && i % (count / 10) == 0) {
278 if (avl_check_invariants(avl))
279 stats.passed_invariants_checks++;
281 stats.failed_invariants_checks++;
284 ok1(print_stats("Inserted", count));
285 ok1(avl_check_invariants(avl));
287 /* remove early duplicates, as they are shadowed in insertions. */
288 qsort(test_item, count, sizeof(*test_item), compare_test_item);
289 for (i = 0, o = 0; i < count;) {
290 uint32_t k = test_item[i]->key;
291 do i++; while (i < count && test_item[i]->key == k);
292 test_item[o++] = test_item[i-1];
295 ok1(avl_count(avl) == count);
297 scramble(test_item, count, sizeof(*test_item));
299 printf(" Finding %zu items...\n", count);
301 for (i=0; i<count; i++) {
302 if (!test_lookup_item(avl, test_item[i]))
307 ok1(print_stats("Retrieved", count));
309 qsort(test_item, count, sizeof(*test_item), compare_test_item);
311 printf(" Traversing forward through %zu items...\n", count);
313 test_foreach(avl, test_item, count);
314 ok1(print_stats("Traversed", count));
316 printf(" Traversing backward through %zu items...\n", count);
318 test_foreach_reverse(avl, test_item, count);
319 ok1(print_stats("Traversed", count));
321 scramble(test_item, count, sizeof(*test_item));
323 printf(" Deleting %zu items...\n", count);
325 for (i=0; i<count; i++) {
326 if (test_remove_item(avl, test_item[i]))
331 /* Periodically do an invariants check */
332 if (count / 10 > 0 && i % (count / 10) == 0) {
333 if (avl_check_invariants(avl))
334 stats.passed_invariants_checks++;
336 stats.failed_invariants_checks++;
339 ok1(print_stats("Deleted", count));
340 ok1(avl_count(avl) == 0);
341 ok1(avl_check_invariants(avl));
347 free(test_item_array);
350 static int compare_ptr(const void *a, const void *b)
364 static bool print_pass_fail(struct fail_total *pf, const char *label)
366 long long fail = pf->fail,
369 printf("%s:\t%lld / %lld\n", label, total - fail, total);
374 static void test_random_access(uint32_t max, int64_t ops)
381 inserts, lookups, removes, checks;
384 #if ANIMATE_RANDOM_ACCESS
385 msec_t last_update, now;
386 msec_t interval = 100;
389 memset(&s, 0, sizeof(s));
391 in_tree = malloc(max);
392 memset(in_tree, 0, max);
394 avl = avl_new(compare_ptr);
396 #if ANIMATE_RANDOM_ACCESS
398 last_update = now - interval;
401 for (i = 0; i < ops; i++) {
402 char *item = &in_tree[rand32() % max];
404 bool inserted, removed;
406 #if ANIMATE_RANDOM_ACCESS
408 if (now >= last_update + interval) {
410 printf("\r%.2f%%\t%zu / %zu\033[K", (double)i * 100 / ops, avl_count(avl), max);
415 switch (rand32() % 3) {
417 inserted = avl_insert(avl, item, item);
419 if ((*item == 0 && !inserted) ||
420 (*item == 1 && inserted))
430 found = avl_lookup(avl, item);
432 if ((*item == 0 && found != NULL) ||
433 (*item == 1 && found != item) ||
434 (avl_member(avl, item) != !!found))
441 removed = avl_remove(avl, item);
443 if ((*item == 0 && removed) ||
444 (*item == 1 && !removed))
454 /* Periodically do an invariants check */
455 if (ops / 10 > 0 && i % (ops / 10) == 0) {
456 if (!avl_check_invariants(avl))
462 #if ANIMATE_RANDOM_ACCESS
469 ok1(print_pass_fail(&s.inserts, "Inserts"));
470 ok1(print_pass_fail(&s.lookups, "Lookups"));
471 ok1(print_pass_fail(&s.removes, "Removes"));
472 ok1(print_pass_fail(&s.checks, "Invariants checks"));
477 plan_tests(18 * 3 + 4);
479 benchmark(100000, 2, false, 0);
480 benchmark(100000, 2, false, 12345);
481 benchmark(100000, 2, false, 100001);
483 printf("Running random access test\n");
484 test_random_access(12345, 1234567);
486 return exit_status();