/* * Main program to benchmark hash functions * Part of the xxHash project * Copyright (C) 2019-2020 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 /* printf */ #include /* INT_MAX */ #include "bhDisplay.h" /* bench_x */ /* === defines list of hashes `hashCandidates` and NB_HASHES *** */ #include "hashes.h" /* === parse command line === */ #undef NDEBUG #include /*! * 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; } /** * longCommand(): * 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= 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; }