You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rspamd_radix_test.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "config.h"
  24. #include "main.h"
  25. #include "radix.h"
  26. #include "ottery.h"
  27. const gsize max_elts = 50 * 1024;
  28. const gint lookup_cycles = 1 * 1024;
  29. const uint masks[] = {
  30. 8,
  31. 16,
  32. 24,
  33. 32,
  34. 27,
  35. 29,
  36. 19,
  37. 13,
  38. 22
  39. };
  40. struct _tv {
  41. const char *ip;
  42. const char *nip;
  43. const char *m;
  44. guint32 mask;
  45. guint8 *addr;
  46. guint8 *naddr;
  47. gsize len;
  48. } test_vec[] = {
  49. {"192.168.1.1", "192.168.1.2", "32", 0, 0, 0, 0},
  50. {"192.168.1.0", "192.168.2.1", "24", 0, 0, 0, 0},
  51. {"192.0.0.0", "193.167.2.1", "8", 0, 0, 0, 0},
  52. {"172.0.0.0", "171.16.1.0", "8", 0, 0, 0, 0},
  53. {"172.16.0.1", "127.0.0.1", "16", 0, 0, 0, 0},
  54. {"172.17.1.0", "10.0.0.1", "27", 0, 0, 0, 0},
  55. {"172.17.1.1", "0.0.0.1", "32", 0, 0, 0, 0},
  56. /* Some bad data known to cause problem in the past */
  57. {"191.245.170.246", NULL, "19", 0, 0, 0, 0},
  58. {"227.88.150.170", NULL, "23", 0, 0, 0, 0},
  59. {"105.225.182.92", NULL, "24", 0, 0, 0, 0},
  60. {"223.167.155.240", NULL, "29", 0, 0, 0, 0},
  61. {"125.241.220.172", NULL, "2", 0, 0, 0, 0},
  62. /* Mask = 0 */
  63. {"143.105.181.13", NULL, "8", 0, 0, 0, 0},
  64. {"113.241.233.86", NULL, "26", 0, 0, 0, 0},
  65. {"185.187.122.222", NULL, "8", 0, 0, 0, 0},
  66. {"109.206.26.202", NULL, "12", 0, 0, 0, 0},
  67. {"130.244.233.150", NULL, "0", 0, 0, 0, 0},
  68. {NULL, NULL, NULL, 0, 0, 0, 0}
  69. };
  70. static void
  71. rspamd_radix_text_vec (void)
  72. {
  73. radix_compressed_t *tree = radix_create_compressed ();
  74. struct _tv *t = &test_vec[0];
  75. struct in_addr ina;
  76. struct in6_addr in6a;
  77. gulong i, val;
  78. while (t->ip != NULL) {
  79. t->addr = g_malloc (sizeof (in6a));
  80. t->naddr = g_malloc (sizeof (in6a));
  81. if (inet_pton (AF_INET, t->ip, &ina) == 1) {
  82. memcpy (t->addr, &ina, sizeof (ina));
  83. t->len = sizeof (ina);
  84. }
  85. else if (inet_pton (AF_INET6, t->ip, &in6a) == 1) {
  86. memcpy (t->addr, &in6a, sizeof (in6a));
  87. t->len = sizeof (in6a);
  88. }
  89. else {
  90. g_assert (0);
  91. }
  92. if (t->nip) {
  93. if (inet_pton (AF_INET, t->nip, &ina) == 1) {
  94. memcpy (t->naddr, &ina, sizeof (ina));
  95. }
  96. else if (inet_pton (AF_INET6, t->nip, &in6a) == 1) {
  97. memcpy (t->naddr, &in6a, sizeof (in6a));
  98. }
  99. else {
  100. g_assert (0);
  101. }
  102. }
  103. t->mask = t->len * NBBY - strtoul (t->m, NULL, 10);
  104. t ++;
  105. }
  106. t = &test_vec[0];
  107. i = 0;
  108. while (t->ip != NULL) {
  109. radix_insert_compressed (tree, t->addr, t->len, t->mask, ++i);
  110. t ++;
  111. }
  112. i = 0;
  113. t = &test_vec[0];
  114. while (t->ip != NULL) {
  115. val = radix_find_compressed (tree, t->addr, t->len);
  116. g_assert (val == ++i);
  117. //g_assert (val != RADIX_NO_VALUE);
  118. if (t->nip != NULL) {
  119. val = radix_find_compressed (tree, t->naddr, t->len);
  120. g_assert (val != i);
  121. }
  122. t ++;
  123. }
  124. radix_destroy_compressed (tree);
  125. }
  126. void
  127. rspamd_radix_test_func (void)
  128. {
  129. #if 0
  130. radix_tree_t *tree = radix_tree_create ();
  131. #endif
  132. radix_compressed_t *comp_tree = radix_create_compressed ();
  133. struct {
  134. guint32 addr;
  135. guint32 mask;
  136. guint8 addr6[16];
  137. guint32 mask6;
  138. } *addrs;
  139. gsize nelts, i;
  140. gint lc;
  141. gboolean all_good = TRUE;
  142. gdouble ts1, ts2;
  143. double diff;
  144. /* Test suite for the compressed trie */
  145. rspamd_radix_text_vec ();
  146. nelts = max_elts;
  147. /* First of all we generate many elements and push them to the array */
  148. addrs = g_malloc (nelts * sizeof (addrs[0]));
  149. for (i = 0; i < nelts; i ++) {
  150. addrs[i].addr = ottery_rand_uint32 ();
  151. addrs[i].mask = masks[ottery_rand_range(G_N_ELEMENTS (masks) - 1)];
  152. ottery_rand_bytes (addrs[i].addr6, sizeof(addrs[i].addr6));
  153. addrs[i].mask6 = ottery_rand_range(128);
  154. }
  155. #if 0
  156. msg_info ("old radix performance (%z elts)", nelts);
  157. ts1 = rspamd_get_ticks ();
  158. for (i = 0; i < nelts; i ++) {
  159. guint32 mask = G_MAXUINT32 << (32 - addrs[i].mask);
  160. radix32tree_insert (tree, addrs[i].addr, mask, 1);
  161. }
  162. ts2 = rspamd_get_ticks ();
  163. diff = (ts2 - ts1) * 1000.0;
  164. msg_info ("Added %z elements in %.6f ms", nelts, diff);
  165. ts1 = rspamd_get_ticks ();
  166. for (lc = 0; lc < lookup_cycles; lc ++) {
  167. for (i = 0; i < nelts; i ++) {
  168. g_assert (radix32tree_find (tree, addrs[i].addr) != RADIX_NO_VALUE);
  169. }
  170. }
  171. ts2 = rspamd_get_ticks ();
  172. diff = (ts2 - ts1) * 1000.0;
  173. msg_info ("Checked %z elements in %.6f ms", nelts, diff);
  174. ts1 = rspamd_get_ticks ();
  175. for (i = 0; i < nelts; i ++) {
  176. radix32tree_delete (tree, addrs[i].addr, addrs[i].mask);
  177. }
  178. ts2 = rspamd_get_ticks ();
  179. diff = (ts2 - ts1) * 1000.;
  180. msg_info ("Deleted %z elements in %.6f ms", nelts, diff);
  181. radix_tree_free (tree);
  182. #endif
  183. msg_info ("new radix performance (%z elts)", nelts);
  184. ts1 = rspamd_get_ticks ();
  185. for (i = 0; i < nelts; i ++) {
  186. radix_insert_compressed (comp_tree, addrs[i].addr6, sizeof (addrs[i].addr6),
  187. 128 - addrs[i].mask6, i);
  188. }
  189. ts2 = rspamd_get_ticks ();
  190. diff = (ts2 - ts1) * 1000.0;
  191. msg_info ("Added %z elements in %.6f ms", nelts, diff);
  192. ts1 = rspamd_get_ticks ();
  193. for (lc = 0; lc < lookup_cycles; lc ++) {
  194. for (i = 0; i < nelts; i ++) {
  195. if (radix_find_compressed (comp_tree, addrs[i].addr6, sizeof (addrs[i].addr6))
  196. == RADIX_NO_VALUE) {
  197. all_good = FALSE;
  198. }
  199. }
  200. }
  201. #if 1
  202. if (!all_good) {
  203. for (i = 0; i < nelts; i ++) {
  204. /* Used to write bad random vector */
  205. char ipbuf[INET6_ADDRSTRLEN + 1];
  206. inet_ntop(AF_INET6, addrs[i].addr6, ipbuf, sizeof(ipbuf));
  207. msg_info("{\"%s\", NULL, \"%ud\", 0, 0, 0, 0},",
  208. ipbuf,
  209. addrs[i].mask6);
  210. }
  211. }
  212. #endif
  213. g_assert (all_good);
  214. ts2 = rspamd_get_ticks ();
  215. diff = (ts2 - ts1) * 1000.0;
  216. msg_info ("Checked %z elements in %.6f ms", nelts, diff);
  217. radix_destroy_compressed (comp_tree);
  218. g_free (addrs);
  219. }