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_shingles_test.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "shingles.h"
  26. #include "fstring.h"
  27. #include "ottery.h"
  28. static void
  29. generate_random_string (char *begin, size_t len)
  30. {
  31. gsize i;
  32. for (i = 0; i < len; i ++) {
  33. begin[i] = ottery_rand_range ('z' - 'a') + 'a';
  34. }
  35. }
  36. static GArray *
  37. generate_fuzzy_words (gsize cnt, gsize max_len)
  38. {
  39. GArray *res;
  40. gsize i, wlen;
  41. rspamd_fstring_t w;
  42. res = g_array_sized_new (FALSE, FALSE, sizeof (rspamd_fstring_t), cnt);
  43. for (i = 0; i < cnt; i ++) {
  44. wlen = ottery_rand_range (max_len) + 1;
  45. w.len = w.size = wlen;
  46. w.begin = g_malloc (wlen);
  47. generate_random_string (w.begin, wlen);
  48. g_array_append_val (res, w);
  49. }
  50. return res;
  51. }
  52. static void
  53. permute_vector (GArray *in, gdouble prob)
  54. {
  55. gsize i, total = 0;
  56. rspamd_fstring_t *w;
  57. for (i = 0; i < in->len; i ++) {
  58. if (ottery_rand_unsigned () <= G_MAXUINT * prob) {
  59. w = &g_array_index (in, rspamd_fstring_t, i);
  60. generate_random_string (w->begin, w->len);
  61. total ++;
  62. }
  63. }
  64. msg_debug ("generated %z permutations of %ud words", total, in->len);
  65. }
  66. static void
  67. free_fuzzy_words (GArray *ar)
  68. {
  69. gsize i;
  70. rspamd_fstring_t *w;
  71. for (i = 0; i < ar->len; i ++) {
  72. w = &g_array_index (ar, rspamd_fstring_t, i);
  73. g_free (w->begin);
  74. }
  75. }
  76. static void
  77. test_case (gsize cnt, gsize max_len, gdouble perm_factor)
  78. {
  79. GArray *input;
  80. struct rspamd_shingle *sgl, *sgl_permuted;
  81. gdouble res;
  82. guchar key[16];
  83. gdouble ts1, ts2;
  84. ottery_rand_bytes (key, sizeof (key));
  85. input = generate_fuzzy_words (cnt, max_len);
  86. ts1 = rspamd_get_ticks ();
  87. sgl = rspamd_shingles_generate (input, key, NULL,
  88. rspamd_shingles_default_filter, NULL);
  89. ts2 = rspamd_get_ticks ();
  90. permute_vector (input, perm_factor);
  91. sgl_permuted = rspamd_shingles_generate (input, key, NULL,
  92. rspamd_shingles_default_filter, NULL);
  93. res = rspamd_shingles_compare (sgl, sgl_permuted);
  94. msg_debug ("percentage of common shingles: %.3f, generate time: %hd usec",
  95. res, (gint)(ts1 - ts2) * 1000);
  96. g_assert_cmpfloat (fabs ((1.0 - res) - sqrt (perm_factor)), <=, 0.20);
  97. free_fuzzy_words (input);
  98. g_free (sgl);
  99. g_free (sgl_permuted);
  100. }
  101. void
  102. rspamd_shingles_test_func (void)
  103. {
  104. //test_case (5, 100, 0.5);
  105. test_case (200, 10, 0.1);
  106. test_case (500, 20, 0.01);
  107. test_case (5000, 20, 0.01);
  108. test_case (5000, 15, 0);
  109. test_case (5000, 30, 1.0);
  110. }