]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-hashmap.c
7782ae585e6471df087d895629e1e111138e9dce
2 #include "git-compat-util.h"
5 #include "string-list.h"
9 int padding
; /* hashmap entry no longer needs to be the first member */
10 struct hashmap_entry ent
;
11 /* key and value as two \0-terminated strings */
15 static int test_entry_cmp(const void *cmp_data
,
16 const struct hashmap_entry
*eptr
,
17 const struct hashmap_entry
*entry_or_key
,
20 const int ignore_case
= cmp_data
? *((int *)cmp_data
) : 0;
21 const struct test_entry
*e1
, *e2
;
22 const char *key
= keydata
;
24 e1
= container_of(eptr
, const struct test_entry
, ent
);
25 e2
= container_of(entry_or_key
, const struct test_entry
, ent
);
28 return strcasecmp(e1
->key
, key
? key
: e2
->key
);
30 return strcmp(e1
->key
, key
? key
: e2
->key
);
33 static struct test_entry
*alloc_test_entry(unsigned int hash
,
37 size_t klen
= strlen(key
);
38 size_t vlen
= strlen(value
);
39 struct test_entry
*entry
= xmalloc(st_add4(sizeof(*entry
), klen
, vlen
, 2));
40 hashmap_entry_init(&entry
->ent
, hash
);
41 memcpy(entry
->key
, key
, klen
+ 1);
42 memcpy(entry
->key
+ klen
+ 1, value
, vlen
+ 1);
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
53 #define TEST_SIZE 100000
55 static unsigned int hash(unsigned int method
, unsigned int i
, const char *key
)
57 unsigned int hash
= 0;
66 case HASH_METHOD_IDIV10
:
74 if (method
& HASH_METHOD_X2
)
80 * Test performance of hashmap.[ch]
81 * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
83 static void perf_hashmap(unsigned int method
, unsigned int rounds
)
87 struct test_entry
**entries
;
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
);
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);
105 for (i
= 0; i
< TEST_SIZE
; i
++) {
106 hashmap_entry_init(&entries
[i
]->ent
, hashes
[i
]);
107 hashmap_add(&map
, &entries
[i
]->ent
);
113 /* test map lookups */
114 hashmap_init(&map
, test_entry_cmp
, NULL
, 0);
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
);
123 for (j
= 0; j
< rounds
; j
++) {
124 for (i
= 0; i
< TEST_SIZE
; i
++) {
125 hashmap_get_from_hash(&map
, hashes
[i
],
134 #define DELIM " \t\r\n"
137 * Read stdin line by line and print result of commands to stdout:
139 * perfhashmap method rounds -> test hashmap.[ch] performance
141 int cmd__hashmap(int argc UNUSED
, const char **argv UNUSED
)
143 struct string_list parts
= STRING_LIST_INIT_NODUP
;
144 struct strbuf line
= STRBUF_INIT
;
146 /* process commands from stdin */
147 while (strbuf_getline(&line
, stdin
) != EOF
) {
150 /* break line into command and up to two parameters */
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);
155 /* ignore empty lines */
158 if (!*parts
.items
[0].string
|| *parts
.items
[0].string
== '#')
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
;
165 if (!strcmp("perfhashmap", cmd
) && p1
&& p2
) {
167 perf_hashmap(atoi(p1
), atoi(p2
));
171 printf("Unknown command %s\n", cmd
);
176 string_list_clear(&parts
, 0);
177 strbuf_release(&line
);