]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-hashmap.c
The sixth batch
[thirdparty/git.git] / t / helper / test-hashmap.c
CommitLineData
7c18cbd5 1#include "test-tool.h"
e8fa59b9 2#include "git-compat-util.h"
6a364ced 3#include "hashmap.h"
7e8089c9 4#include "strbuf.h"
826f0e33 5#include "string-list.h"
6a364ced
KB
6
7struct test_entry
8{
e2b5038d 9 int padding; /* hashmap entry no longer needs to be the first member */
6a364ced
KB
10 struct hashmap_entry ent;
11 /* key and value as two \0-terminated strings */
12 char key[FLEX_ARRAY];
13};
14
6815d114 15static int test_entry_cmp(const void *cmp_data,
939af16e
EW
16 const struct hashmap_entry *eptr,
17 const struct hashmap_entry *entry_or_key,
6815d114 18 const void *keydata)
6a364ced 19{
6815d114 20 const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
939af16e 21 const struct test_entry *e1, *e2;
6815d114
SB
22 const char *key = keydata;
23
939af16e
EW
24 e1 = container_of(eptr, const struct test_entry, ent);
25 e2 = container_of(entry_or_key, const struct test_entry, ent);
26
6815d114
SB
27 if (ignore_case)
28 return strcasecmp(e1->key, key ? key : e2->key);
29 else
30 return strcmp(e1->key, key ? key : e2->key);
6a364ced
KB
31}
32
a6119f82 33static struct test_entry *alloc_test_entry(unsigned int hash,
b567004b
PS
34 const char *key,
35 const char *value)
6a364ced 36{
7daa825d
JK
37 size_t klen = strlen(key);
38 size_t vlen = strlen(value);
b6c4380d 39 struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
d22245a2 40 hashmap_entry_init(&entry->ent, hash);
6a364ced
KB
41 memcpy(entry->key, key, klen + 1);
42 memcpy(entry->key + klen + 1, value, vlen + 1);
43 return entry;
44}
45
46#define HASH_METHOD_FNV 0
47#define HASH_METHOD_I 1
48#define HASH_METHOD_IDIV10 2
49#define HASH_METHOD_0 3
50#define HASH_METHOD_X2 4
51#define TEST_SPARSE 8
52#define TEST_ADD 16
53#define TEST_SIZE 100000
54
55static unsigned int hash(unsigned int method, unsigned int i, const char *key)
56{
3b9a2b07 57 unsigned int hash = 0;
6a364ced
KB
58 switch (method & 3)
59 {
60 case HASH_METHOD_FNV:
61 hash = strhash(key);
62 break;
63 case HASH_METHOD_I:
64 hash = i;
65 break;
66 case HASH_METHOD_IDIV10:
67 hash = i / 10;
68 break;
69 case HASH_METHOD_0:
70 hash = 0;
71 break;
72 }
73
74 if (method & HASH_METHOD_X2)
75 hash = 2 * hash;
76 return hash;
77}
78
79/*
80 * Test performance of hashmap.[ch]
7c18cbd5 81 * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
6a364ced
KB
82 */
83static 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
aef6cf1e
JK
91 ALLOC_ARRAY(entries, TEST_SIZE);
92 ALLOC_ARRAY(hashes, TEST_SIZE);
6a364ced 93 for (i = 0; i < TEST_SIZE; i++) {
cbadf0ee 94 xsnprintf(buf, sizeof(buf), "%i", i);
7daa825d 95 entries[i] = alloc_test_entry(0, buf, "");
6a364ced
KB
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++) {
6815d114 102 hashmap_init(&map, test_entry_cmp, NULL, 0);
6a364ced
KB
103
104 /* add entries */
105 for (i = 0; i < TEST_SIZE; i++) {
d22245a2 106 hashmap_entry_init(&entries[i]->ent, hashes[i]);
b94e5c1d 107 hashmap_add(&map, &entries[i]->ent);
6a364ced
KB
108 }
109
6da1a258 110 hashmap_clear(&map);
6a364ced
KB
111 }
112 } else {
113 /* test map lookups */
6815d114 114 hashmap_init(&map, test_entry_cmp, NULL, 0);
6a364ced
KB
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++) {
d22245a2 119 hashmap_entry_init(&entries[i]->ent, hashes[i]);
b94e5c1d 120 hashmap_add(&map, &entries[i]->ent);
6a364ced
KB
121 }
122
123 for (j = 0; j < rounds; j++) {
124 for (i = 0; i < TEST_SIZE; i++) {
ab73a9d1
KB
125 hashmap_get_from_hash(&map, hashes[i],
126 entries[i]->key);
6a364ced
KB
127 }
128 }
129
6da1a258 130 hashmap_clear(&map);
6a364ced
KB
131 }
132}
133
6a364ced
KB
134#define DELIM " \t\r\n"
135
136/*
137 * Read stdin line by line and print result of commands to stdout:
138 *
6a364ced 139 * perfhashmap method rounds -> test hashmap.[ch] performance
6a364ced 140 */
7046c85c 141int cmd__hashmap(int argc UNUSED, const char **argv UNUSED)
6a364ced 142{
826f0e33 143 struct string_list parts = STRING_LIST_INIT_NODUP;
7e8089c9 144 struct strbuf line = STRBUF_INIT;
6a364ced
KB
145
146 /* process commands from stdin */
7e8089c9 147 while (strbuf_getline(&line, stdin) != EOF) {
826f0e33 148 char *cmd, *p1, *p2;
6a364ced
KB
149
150 /* break line into command and up to two parameters */
826f0e33
TB
151 string_list_setlen(&parts, 0);
152 string_list_split_in_place(&parts, line.buf, DELIM, 2);
153 string_list_remove_empty_items(&parts, 0);
154
6a364ced 155 /* ignore empty lines */
826f0e33
TB
156 if (!parts.nr)
157 continue;
158 if (!*parts.items[0].string || *parts.items[0].string == '#')
6a364ced
KB
159 continue;
160
826f0e33
TB
161 cmd = parts.items[0].string;
162 p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
163 p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
7b64d42d 164
3469a236 165 if (!strcmp("perfhashmap", cmd) && p1 && p2) {
6a364ced
KB
166
167 perf_hashmap(atoi(p1), atoi(p2));
168
6a364ced
KB
169 } else {
170
171 printf("Unknown command %s\n", cmd);
172
173 }
174 }
175
826f0e33 176 string_list_clear(&parts, 0);
7e8089c9 177 strbuf_release(&line);
6a364ced
KB
178 return 0;
179}