]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/helper: add a test helper to compute hash speed
authorbrian m. carlson <sandals@crustytoothpaste.net>
Wed, 14 Nov 2018 04:09:34 +0000 (04:09 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Nov 2018 07:54:52 +0000 (16:54 +0900)
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>
Makefile
t/helper/test-hash-speed.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h

index 324967410d2dc80c79434d29da78920999eeb08b..1c43bf9aa8af61f1b87ed42536f942f00a8263cc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -716,6 +716,7 @@ TEST_BUILTINS_OBJS += test-example-decorate.o
 TEST_BUILTINS_OBJS += test-genrandom.o
 TEST_BUILTINS_OBJS += test-hash.o
 TEST_BUILTINS_OBJS += test-hashmap.o
+TEST_BUILTINS_OBJS += test-hash-speed.o
 TEST_BUILTINS_OBJS += test-index-version.o
 TEST_BUILTINS_OBJS += test-json-writer.o
 TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
diff --git a/t/helper/test-hash-speed.c b/t/helper/test-hash-speed.c
new file mode 100644 (file)
index 0000000..432233c
--- /dev/null
@@ -0,0 +1,61 @@
+#include "test-tool.h"
+#include "cache.h"
+
+#define NUM_SECONDS 3
+
+static inline void compute_hash(const struct git_hash_algo *algo, git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len)
+{
+       algo->init_fn(ctx);
+       algo->update_fn(ctx, p, len);
+       algo->final_fn(final, ctx);
+}
+
+int cmd__hash_speed(int ac, const char **av)
+{
+       git_hash_ctx ctx;
+       unsigned char hash[GIT_MAX_RAWSZ];
+       clock_t initial, start, end;
+       unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 };
+       int i;
+       void *p;
+       const struct git_hash_algo *algo = NULL;
+
+       if (ac == 2) {
+               for (i = 1; i < GIT_HASH_NALGOS; i++) {
+                       if (!strcmp(av[1], hash_algos[i].name)) {
+                               algo = &hash_algos[i];
+                               break;
+                       }
+               }
+       }
+       if (!algo)
+               die("usage: test-tool hash-speed algo_name");
+
+       /* Use this as an offset to make overflow less likely. */
+       initial = clock();
+
+       printf("algo: %s\n", algo->name);
+
+       for (i = 0; i < ARRAY_SIZE(bufsizes); i++) {
+               unsigned long j, kb;
+               double kb_per_sec;
+               p = xcalloc(1, bufsizes[i]);
+               start = end = clock() - initial;
+               for (j = 0; ((end - start) / CLOCKS_PER_SEC) < NUM_SECONDS; j++) {
+                       compute_hash(algo, &ctx, hash, p, bufsizes[i]);
+
+                       /*
+                        * Only check elapsed time every 128 iterations to avoid
+                        * dominating the runtime with system calls.
+                        */
+                       if (!(j & 127))
+                               end = clock() - initial;
+               }
+               kb = j * bufsizes[i];
+               kb_per_sec = kb / (1024 * ((double)end - start) / CLOCKS_PER_SEC);
+               printf("size %u: %lu iters; %lu KiB; %0.2f KiB/s\n", bufsizes[i], j, kb, kb_per_sec);
+               free(p);
+       }
+
+       exit(0);
+}
index 6b5836dc1b93b85333f2acb9cf1717a4462e209c..e009c8186d72dc818e91323c6e272e49ad0a9b3c 100644 (file)
@@ -20,6 +20,7 @@ static struct test_cmd cmds[] = {
        { "example-decorate", cmd__example_decorate },
        { "genrandom", cmd__genrandom },
        { "hashmap", cmd__hashmap },
+       { "hash-speed", cmd__hash_speed },
        { "index-version", cmd__index_version },
        { "json-writer", cmd__json_writer },
        { "lazy-init-name-hash", cmd__lazy_init_name_hash },
index 29ac7b0b0d6c167bfedf414a37734640a5e4ca39..19a7e8332ad80831d7cf56af63292c7ac1f99893 100644 (file)
@@ -16,6 +16,7 @@ int cmd__dump_untracked_cache(int argc, const char **argv);
 int cmd__example_decorate(int argc, const char **argv);
 int cmd__genrandom(int argc, const char **argv);
 int cmd__hashmap(int argc, const char **argv);
+int cmd__hash_speed(int argc, const char **argv);
 int cmd__index_version(int argc, const char **argv);
 int cmd__json_writer(int argc, const char **argv);
 int cmd__lazy_init_name_hash(int argc, const char **argv);