Git fork

t/helper: add a test helper to compute hash speed

Add a utility (which is less for the testsuite and more for developers)
that can compute hash speeds for whatever hash algorithms are
implemented. This allows developers to test their personal systems to
determine the performance characteristics of various algorithms.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

brian m. carlson and committed by
Junio C Hamano
37649b7f a2ce0a75

+64
+1
Makefile
··· 716 716 TEST_BUILTINS_OBJS += test-genrandom.o 717 717 TEST_BUILTINS_OBJS += test-hash.o 718 718 TEST_BUILTINS_OBJS += test-hashmap.o 719 + TEST_BUILTINS_OBJS += test-hash-speed.o 719 720 TEST_BUILTINS_OBJS += test-index-version.o 720 721 TEST_BUILTINS_OBJS += test-json-writer.o 721 722 TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
+61
t/helper/test-hash-speed.c
··· 1 + #include "test-tool.h" 2 + #include "cache.h" 3 + 4 + #define NUM_SECONDS 3 5 + 6 + static inline void compute_hash(const struct git_hash_algo *algo, git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len) 7 + { 8 + algo->init_fn(ctx); 9 + algo->update_fn(ctx, p, len); 10 + algo->final_fn(final, ctx); 11 + } 12 + 13 + int cmd__hash_speed(int ac, const char **av) 14 + { 15 + git_hash_ctx ctx; 16 + unsigned char hash[GIT_MAX_RAWSZ]; 17 + clock_t initial, start, end; 18 + unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 }; 19 + int i; 20 + void *p; 21 + const struct git_hash_algo *algo = NULL; 22 + 23 + if (ac == 2) { 24 + for (i = 1; i < GIT_HASH_NALGOS; i++) { 25 + if (!strcmp(av[1], hash_algos[i].name)) { 26 + algo = &hash_algos[i]; 27 + break; 28 + } 29 + } 30 + } 31 + if (!algo) 32 + die("usage: test-tool hash-speed algo_name"); 33 + 34 + /* Use this as an offset to make overflow less likely. */ 35 + initial = clock(); 36 + 37 + printf("algo: %s\n", algo->name); 38 + 39 + for (i = 0; i < ARRAY_SIZE(bufsizes); i++) { 40 + unsigned long j, kb; 41 + double kb_per_sec; 42 + p = xcalloc(1, bufsizes[i]); 43 + start = end = clock() - initial; 44 + for (j = 0; ((end - start) / CLOCKS_PER_SEC) < NUM_SECONDS; j++) { 45 + compute_hash(algo, &ctx, hash, p, bufsizes[i]); 46 + 47 + /* 48 + * Only check elapsed time every 128 iterations to avoid 49 + * dominating the runtime with system calls. 50 + */ 51 + if (!(j & 127)) 52 + end = clock() - initial; 53 + } 54 + kb = j * bufsizes[i]; 55 + kb_per_sec = kb / (1024 * ((double)end - start) / CLOCKS_PER_SEC); 56 + printf("size %u: %lu iters; %lu KiB; %0.2f KiB/s\n", bufsizes[i], j, kb, kb_per_sec); 57 + free(p); 58 + } 59 + 60 + exit(0); 61 + }
+1
t/helper/test-tool.c
··· 20 20 { "example-decorate", cmd__example_decorate }, 21 21 { "genrandom", cmd__genrandom }, 22 22 { "hashmap", cmd__hashmap }, 23 + { "hash-speed", cmd__hash_speed }, 23 24 { "index-version", cmd__index_version }, 24 25 { "json-writer", cmd__json_writer }, 25 26 { "lazy-init-name-hash", cmd__lazy_init_name_hash },
+1
t/helper/test-tool.h
··· 16 16 int cmd__example_decorate(int argc, const char **argv); 17 17 int cmd__genrandom(int argc, const char **argv); 18 18 int cmd__hashmap(int argc, const char **argv); 19 + int cmd__hash_speed(int argc, const char **argv); 19 20 int cmd__index_version(int argc, const char **argv); 20 21 int cmd__json_writer(int argc, const char **argv); 21 22 int cmd__lazy_init_name_hash(int argc, const char **argv);