hashtable_bench.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @file hashtable_bench.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <misc/offset.h>
  24. #include <misc/brandom.h>
  25. #include <misc/debug.h>
  26. #include <misc/jenkins_hash.h>
  27. #include <structure/HashTable.h>
  28. struct mynode {
  29. int used;
  30. int num;
  31. HashTableNode hash_node;
  32. };
  33. static int key_comparator (int *key1, int *key2)
  34. {
  35. return (*key1 == *key2);
  36. }
  37. static int hash_function (int *key, int modulo)
  38. {
  39. return (jenkins_one_at_a_time_hash((uint8_t *)key, sizeof(int)) % modulo);
  40. }
  41. static void print_indent (int indent)
  42. {
  43. for (int i = 0; i < indent; i++) {
  44. printf(" ");
  45. }
  46. }
  47. int main (int argc, char **argv)
  48. {
  49. int num_nodes;
  50. int num_random_delete;
  51. if (argc != 3 || (num_nodes = atoi(argv[1])) <= 0 || (num_random_delete = atoi(argv[2])) < 0) {
  52. fprintf(stderr, "Usage: %s <num> <numrandomdelete>\n", (argc > 0 ? argv[0] : NULL));
  53. return 1;
  54. }
  55. struct mynode *nodes = malloc(num_nodes * sizeof(*nodes));
  56. if (!nodes) {
  57. fprintf(stderr, "malloc failed\n");
  58. return 1;
  59. }
  60. int *values_ins = malloc(num_nodes * sizeof(int));
  61. ASSERT_FORCE(values_ins)
  62. int *values = malloc(num_random_delete * sizeof(int));
  63. ASSERT_FORCE(values)
  64. HashTable ht;
  65. if (!HashTable_Init(
  66. &ht,
  67. OFFSET_DIFF(struct mynode, num, hash_node),
  68. (HashTable_comparator)key_comparator,
  69. (HashTable_hash_function)hash_function,
  70. num_nodes * 2
  71. )) {
  72. fprintf(stderr, "HashTable_Init failed\n");
  73. return 1;
  74. }
  75. printf("Inserting random values...\n");
  76. brandom_randomize((uint8_t *)values_ins, num_nodes * sizeof(int));
  77. for (int i = 0; i < num_nodes; i++) {
  78. nodes[i].num = values_ins[i];
  79. if (HashTable_Insert(&ht, &nodes[i].hash_node)) {
  80. nodes[i].used = 1;
  81. } else {
  82. nodes[i].used = 0;
  83. printf("Insert collision!\n");
  84. }
  85. }
  86. printf("Removing random entries...\n");
  87. int removed = 0;
  88. brandom_randomize((uint8_t *)values, num_random_delete * sizeof(int));
  89. for (int i = 0; i < num_random_delete; i++) {
  90. int index = (((unsigned int *)values)[i] % num_nodes);
  91. struct mynode *node = nodes + index;
  92. if (node->used) {
  93. ASSERT_EXECUTE(HashTable_Remove(&ht, &node->num))
  94. node->used = 0;
  95. removed++;
  96. }
  97. }
  98. printf("Removed %d entries\n", removed);
  99. free(nodes);
  100. free(values);
  101. return 0;
  102. }