Merge branch 'tb/stash-in-c-unused-param-fix'
[alt-git.git] / t / helper / test-hash-speed.c
blob432233c7f0e54c306e6f0462ce3d4ab75a31fe61
1 #include "test-tool.h"
2 #include "cache.h"
4 #define NUM_SECONDS 3
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)
8 algo->init_fn(ctx);
9 algo->update_fn(ctx, p, len);
10 algo->final_fn(final, ctx);
13 int cmd__hash_speed(int ac, const char **av)
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;
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;
31 if (!algo)
32 die("usage: test-tool hash-speed algo_name");
34 /* Use this as an offset to make overflow less likely. */
35 initial = clock();
37 printf("algo: %s\n", algo->name);
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]);
48 * Only check elapsed time every 128 iterations to avoid
49 * dominating the runtime with system calls.
51 if (!(j & 127))
52 end = clock() - initial;
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);
60 exit(0);