Merge branch 'rs/column-use-utf8-strnwidth'
[git] / 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 }