Debian Codegraph @

tests/bench/siphash.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/ascii.h>
20 #include <isc/random.h>
21 #include <isc/siphash.h>
22 #include <isc/time.h>
23
24 #define SIZE (1024 * 1024)
25
26 #define KILOHASHES(count, us) ((us) == 0 ? 0.0 : ((count) * 1000.0 / (us)))
27
28 int
29 main(void) {
30 static uint8_t bytes[SIZE];
31 static uint8_t key[16];
32
33 isc_random_buf(bytes, SIZE);
34 isc_random_buf(key, sizeof(key));
35
36 for (size_t len = 256; len > 0; len = len * 4 / 5) {
37 isc_time_t start, finish;
38 uint64_t count = 0;
39 uint64_t sum = 0;
40 uint64_t us;
41
42 start = isc_time_now_hires();
43
44 for (size_t end = len; end < SIZE; end += len) {
45 uint64_t hash;
46 uint8_t lower[1024];
47 isc_ascii_lowercopy(lower, bytes + end - len, len);
48 isc_siphash24(key, lower, len, true, (void *)&hash);
49 sum += hash;
50 count++;
51 }
52
53 finish = isc_time_now_hires();
54
55 us = isc_time_microdiff(&finish, &start);
56 printf("%f us wide-lower len %3zu, %7.0f kh/s (%llx)\n",
57 (double)us / 1000000.0, len, KILOHASHES(count, us),
58 (unsigned long long)sum);
59 }
60
61 for (size_t len = 256; len > 0; len = len * 4 / 5) {
62 isc_time_t start, finish;
63 uint64_t count = 0;
64 uint64_t sum = 0;
65 uint64_t us;
66
67 start = isc_time_now_hires();
68
69 for (size_t end = len; end < SIZE; end += len) {
70 uint64_t hash;
71 isc_siphash24(key, bytes + end - len, len, false,
72 (void *)&hash);
73 sum += hash;
74 count++;
75 }
76
77 finish = isc_time_now_hires();
78
79 us = isc_time_microdiff(&finish, &start);
80 printf("%f us wide-icase len %3zu, %7.0f kh/s (%llx)\n",
81 (double)us / 1000000.0, len, KILOHASHES(count, us),
82 (unsigned long long)sum);
83 }
84 for (size_t len = 256; len > 0; len = len * 4 / 5) {
85 isc_time_t start, finish;
86 uint64_t count = 0;
87 uint64_t sum = 0;
88 uint64_t us;
89
90 start = isc_time_now_hires();
91
92 for (size_t end = len; end < SIZE; end += len) {
93 uint64_t hash;
94 isc_siphash24(key, bytes + end - len, len, true,
95 (void *)&hash);
96 sum += hash;
97 count++;
98 }
99
100 finish = isc_time_now_hires();
101
102 us = isc_time_microdiff(&finish, &start);
103 printf("%f us wide-bytes len %3zu, %7.0f kh/s (%llx)\n",
104 (double)us / 1000000.0, len, KILOHASHES(count, us),
105 (unsigned long long)sum);
106 }
107
108 for (size_t len = 256; len > 0; len = len * 4 / 5) {
109 isc_time_t start, finish;
110 uint64_t count = 0;
111 uint64_t sum = 0;
112 uint64_t us;
113
114 start = isc_time_now_hires();
115
116 for (size_t end = len; end < SIZE; end += len) {
117 uint32_t hash;
118 uint8_t lower[1024];
119 isc_ascii_lowercopy(lower, bytes + end - len, len);
120 isc_halfsiphash24(key, lower, len, true, (void *)&hash);
121 sum += hash;
122 count++;
123 }
124
125 finish = isc_time_now_hires();
126
127 us = isc_time_microdiff(&finish, &start);
128 printf("%f us half-lower len %3zu, %7.0f kh/s (%llx)\n",
129 (double)us / 1000000.0, len, KILOHASHES(count, us),
130 (unsigned long long)sum);
131 }
132
133 for (size_t len = 256; len > 0; len = len * 4 / 5) {
134 isc_time_t start, finish;
135 uint64_t count = 0;
136 uint64_t sum = 0;
137 uint64_t us;
138
139 start = isc_time_now_hires();
140
141 for (size_t end = len; end < SIZE; end += len) {
142 uint32_t hash;
143 isc_halfsiphash24(key, bytes + end - len, len, false,
144 (void *)&hash);
145 sum += hash;
146 count++;
147 }
148
149 finish = isc_time_now_hires();
150
151 us = isc_time_microdiff(&finish, &start);
152 printf("%f us half-icase len %3zu, %7.0f kh/s (%llx)\n",
153 (double)us / 1000000.0, len, KILOHASHES(count, us),
154 (unsigned long long)sum);
155 }
156
157 for (size_t len = 256; len > 0; len = len * 4 / 5) {
158 isc_time_t start, finish;
159 uint64_t count = 0;
160 uint64_t sum = 0;
161 uint64_t us;
162
163 start = isc_time_now_hires();
164
165 for (size_t end = len; end < SIZE; end += len) {
166 uint32_t hash;
167 isc_halfsiphash24(key, bytes + end - len, len, true,
168 (void *)&hash);
169 sum += hash;
170 count++;
171 }
172
173 finish = isc_time_now_hires();
174
175 us = isc_time_microdiff(&finish, &start);
176 printf("%f us half-bytes len %3zu, %7.0f kh/s (%llx)\n",
177 (double)us / 1000000.0, len, KILOHASHES(count, us),
178 (unsigned long long)sum);
179 }
180 }