2 * Copyright (C) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #include <ccan/avl/avl.h>
25 #define remove remove_
26 #include <ccan/avl/avl.c>
29 #include <ccan/tap/tap.h>
36 #define ANIMATE_RANDOM_ACCESS 0
38 #if ANIMATE_RANDOM_ACCESS
42 typedef int64_t msec_t;
44 static msec_t time_ms(void)
47 gettimeofday(&tv, NULL);
48 return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
53 uint32_t rand32_state = 0;
56 * Finds a pseudorandom 32-bit number from 0 to 2^32-1 .
57 * Uses the BCPL linear congruential generator method.
59 * Note: this method (or maybe just this implementation) seems to
60 * go back and forth between odd and even exactly, which can
61 * affect low-cardinality testing if the cardinality given is even.
63 static uint32_t rand32(void)
65 rand32_state *= (uint32_t)0x7FF8A3ED;
66 rand32_state += (uint32_t)0x2AA01D31;
70 static void scramble(void *base, size_t nmemb, size_t size)
75 for (;nmemb>1;nmemb--) {
76 o = i + size*(rand32()%nmemb);
87 size_t passed_invariants_checks;
94 size_t failed_invariants_checks;
97 static void clear_stats(void)
99 memset(&stats, 0, sizeof(stats));
102 static bool print_stats(const char *success_label, size_t expected_success)
106 printf(" %s: \t%zu\n", success_label, stats.success);
107 if (stats.success != expected_success)
110 if (stats.passed_invariants_checks)
111 printf(" Passed invariants checks: %zu\n", stats.passed_invariants_checks);
114 failed = 1, printf(" Excess: \t%zu\n", stats.excess);
116 failed = 1, printf(" Duplicate: \t%zu\n", stats.duplicate);
118 failed = 1, printf(" Missing: \t%zu\n", stats.missing);
120 failed = 1, printf(" Incorrect: \t%zu\n", stats.incorrect);
122 failed = 1, printf(" Failed: \t%zu\n", stats.failed);
123 if (stats.failed_invariants_checks)
124 failed = 1, printf(" Failed invariants checks: %zu\n", stats.failed_invariants_checks);
132 size_t insert_id; /* needed because qsort is not a stable sort */
135 static int compare_test_item(const void *ap, const void *bp)
137 const struct test_item *a = *(void**)ap, *b = *(void**)bp;
144 if (a->insert_id < b->insert_id)
146 if (a->insert_id > b->insert_id)
152 static bool test_insert_item(AVL *avl, struct test_item *item)
154 avl_insert(avl, &item->key, &item->value);
158 static bool test_lookup_item(const AVL *avl, const struct test_item *item)
160 return avl_member(avl, &item->key) && avl_lookup(avl, &item->key) == &item->value;
163 static bool test_remove_item(AVL *avl, struct test_item *item)
165 return avl_remove(avl, &item->key);
168 static void test_foreach(AVL *avl, struct test_item **test_items, int count)
174 avl_foreach(iter, avl) {
179 if (iter.key == &test_items[i]->key && iter.value == &test_items[i]->value)
187 static void test_foreach_reverse(AVL *avl, struct test_item **test_items, int count)
193 avl_foreach_reverse(iter, avl) {
198 if (iter.key == &test_items[i]->key && iter.value == &test_items[i]->value)
206 static void benchmark(size_t max_per_trial, size_t round_count, bool random_counts, int cardinality)
208 struct test_item **test_item;
209 struct test_item *test_item_array;
213 test_item = malloc(max_per_trial * sizeof(*test_item));
214 test_item_array = malloc(max_per_trial * sizeof(*test_item_array));
216 if (!test_item || !test_item_array) {
217 fail("Not enough memory for %zu keys per trial\n",
222 /* Initialize test_item pointers. */
223 for (i=0; i<max_per_trial; i++)
224 test_item[i] = &test_item_array[i];
227 * If round_count is not zero, run round_count trials.
228 * Otherwise, run forever.
230 for (round = 1; round_count==0 || round <= round_count; round++) {
234 printf("Round %zu (cardinality = %d)\n", round, cardinality);
236 printf("Round %zu\n", round);
239 count = rand32() % (max_per_trial+1);
241 count = max_per_trial;
244 * Initialize test items by giving them sequential keys and
245 * random values. Scramble them so the order of insertion
248 for (i=0; i<count; i++) {
249 test_item[i]->key = rand32();
250 test_item[i]->value = rand32();
253 test_item[i]->key %= cardinality;
255 scramble(test_item, count, sizeof(*test_item));
257 avl = avl_new(order_u32_noctx);
260 printf(" Inserting %zu items...\n", count);
261 for (i=0; i<count; i++) {
262 if (test_insert_item(avl, test_item[i])) {
264 test_item[i]->insert_id = i;
266 printf("invariants check failed at insertion %zu\n", i);
270 /* Periodically do an invariants check */
271 if (count / 10 > 0 && i % (count / 10) == 0) {
272 if (avl_check_invariants(avl))
273 stats.passed_invariants_checks++;
275 stats.failed_invariants_checks++;
278 ok1(print_stats("Inserted", count));
279 ok1(avl_check_invariants(avl));
281 /* remove early duplicates, as they are shadowed in insertions. */
282 qsort(test_item, count, sizeof(*test_item), compare_test_item);
283 for (i = 0, o = 0; i < count;) {
284 uint32_t k = test_item[i]->key;
285 do i++; while (i < count && test_item[i]->key == k);
286 test_item[o++] = test_item[i-1];
289 ok1(avl_count(avl) == count);
291 scramble(test_item, count, sizeof(*test_item));
293 printf(" Finding %zu items...\n", count);
295 for (i=0; i<count; i++) {
296 if (!test_lookup_item(avl, test_item[i]))
301 ok1(print_stats("Retrieved", count));
303 qsort(test_item, count, sizeof(*test_item), compare_test_item);
305 printf(" Traversing forward through %zu items...\n", count);
307 test_foreach(avl, test_item, count);
308 ok1(print_stats("Traversed", count));
310 printf(" Traversing backward through %zu items...\n", count);
312 test_foreach_reverse(avl, test_item, count);
313 ok1(print_stats("Traversed", count));
315 scramble(test_item, count, sizeof(*test_item));
317 printf(" Deleting %zu items...\n", count);
319 for (i=0; i<count; i++) {
320 if (test_remove_item(avl, test_item[i]))
325 /* Periodically do an invariants check */
326 if (count / 10 > 0 && i % (count / 10) == 0) {
327 if (avl_check_invariants(avl))
328 stats.passed_invariants_checks++;
330 stats.failed_invariants_checks++;
333 ok1(print_stats("Deleted", count));
334 ok1(avl_count(avl) == 0);
335 ok1(avl_check_invariants(avl));
341 free(test_item_array);
344 static int compare_ptr(const void *a, const void *b)
358 static bool print_pass_fail(struct fail_total *pf, const char *label)
360 long long fail = pf->fail,
363 printf("%s:\t%lld / %lld\n", label, total - fail, total);
368 static void test_random_access(uint32_t max, int64_t ops)
375 inserts, lookups, removes, checks;
378 #if ANIMATE_RANDOM_ACCESS
379 msec_t last_update, now;
380 msec_t interval = 100;
383 memset(&s, 0, sizeof(s));
385 in_tree = malloc(max);
386 memset(in_tree, 0, max);
388 avl = avl_new(compare_ptr);
390 #if ANIMATE_RANDOM_ACCESS
392 last_update = now - interval;
395 for (i = 0; i < ops; i++) {
396 char *item = &in_tree[rand32() % max];
398 bool inserted, removed;
400 #if ANIMATE_RANDOM_ACCESS
402 if (now >= last_update + interval) {
404 printf("\r%.2f%%\t%zu / %zu\033[K", (double)i * 100 / ops, avl_count(avl), max);
409 switch (rand32() % 3) {
411 inserted = avl_insert(avl, item, item);
413 if ((*item == 0 && !inserted) ||
414 (*item == 1 && inserted))
424 found = avl_lookup(avl, item);
426 if ((*item == 0 && found != NULL) ||
427 (*item == 1 && found != item) ||
428 (avl_member(avl, item) != !!found))
435 removed = avl_remove(avl, item);
437 if ((*item == 0 && removed) ||
438 (*item == 1 && !removed))
448 /* Periodically do an invariants check */
449 if (ops / 10 > 0 && i % (ops / 10) == 0) {
450 if (!avl_check_invariants(avl))
456 #if ANIMATE_RANDOM_ACCESS
463 ok1(print_pass_fail(&s.inserts, "Inserts"));
464 ok1(print_pass_fail(&s.lookups, "Lookups"));
465 ok1(print_pass_fail(&s.removes, "Removes"));
466 ok1(print_pass_fail(&s.checks, "Invariants checks"));
471 plan_tests(18 * 3 + 4);
473 benchmark(100000, 2, false, 0);
474 benchmark(100000, 2, false, 12345);
475 benchmark(100000, 2, false, 100001);
477 printf("Running random access test\n");
478 test_random_access(12345, 1234567);
480 return exit_status();