Debian Codegraph @

tests/bench/iterated_hash.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <stdbool.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include <isc/iterated_hash.h>
20 #include <isc/random.h>
21 #include <isc/time.h>
22
23 #include <dns/name.h>
24
25 static void
26 time_it(const int count, const int iterations, const unsigned char *salt,
27 const int saltlen, const unsigned char *in, const int inlen) {
28 uint8_t out[NSEC3_MAX_HASH_LENGTH] = { 0 };
29 isc_time_t start, finish;
30
31 printf("%d iterations, %d salt length, %d input length: ", iterations,
32 saltlen, inlen);
33 fflush(stdout);
34
35 start = isc_time_now_hires();
36
37 int i = 0;
38 while (i++ < count) {
39 isc_iterated_hash(out, 1, iterations, salt, saltlen, in, inlen);
40 }
41
42 finish = isc_time_now_hires();
43
44 uint64_t microseconds = isc_time_microdiff(&finish, &start);
45 printf("%0.2f us per iterated_hash()\n", (double)microseconds / count);
46 fflush(stdout);
47 }
48
49 int
50 main(void) {
51 uint8_t salt[DNS_NAME_MAXWIRE];
52 uint8_t in[DNS_NAME_MAXWIRE];
53 size_t saltlen = sizeof(salt);
54 size_t inlen = sizeof(in);
55
56 isc_random_buf(salt, saltlen);
57 isc_random_buf(in, inlen);
58
59 time_it(10000, 150, salt, saltlen, in, inlen);
60 time_it(10000, 15, salt, saltlen, in, inlen);
61 time_it(10000, 0, salt, saltlen, in, inlen);
62
63 saltlen = 32;
64 inlen = 32;
65
66 time_it(10000, 150, salt, 32, in, inlen);
67 time_it(10000, 15, salt, 32, in, inlen);
68 time_it(10000, 0, salt, saltlen, in, inlen);
69
70 saltlen = 0;
71 inlen = 1;
72
73 time_it(10000, 150, salt, 32, in, inlen);
74 time_it(10000, 15, salt, 32, in, inlen);
75 time_it(10000, 0, salt, saltlen, in, inlen);
76 }