main.c 7.3 KB

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