git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: git@vger.kernel.org
Cc: "Derrick Stolee" <stolee@gmail.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Duy Nguyen" <pclouds@gmail.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>
Subject: [PATCH v4 08/12] t/helper: add a test helper to compute hash speed
Date: Thu, 25 Oct 2018 02:40:01 +0000	[thread overview]
Message-ID: <20181025024005.154208-9-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20181025024005.154208-1-sandals@crustytoothpaste.net>

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>
---
 Makefile                   |  1 +
 t/helper/test-hash-speed.c | 61 ++++++++++++++++++++++++++++++++++++++
 t/helper/test-tool.c       |  1 +
 t/helper/test-tool.h       |  1 +
 4 files changed, 64 insertions(+)
 create mode 100644 t/helper/test-hash-speed.c

diff --git a/Makefile b/Makefile
index 81dc9ac819..68169a7abb 100644
--- 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
index 0000000000..432233c7f0
--- /dev/null
+++ b/t/helper/test-hash-speed.c
@@ -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);
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index 6b5836dc1b..e009c8186d 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -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 },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 29ac7b0b0d..19a7e8332a 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -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);

  parent reply	other threads:[~2018-10-25  2:40 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-25  2:39 [PATCH v4 00/12] Base SHA-256 implementation brian m. carlson
2018-10-25  2:39 ` [PATCH v4 01/12] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-10-25  2:39 ` [PATCH v4 02/12] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-10-25  2:39 ` [PATCH v4 03/12] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-10-25  2:39 ` [PATCH v4 04/12] cache: make hashcmp and hasheq work with larger hashes brian m. carlson
2018-10-25  2:39 ` [PATCH v4 05/12] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-10-25  2:39 ` [PATCH v4 06/12] t: make the sha1 test-tool helper generic brian m. carlson
2018-10-25  2:40 ` [PATCH v4 07/12] sha1-file: add a constant for hash block size brian m. carlson
2018-10-25  2:40 ` brian m. carlson [this message]
2018-10-25  2:40 ` [PATCH v4 09/12] commit-graph: convert to using the_hash_algo brian m. carlson
2018-10-25  2:40 ` [PATCH v4 10/12] Add a base implementation of SHA-256 support brian m. carlson
2018-10-25  3:02   ` Carlo Arenas
2018-10-28 15:52     ` brian m. carlson
2018-10-29  0:39       ` Junio C Hamano
2018-10-31 22:55         ` brian m. carlson
2018-11-01  5:29           ` Junio C Hamano
2018-10-27  9:03   ` Christian Couder
2018-10-25  2:40 ` [PATCH v4 11/12] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-10-25  2:40 ` [PATCH v4 12/12] hash: add an SHA-256 implementation using OpenSSL brian m. carlson
2018-11-04 23:44 ` [PATCH v5 00/12] Base SHA-256 implementation brian m. carlson
2018-11-04 23:44   ` [PATCH v5 01/12] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-11-05  7:21     ` Ævar Arnfjörð Bjarmason
2018-11-04 23:44   ` [PATCH v5 02/12] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-11-13 18:42     ` Derrick Stolee
2018-11-13 18:45       ` Duy Nguyen
2018-11-14  1:01         ` brian m. carlson
2018-11-14  0:11       ` Ramsay Jones
2018-11-14  0:42         ` Ramsay Jones
2018-11-14  0:51           ` Jeff King
2018-11-14  2:11         ` brian m. carlson
2018-11-14  3:53           ` Ramsay Jones
2018-11-04 23:44   ` [PATCH v5 03/12] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-11-04 23:44   ` [PATCH v5 04/12] cache: make hashcmp and hasheq work with larger hashes brian m. carlson
2018-11-04 23:44   ` [PATCH v5 05/12] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-11-04 23:44   ` [PATCH v5 06/12] t: make the sha1 test-tool helper generic brian m. carlson
2018-11-04 23:44   ` [PATCH v5 07/12] sha1-file: add a constant for hash block size brian m. carlson
2018-11-04 23:44   ` [PATCH v5 08/12] t/helper: add a test helper to compute hash speed brian m. carlson
2018-11-04 23:44   ` [PATCH v5 09/12] commit-graph: convert to using the_hash_algo brian m. carlson
2018-11-04 23:44   ` [PATCH v5 10/12] Add a base implementation of SHA-256 support brian m. carlson
2018-11-05 11:39     ` Ævar Arnfjörð Bjarmason
2018-11-07  1:30       ` brian m. carlson
2018-11-10 15:52         ` Ævar Arnfjörð Bjarmason
2018-11-04 23:44   ` [PATCH v5 11/12] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-11-04 23:44   ` [PATCH v5 12/12] hash: add an SHA-256 implementation using OpenSSL brian m. carlson
2018-11-05  2:45   ` [PATCH v5 00/12] Base SHA-256 implementation Junio C Hamano
2018-11-14  4:09   ` [PATCH v6 " brian m. carlson
2018-11-14  4:09     ` [PATCH v6 01/12] sha1-file: rename algorithm to "sha1" brian m. carlson
2018-11-14  4:09     ` [PATCH v6 02/12] sha1-file: provide functions to look up hash algorithms brian m. carlson
2018-11-14  4:09     ` [PATCH v6 03/12] hex: introduce functions to print arbitrary hashes brian m. carlson
2018-11-14  4:09     ` [PATCH v6 04/12] cache: make hashcmp and hasheq work with larger hashes brian m. carlson
2018-11-14  4:09     ` [PATCH v6 05/12] t: add basic tests for our SHA-1 implementation brian m. carlson
2018-11-14  4:09     ` [PATCH v6 06/12] t: make the sha1 test-tool helper generic brian m. carlson
2018-11-14  4:09     ` [PATCH v6 07/12] sha1-file: add a constant for hash block size brian m. carlson
2018-11-14  4:09     ` [PATCH v6 08/12] t/helper: add a test helper to compute hash speed brian m. carlson
2018-11-14  4:09     ` [PATCH v6 09/12] commit-graph: convert to using the_hash_algo brian m. carlson
2018-11-14  4:09     ` [PATCH v6 10/12] Add a base implementation of SHA-256 support brian m. carlson
2018-11-14  4:09     ` [PATCH v6 11/12] sha256: add an SHA-256 implementation using libgcrypt brian m. carlson
2018-11-14  4:09     ` [PATCH v6 12/12] hash: add an SHA-256 implementation using OpenSSL brian m. carlson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181025024005.154208-9-sandals@crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=stolee@gmail.com \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).