BLI_ghash_performance_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include "BLI_ressource_strings.h"
  4. #define GHASH_INTERNAL_API
  5. extern "C" {
  6. #include "MEM_guardedalloc.h"
  7. #include "BLI_utildefines.h"
  8. #include "BLI_ghash.h"
  9. #include "BLI_rand.h"
  10. #include "BLI_string.h"
  11. #include "PIL_time_utildefines.h"
  12. }
  13. /* Using http://corpora.uni-leipzig.de/downloads/eng_wikipedia_2010_1M-text.tar.gz
  14. * (1 million of words, about 122MB of text) from
  15. * http://corpora.informatik.uni-leipzig.de/download.html */
  16. #if 0
  17. # define TEXT_CORPUS_PATH \
  18. "/path/to/Téléchargements/eng_wikipedia_2010_1M-text/eng_wikipedia_2010_1M-sentences.txt"
  19. #endif
  20. /* Resizing the hash has a huge cost over global filling operation! */
  21. //#define GHASH_RESERVE
  22. /* Run the longest tests! */
  23. //#define GHASH_RUN_BIG
  24. /* Size of 'small case' ghash (number of entries). */
  25. #define TESTCASE_SIZE_SMALL 17
  26. #define PRINTF_GHASH_STATS(_gh) \
  27. { \
  28. double q, lf, var, pempty, poverloaded; \
  29. int bigb; \
  30. q = BLI_ghash_calc_quality_ex((_gh), &lf, &var, &pempty, &poverloaded, &bigb); \
  31. printf( \
  32. "GHash stats (%u entries):\n\t" \
  33. "Quality (the lower the better): %f\n\tVariance (the lower the better): %f\n\tLoad: " \
  34. "%f\n\t" \
  35. "Empty buckets: %.2f%%\n\tOverloaded buckets: %.2f%% (biggest bucket: %d)\n", \
  36. BLI_ghash_len(_gh), \
  37. q, \
  38. var, \
  39. lf, \
  40. pempty * 100.0, \
  41. poverloaded * 100.0, \
  42. bigb); \
  43. } \
  44. void(0)
  45. /* Str: whole text, lines and words from a 'corpus' text. */
  46. static void str_ghash_tests(GHash *ghash, const char *id)
  47. {
  48. printf("\n========== STARTING %s ==========\n", id);
  49. #ifdef TEXT_CORPUS_PATH
  50. size_t sz = 0;
  51. char *data;
  52. {
  53. struct stat st;
  54. if (stat(TEXT_CORPUS_PATH, &st) == 0)
  55. sz = st.st_size;
  56. }
  57. if (sz != 0) {
  58. FILE *f = fopen(TEXT_CORPUS_PATH, "r");
  59. data = (char *)MEM_mallocN(sz + 1, __func__);
  60. if (fread(data, sizeof(*data), sz, f) != sz) {
  61. printf("ERROR in reading file %s!", TEXT_CORPUS_PATH);
  62. MEM_freeN(data);
  63. data = BLI_strdup(words10k);
  64. }
  65. data[sz] = '\0';
  66. fclose(f);
  67. }
  68. else {
  69. data = BLI_strdup(words10k);
  70. }
  71. #else
  72. char *data = BLI_strdup(words10k);
  73. #endif
  74. char *data_p = BLI_strdup(data);
  75. char *data_w = BLI_strdup(data);
  76. char *data_bis = BLI_strdup(data);
  77. {
  78. char *p, *w, *c_p, *c_w;
  79. TIMEIT_START(string_insert);
  80. #ifdef GHASH_RESERVE
  81. BLI_ghash_reserve(ghash, strlen(data) / 32); /* rough estimation... */
  82. #endif
  83. BLI_ghash_insert(ghash, data, POINTER_FROM_INT(data[0]));
  84. for (p = c_p = data_p, w = c_w = data_w; *c_w; c_w++, c_p++) {
  85. if (*c_p == '.') {
  86. *c_p = *c_w = '\0';
  87. if (!BLI_ghash_haskey(ghash, p)) {
  88. BLI_ghash_insert(ghash, p, POINTER_FROM_INT(p[0]));
  89. }
  90. if (!BLI_ghash_haskey(ghash, w)) {
  91. BLI_ghash_insert(ghash, w, POINTER_FROM_INT(w[0]));
  92. }
  93. p = c_p + 1;
  94. w = c_w + 1;
  95. }
  96. else if (*c_w == ' ') {
  97. *c_w = '\0';
  98. if (!BLI_ghash_haskey(ghash, w)) {
  99. BLI_ghash_insert(ghash, w, POINTER_FROM_INT(w[0]));
  100. }
  101. w = c_w + 1;
  102. }
  103. }
  104. TIMEIT_END(string_insert);
  105. }
  106. PRINTF_GHASH_STATS(ghash);
  107. {
  108. char *p, *w, *c;
  109. void *v;
  110. TIMEIT_START(string_lookup);
  111. v = BLI_ghash_lookup(ghash, data_bis);
  112. EXPECT_EQ(POINTER_AS_INT(v), data_bis[0]);
  113. for (p = w = c = data_bis; *c; c++) {
  114. if (*c == '.') {
  115. *c = '\0';
  116. v = BLI_ghash_lookup(ghash, w);
  117. EXPECT_EQ(POINTER_AS_INT(v), w[0]);
  118. v = BLI_ghash_lookup(ghash, p);
  119. EXPECT_EQ(POINTER_AS_INT(v), p[0]);
  120. p = w = c + 1;
  121. }
  122. else if (*c == ' ') {
  123. *c = '\0';
  124. v = BLI_ghash_lookup(ghash, w);
  125. EXPECT_EQ(POINTER_AS_INT(v), w[0]);
  126. w = c + 1;
  127. }
  128. }
  129. TIMEIT_END(string_lookup);
  130. }
  131. BLI_ghash_free(ghash, NULL, NULL);
  132. MEM_freeN(data);
  133. MEM_freeN(data_p);
  134. MEM_freeN(data_w);
  135. MEM_freeN(data_bis);
  136. printf("========== ENDED %s ==========\n\n", id);
  137. }
  138. TEST(ghash, TextGHash)
  139. {
  140. GHash *ghash = BLI_ghash_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, __func__);
  141. str_ghash_tests(ghash, "StrGHash - GHash");
  142. }
  143. TEST(ghash, TextMurmur2a)
  144. {
  145. GHash *ghash = BLI_ghash_new(BLI_ghashutil_strhash_p_murmur, BLI_ghashutil_strcmp, __func__);
  146. str_ghash_tests(ghash, "StrGHash - Murmur");
  147. }
  148. /* Int: uniform 100M first integers. */
  149. static void int_ghash_tests(GHash *ghash, const char *id, const unsigned int nbr)
  150. {
  151. printf("\n========== STARTING %s ==========\n", id);
  152. {
  153. unsigned int i = nbr;
  154. TIMEIT_START(int_insert);
  155. #ifdef GHASH_RESERVE
  156. BLI_ghash_reserve(ghash, nbr);
  157. #endif
  158. while (i--) {
  159. BLI_ghash_insert(ghash, POINTER_FROM_UINT(i), POINTER_FROM_UINT(i));
  160. }
  161. TIMEIT_END(int_insert);
  162. }
  163. PRINTF_GHASH_STATS(ghash);
  164. {
  165. unsigned int i = nbr;
  166. TIMEIT_START(int_lookup);
  167. while (i--) {
  168. void *v = BLI_ghash_lookup(ghash, POINTER_FROM_UINT(i));
  169. EXPECT_EQ(POINTER_AS_UINT(v), i);
  170. }
  171. TIMEIT_END(int_lookup);
  172. }
  173. {
  174. void *k, *v;
  175. TIMEIT_START(int_pop);
  176. GHashIterState pop_state = {0};
  177. while (BLI_ghash_pop(ghash, &pop_state, &k, &v)) {
  178. EXPECT_EQ(k, v);
  179. }
  180. TIMEIT_END(int_pop);
  181. }
  182. EXPECT_EQ(BLI_ghash_len(ghash), 0);
  183. BLI_ghash_free(ghash, NULL, NULL);
  184. printf("========== ENDED %s ==========\n\n", id);
  185. }
  186. TEST(ghash, IntGHash12000)
  187. {
  188. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  189. int_ghash_tests(ghash, "IntGHash - GHash - 12000", 12000);
  190. }
  191. #ifdef GHASH_RUN_BIG
  192. TEST(ghash, IntGHash100000000)
  193. {
  194. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  195. int_ghash_tests(ghash, "IntGHash - GHash - 100000000", 100000000);
  196. }
  197. #endif
  198. TEST(ghash, IntMurmur2a12000)
  199. {
  200. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  201. int_ghash_tests(ghash, "IntGHash - Murmur - 12000", 12000);
  202. }
  203. #ifdef GHASH_RUN_BIG
  204. TEST(ghash, IntMurmur2a100000000)
  205. {
  206. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  207. int_ghash_tests(ghash, "IntGHash - Murmur - 100000000", 100000000);
  208. }
  209. #endif
  210. /* Int: random 50M integers. */
  211. static void randint_ghash_tests(GHash *ghash, const char *id, const unsigned int nbr)
  212. {
  213. printf("\n========== STARTING %s ==========\n", id);
  214. unsigned int *data = (unsigned int *)MEM_mallocN(sizeof(*data) * (size_t)nbr, __func__);
  215. unsigned int *dt;
  216. unsigned int i;
  217. {
  218. RNG *rng = BLI_rng_new(0);
  219. for (i = nbr, dt = data; i--; dt++) {
  220. *dt = BLI_rng_get_uint(rng);
  221. }
  222. BLI_rng_free(rng);
  223. }
  224. {
  225. TIMEIT_START(int_insert);
  226. #ifdef GHASH_RESERVE
  227. BLI_ghash_reserve(ghash, nbr);
  228. #endif
  229. for (i = nbr, dt = data; i--; dt++) {
  230. BLI_ghash_insert(ghash, POINTER_FROM_UINT(*dt), POINTER_FROM_UINT(*dt));
  231. }
  232. TIMEIT_END(int_insert);
  233. }
  234. PRINTF_GHASH_STATS(ghash);
  235. {
  236. TIMEIT_START(int_lookup);
  237. for (i = nbr, dt = data; i--; dt++) {
  238. void *v = BLI_ghash_lookup(ghash, POINTER_FROM_UINT(*dt));
  239. EXPECT_EQ(POINTER_AS_UINT(v), *dt);
  240. }
  241. TIMEIT_END(int_lookup);
  242. }
  243. BLI_ghash_free(ghash, NULL, NULL);
  244. printf("========== ENDED %s ==========\n\n", id);
  245. }
  246. TEST(ghash, IntRandGHash12000)
  247. {
  248. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  249. randint_ghash_tests(ghash, "RandIntGHash - GHash - 12000", 12000);
  250. }
  251. #ifdef GHASH_RUN_BIG
  252. TEST(ghash, IntRandGHash50000000)
  253. {
  254. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  255. randint_ghash_tests(ghash, "RandIntGHash - GHash - 50000000", 50000000);
  256. }
  257. #endif
  258. TEST(ghash, IntRandMurmur2a12000)
  259. {
  260. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  261. randint_ghash_tests(ghash, "RandIntGHash - Murmur - 12000", 12000);
  262. }
  263. #ifdef GHASH_RUN_BIG
  264. TEST(ghash, IntRandMurmur2a50000000)
  265. {
  266. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  267. randint_ghash_tests(ghash, "RandIntGHash - Murmur - 50000000", 50000000);
  268. }
  269. #endif
  270. static unsigned int ghashutil_tests_nohash_p(const void *p)
  271. {
  272. return POINTER_AS_UINT(p);
  273. }
  274. static bool ghashutil_tests_cmp_p(const void *a, const void *b)
  275. {
  276. return a != b;
  277. }
  278. TEST(ghash, Int4NoHash12000)
  279. {
  280. GHash *ghash = BLI_ghash_new(ghashutil_tests_nohash_p, ghashutil_tests_cmp_p, __func__);
  281. randint_ghash_tests(ghash, "RandIntGHash - No Hash - 12000", 12000);
  282. }
  283. #ifdef GHASH_RUN_BIG
  284. TEST(ghash, Int4NoHash50000000)
  285. {
  286. GHash *ghash = BLI_ghash_new(ghashutil_tests_nohash_p, ghashutil_tests_cmp_p, __func__);
  287. randint_ghash_tests(ghash, "RandIntGHash - No Hash - 50000000", 50000000);
  288. }
  289. #endif
  290. /* Int_v4: 20M of randomly-generated integer vectors. */
  291. static void int4_ghash_tests(GHash *ghash, const char *id, const unsigned int nbr)
  292. {
  293. printf("\n========== STARTING %s ==========\n", id);
  294. void *data_v = MEM_mallocN(sizeof(unsigned int[4]) * (size_t)nbr, __func__);
  295. unsigned int(*data)[4] = (unsigned int(*)[4])data_v;
  296. unsigned int(*dt)[4];
  297. unsigned int i, j;
  298. {
  299. RNG *rng = BLI_rng_new(0);
  300. for (i = nbr, dt = data; i--; dt++) {
  301. for (j = 4; j--;) {
  302. (*dt)[j] = BLI_rng_get_uint(rng);
  303. }
  304. }
  305. BLI_rng_free(rng);
  306. }
  307. {
  308. TIMEIT_START(int_v4_insert);
  309. #ifdef GHASH_RESERVE
  310. BLI_ghash_reserve(ghash, nbr);
  311. #endif
  312. for (i = nbr, dt = data; i--; dt++) {
  313. BLI_ghash_insert(ghash, *dt, POINTER_FROM_UINT(i));
  314. }
  315. TIMEIT_END(int_v4_insert);
  316. }
  317. PRINTF_GHASH_STATS(ghash);
  318. {
  319. TIMEIT_START(int_v4_lookup);
  320. for (i = nbr, dt = data; i--; dt++) {
  321. void *v = BLI_ghash_lookup(ghash, (void *)(*dt));
  322. EXPECT_EQ(POINTER_AS_UINT(v), i);
  323. }
  324. TIMEIT_END(int_v4_lookup);
  325. }
  326. BLI_ghash_free(ghash, NULL, NULL);
  327. MEM_freeN(data);
  328. printf("========== ENDED %s ==========\n\n", id);
  329. }
  330. TEST(ghash, Int4GHash2000)
  331. {
  332. GHash *ghash = BLI_ghash_new(
  333. BLI_ghashutil_uinthash_v4_p, BLI_ghashutil_uinthash_v4_cmp, __func__);
  334. int4_ghash_tests(ghash, "Int4GHash - GHash - 2000", 2000);
  335. }
  336. #ifdef GHASH_RUN_BIG
  337. TEST(ghash, Int4GHash20000000)
  338. {
  339. GHash *ghash = BLI_ghash_new(
  340. BLI_ghashutil_uinthash_v4_p, BLI_ghashutil_uinthash_v4_cmp, __func__);
  341. int4_ghash_tests(ghash, "Int4GHash - GHash - 20000000", 20000000);
  342. }
  343. #endif
  344. TEST(ghash, Int4Murmur2a2000)
  345. {
  346. GHash *ghash = BLI_ghash_new(
  347. BLI_ghashutil_uinthash_v4_p_murmur, BLI_ghashutil_uinthash_v4_cmp, __func__);
  348. int4_ghash_tests(ghash, "Int4GHash - Murmur - 2000", 2000);
  349. }
  350. #ifdef GHASH_RUN_BIG
  351. TEST(ghash, Int4Murmur2a20000000)
  352. {
  353. GHash *ghash = BLI_ghash_new(
  354. BLI_ghashutil_uinthash_v4_p_murmur, BLI_ghashutil_uinthash_v4_cmp, __func__);
  355. int4_ghash_tests(ghash, "Int4GHash - Murmur - 20000000", 20000000);
  356. }
  357. #endif
  358. /* MultiSmall: create and manipulate a lot of very small ghashes
  359. * (90% < 10 items, 9% < 100 items, 1% < 1000 items). */
  360. static void multi_small_ghash_tests_one(GHash *ghash, RNG *rng, const unsigned int nbr)
  361. {
  362. unsigned int *data = (unsigned int *)MEM_mallocN(sizeof(*data) * (size_t)nbr, __func__);
  363. unsigned int *dt;
  364. unsigned int i;
  365. for (i = nbr, dt = data; i--; dt++) {
  366. *dt = BLI_rng_get_uint(rng);
  367. }
  368. #ifdef GHASH_RESERVE
  369. BLI_ghash_reserve(ghash, nbr);
  370. #endif
  371. for (i = nbr, dt = data; i--; dt++) {
  372. BLI_ghash_insert(ghash, POINTER_FROM_UINT(*dt), POINTER_FROM_UINT(*dt));
  373. }
  374. for (i = nbr, dt = data; i--; dt++) {
  375. void *v = BLI_ghash_lookup(ghash, POINTER_FROM_UINT(*dt));
  376. EXPECT_EQ(POINTER_AS_UINT(v), *dt);
  377. }
  378. BLI_ghash_clear(ghash, NULL, NULL);
  379. }
  380. static void multi_small_ghash_tests(GHash *ghash, const char *id, const unsigned int nbr)
  381. {
  382. printf("\n========== STARTING %s ==========\n", id);
  383. RNG *rng = BLI_rng_new(0);
  384. TIMEIT_START(multi_small_ghash);
  385. unsigned int i = nbr;
  386. while (i--) {
  387. const int nbr = 1 + (BLI_rng_get_int(rng) % TESTCASE_SIZE_SMALL) *
  388. (!(i % 100) ? 100 : (!(i % 10) ? 10 : 1));
  389. multi_small_ghash_tests_one(ghash, rng, nbr);
  390. }
  391. TIMEIT_END(multi_small_ghash);
  392. TIMEIT_START(multi_small2_ghash);
  393. unsigned int i = nbr;
  394. while (i--) {
  395. const int nbr = 1 + (BLI_rng_get_int(rng) % TESTCASE_SIZE_SMALL) / 2 *
  396. (!(i % 100) ? 100 : (!(i % 10) ? 10 : 1));
  397. multi_small_ghash_tests_one(ghash, rng, nbr);
  398. }
  399. TIMEIT_END(multi_small2_ghash);
  400. BLI_ghash_free(ghash, NULL, NULL);
  401. BLI_rng_free(rng);
  402. printf("========== ENDED %s ==========\n\n", id);
  403. }
  404. TEST(ghash, MultiRandIntGHash2000)
  405. {
  406. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  407. multi_small_ghash_tests(ghash, "MultiSmall RandIntGHash - GHash - 2000", 2000);
  408. }
  409. TEST(ghash, MultiRandIntGHash200000)
  410. {
  411. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, __func__);
  412. multi_small_ghash_tests(ghash, "MultiSmall RandIntGHash - GHash - 200000", 200000);
  413. }
  414. TEST(ghash, MultiRandIntMurmur2a2000)
  415. {
  416. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  417. multi_small_ghash_tests(ghash, "MultiSmall RandIntGHash - Murmur2a - 2000", 2000);
  418. }
  419. TEST(ghash, MultiRandIntMurmur2a200000)
  420. {
  421. GHash *ghash = BLI_ghash_new(BLI_ghashutil_inthash_p_murmur, BLI_ghashutil_intcmp, __func__);
  422. multi_small_ghash_tests(ghash, "MultiSmall RandIntGHash - Murmur2a - 200000", 200000);
  423. }