* Test performance of hashmap.[ch] * Usage: time echo "perfhashmap method rounds" | test-tool hashmap */
| 81 | * Usage: time echo "perfhashmap method rounds" | test-tool hashmap |
| 82 | */ |
| 83 | static void perf_hashmap(unsigned int method, unsigned int rounds) |
| 84 | { |
| 85 | struct hashmap map; |
| 86 | char buf[16]; |
| 87 | struct test_entry **entries; |
| 88 | unsigned int *hashes; |
| 89 | unsigned int i, j; |
| 90 | |
| 91 | ALLOC_ARRAY(entries, TEST_SIZE); |
| 92 | ALLOC_ARRAY(hashes, TEST_SIZE); |
| 93 | for (i = 0; i < TEST_SIZE; i++) { |
| 94 | xsnprintf(buf, sizeof(buf), "%i", i); |
| 95 | entries[i] = alloc_test_entry(0, buf, ""); |
| 96 | hashes[i] = hash(method, i, entries[i]->key); |
| 97 | } |
| 98 | |
| 99 | if (method & TEST_ADD) { |
| 100 | /* test adding to the map */ |
| 101 | for (j = 0; j < rounds; j++) { |
| 102 | hashmap_init(&map, test_entry_cmp, NULL, 0); |
| 103 | |
| 104 | /* add entries */ |
| 105 | for (i = 0; i < TEST_SIZE; i++) { |
| 106 | hashmap_entry_init(&entries[i]->ent, hashes[i]); |
| 107 | hashmap_add(&map, &entries[i]->ent); |
| 108 | } |
| 109 | |
| 110 | hashmap_clear(&map); |
| 111 | } |
| 112 | } else { |
| 113 | /* test map lookups */ |
| 114 | hashmap_init(&map, test_entry_cmp, NULL, 0); |
| 115 | |
| 116 | /* fill the map (sparsely if specified) */ |
| 117 | j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE; |
| 118 | for (i = 0; i < j; i++) { |
| 119 | hashmap_entry_init(&entries[i]->ent, hashes[i]); |
| 120 | hashmap_add(&map, &entries[i]->ent); |
| 121 | } |
| 122 | |
| 123 | for (j = 0; j < rounds; j++) { |
| 124 | for (i = 0; i < TEST_SIZE; i++) { |
| 125 | hashmap_get_from_hash(&map, hashes[i], |
| 126 | entries[i]->key); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | hashmap_clear(&map); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #define DELIM " \t\r\n" |
| 135 |
no test coverage detected