]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add radix trie test suite.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 12 Sep 2014 11:18:55 +0000 (12:18 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 12 Sep 2014 11:18:55 +0000 (12:18 +0100)
test/CMakeLists.txt
test/rspamd_radix_test.c [new file with mode: 0644]
test/rspamd_test_suite.c
test/tests.h

index 0972c506b9b590b6f84811c6b423d4c4cf13caec..0767825f0d3171222ca78661a157a3e1fa71ebfa 100644 (file)
@@ -2,12 +2,13 @@ SET(TESTSRC           rspamd_expression_test.c
                                rspamd_mem_pool_test.c
                                rspamd_statfile_test.c
                                rspamd_fuzzy_test.c
-                               rspamd_test_suite.c
                                rspamd_url_test.c
                                rspamd_dns_test.c
                                rspamd_async_test.c
                                rspamd_dkim_test.c
-                               rspamd_rrd_test.c)
+                               rspamd_rrd_test.c
+                               rspamd_radix_test.c
+                               rspamd_test_suite.c)
 
 ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC})
 SET_TARGET_PROPERTIES(rspamd-test PROPERTIES LINKER_LANGUAGE C)
diff --git a/test/rspamd_radix_test.c b/test/rspamd_radix_test.c
new file mode 100644 (file)
index 0000000..3d37a11
--- /dev/null
@@ -0,0 +1,85 @@
+/* Copyright (c) 2014, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *       * Redistributions of source code must retain the above copyright
+ *         notice, this list of conditions and the following disclaimer.
+ *       * Redistributions in binary form must reproduce the above copyright
+ *         notice, this list of conditions and the following disclaimer in the
+ *         documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "main.h"
+#include "radix.h"
+#include "ottery.h"
+
+const gsize max_elts = 50 * 1024 * 1024;
+
+void
+rspamd_radix_test_func (void)
+{
+       radix_tree_t *tree = radix_tree_create ();
+       struct {
+               guint32 addr;
+               guint32 mask;
+       } *addrs;
+       gsize nelts, i;
+       struct timespec ts1, ts2;
+       double diff;
+
+       g_assert (tree != NULL);
+       nelts = max_elts;
+       /* First of all we generate many elements and push them to the array */
+       addrs = g_malloc (nelts * sizeof (addrs[0]));
+
+       for (i = 0; i < nelts; i ++) {
+               addrs[i].addr = ottery_rand_uint32 ();
+               addrs[i].mask = ottery_rand_range (32) + 1;
+       }
+
+       clock_gettime (CLOCK_MONOTONIC, &ts1);
+       for (i = 0; i < nelts; i ++) {
+               radix32tree_insert (tree, addrs[i].addr, addrs[i].mask, 1);
+       }
+       clock_gettime (CLOCK_MONOTONIC, &ts2);
+       diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
+               (ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
+
+       msg_info ("Added %z elements in %.6f ms", nelts, diff);
+
+       clock_gettime (CLOCK_MONOTONIC, &ts1);
+       for (i = 0; i < nelts; i ++) {
+               g_assert (radix32tree_find (tree, addrs[i].addr) != RADIX_NO_VALUE);
+       }
+       clock_gettime (CLOCK_MONOTONIC, &ts2);
+       diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
+                       (ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
+
+       msg_info ("Checked %z elements in %.6f ms", nelts, diff);
+
+       clock_gettime (CLOCK_MONOTONIC, &ts1);
+       for (i = 0; i < nelts; i ++) {
+               radix32tree_delete (tree, addrs[i].addr, addrs[i].mask);
+       }
+       clock_gettime (CLOCK_MONOTONIC, &ts2);
+       diff = (ts2.tv_sec - ts1.tv_sec) * 1000. +   /* Seconds */
+                       (ts2.tv_nsec - ts1.tv_nsec) / 1000000.;  /* Nanoseconds */
+
+       msg_info ("Deleted %z elements in %.6f ms", nelts, diff);
+       g_free (addrs);
+
+       radix_tree_free (tree);
+}
index 73580a9981f99010ce1a8543188e52584e0110c3..864d5a41904fe8074312968d3653cde232586dd4 100644 (file)
@@ -49,6 +49,7 @@ main (int argc, char **argv)
        g_test_add_func ("/rspamd/url", rspamd_url_test_func);
        g_test_add_func ("/rspamd/expression", rspamd_expression_test_func);
        g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func);
+       g_test_add_func ("/rspamd/radix", rspamd_radix_test_func);
        g_test_add_func ("/rspamd/dns", rspamd_dns_test_func);
        g_test_add_func ("/rspamd/aio", rspamd_async_test_func);
        g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func);
index a6779f21ad777fe73c36e3ca0f6ae35a790bf5f3..12906892060b6aadd00662e8be6bf9ee7f47c352 100644 (file)
@@ -20,6 +20,9 @@ void rspamd_fuzzy_test_func (void);
 /* Stat file */
 void rspamd_statfile_test_func (void);
 
+/* Radix test */
+void rspamd_radix_test_func (void);
+
 /* DNS resolving */
 void rspamd_dns_test_func (void);