-
Notifications
You must be signed in to change notification settings - Fork 787
/
main.c
220 lines (193 loc) · 7.26 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Main program to benchmark hash functions
* Part of the xxHash project
* Copyright (C) 2019-2021 Yann Collet
* GPL v2 License
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* You can contact the author at:
* - xxHash homepage: https://www.xxhash.com
* - xxHash source repository: https://github.com/Cyan4973/xxHash
*/
/* === dependencies === */
#include <stdio.h> /* printf */
#include <limits.h> /* INT_MAX */
#include "bhDisplay.h" /* bench_x */
/* === defines list of hashes `hashCandidates` and NB_HASHES *** */
#include "hashes.h"
/* === parse command line === */
#undef NDEBUG
#include <assert.h>
/*!
* readIntFromChar():
* Allows and interprets K, KB, KiB, M, MB and MiB suffix.
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
*/
static int readIntFromChar(const char** stringPtr)
{
static int const max = (INT_MAX / 10) - 1;
int result = 0;
while ((**stringPtr >='0') && (**stringPtr <='9')) {
assert(result < max);
result *= 10;
result += (unsigned)(**stringPtr - '0');
(*stringPtr)++ ;
}
if ((**stringPtr=='K') || (**stringPtr=='M')) {
int const maxK = INT_MAX >> 10;
assert(result < maxK);
result <<= 10;
if (**stringPtr=='M') {
assert(result < maxK);
result <<= 10;
}
(*stringPtr)++; /* skip `K` or `M` */
if (**stringPtr=='i') (*stringPtr)++;
if (**stringPtr=='B') (*stringPtr)++;
}
return result;
}
/**
* isCommand():
* Checks if string is the same as longCommand.
* If yes, @return 1, otherwise @return 0
*/
static int isCommand(const char* string, const char* longCommand)
{
assert(string);
assert(longCommand);
size_t const comSize = strlen(longCommand);
return !strncmp(string, longCommand, comSize);
}
/*
* longCommandWArg():
* Checks if *stringPtr is the same as longCommand.
* If yes, @return 1 and advances *stringPtr to the position which immediately
* follows longCommand.
* @return 0 and doesn't modify *stringPtr otherwise.
*/
static int longCommandWArg(const char** stringPtr, const char* longCommand)
{
assert(stringPtr);
assert(longCommand);
size_t const comSize = strlen(longCommand);
int const result = isCommand(*stringPtr, longCommand);
if (result) *stringPtr += comSize;
return result;
}
/* === default values - can be redefined at compilation time === */
#ifndef SMALL_SIZE_MIN_DEFAULT
# define SMALL_SIZE_MIN_DEFAULT 1
#endif
#ifndef SMALL_SIZE_MAX_DEFAULT
# define SMALL_SIZE_MAX_DEFAULT 127
#endif
#ifndef LARGE_SIZELOG_MIN_DEFAULT
# define LARGE_SIZELOG_MIN_DEFAULT 9
#endif
#ifndef LARGE_SIZELOG_MAX_DEFAULT
# define LARGE_SIZELOG_MAX_DEFAULT 27
#endif
static int display_hash_names(void)
{
int i;
printf("available hashes : \n");
for (i=0; i<NB_HASHES; i++) {
printf("%s, ", hashCandidates[i].name);
}
printf("\b\b \n");
return 0;
}
/*
* @return: hashID (necessarily between 0 and NB_HASHES) if present
* -1 on error (hname not present)
*/
static int hashID(const char* hname)
{
int id;
assert(hname);
for (id=0; id < NB_HASHES; id++) {
assert(hashCandidates[id].name);
if (strlen(hname) != strlen(hashCandidates[id].name)) continue;
if (isCommand(hname, hashCandidates[id].name)) return id;
}
return -1;
}
static int help(const char* exename)
{
printf("Usage: %s [options]... [hash]\n", exename);
printf("Runs various benchmarks at various lengths for the listed hash functions\n");
printf("and outputs them in a CSV format.\n\n");
printf("Options: \n");
printf(" --list Name available hash algorithms and exit \n");
printf(" --mins=LEN Starting length for small size bench (default: %i) \n", SMALL_SIZE_MIN_DEFAULT);
printf(" --maxs=LEN End length for small size bench (default: %i) \n", SMALL_SIZE_MAX_DEFAULT);
printf(" --minl=LEN Starting log2(length) for large size bench (default: %i) \n", LARGE_SIZELOG_MIN_DEFAULT);
printf(" --maxl=LEN End log2(length) for large size bench (default: %i) \n", LARGE_SIZELOG_MAX_DEFAULT);
printf(" [hash] Optional, bench all available hashes if not provided \n");
return 0;
}
static int badusage(const char* exename)
{
printf("Bad command ... \n");
help(exename);
return 1;
}
int main(int argc, const char* argv[])
{
const char* const exename = argv[0];
int hashNb = 0;
int nb_h_test = NB_HASHES;
int largeTest_log_min = LARGE_SIZELOG_MIN_DEFAULT;
int largeTest_log_max = LARGE_SIZELOG_MAX_DEFAULT;
size_t smallTest_size_min = SMALL_SIZE_MIN_DEFAULT;
size_t smallTest_size_max = SMALL_SIZE_MAX_DEFAULT;
int arg_nb;
for (arg_nb = 1; arg_nb < argc; arg_nb++) {
const char** arg = argv + arg_nb;
if (isCommand(*arg, "-h")) { assert(argc >= 1); return help(exename); }
if (isCommand(*arg, "--list")) { return display_hash_names(); }
if (longCommandWArg(arg, "--n=")) { nb_h_test = readIntFromChar(arg); continue; } /* hidden command */
if (longCommandWArg(arg, "--minl=")) { largeTest_log_min = readIntFromChar(arg); continue; }
if (longCommandWArg(arg, "--maxl=")) { largeTest_log_max = readIntFromChar(arg); continue; }
if (longCommandWArg(arg, "--mins=")) { smallTest_size_min = (size_t)readIntFromChar(arg); continue; }
if (longCommandWArg(arg, "--maxs=")) { smallTest_size_max = (size_t)readIntFromChar(arg); continue; }
/* not a command: must be a hash name */
hashNb = hashID(*arg);
if (hashNb >= 0) {
nb_h_test = 1;
} else {
/* not a hash name: error */
return badusage(exename);
}
}
/* border case (requires (mis)using hidden command `--n=#`) */
if (hashNb + nb_h_test > NB_HASHES) {
printf("wrong hash selection \n");
return 1;
}
printf(" === benchmarking %i hash functions === \n", nb_h_test);
if (largeTest_log_max >= largeTest_log_min) {
bench_largeInput(hashCandidates+hashNb, nb_h_test, largeTest_log_min, largeTest_log_max);
}
if (smallTest_size_max >= smallTest_size_min) {
bench_throughput_smallInputs(hashCandidates+hashNb, nb_h_test, smallTest_size_min, smallTest_size_max);
bench_throughput_randomInputLength(hashCandidates+hashNb, nb_h_test, smallTest_size_min, smallTest_size_max);
bench_latency_smallInputs(hashCandidates+hashNb, nb_h_test, smallTest_size_min, smallTest_size_max);
bench_latency_randomInputLength(hashCandidates+hashNb, nb_h_test, smallTest_size_min, smallTest_size_max);
}
return 0;
}