]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Benchmark for lua calls
authorMikhail Galanin <mgalanin@mimecast.com>
Mon, 20 Aug 2018 09:27:12 +0000 (10:27 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Mon, 20 Aug 2018 09:27:12 +0000 (10:27 +0100)
test/CMakeLists.txt
test/lua/pcall_test.lua [new file with mode: 0644]
test/rspamd_lua_pcall_vs_resume_test.c [new file with mode: 0644]
test/rspamd_test_suite.c
test/tests.h

index f2d84652568cd691f95c9bb527f8c790e380d49c..226db80ef1c5137539db4a4e37ec55e84c8d29c6 100644 (file)
@@ -9,6 +9,7 @@ SET(TESTSRC             rspamd_mem_pool_test.c
                                rspamd_shingles_test.c
                                rspamd_upstream_test.c
                                rspamd_http_test.c
+                               rspamd_lua_pcall_vs_resume_test.c
                                rspamd_lua_test.c
                                rspamd_cryptobox_test.c
                                rspamd_heap_test.c
diff --git a/test/lua/pcall_test.lua b/test/lua/pcall_test.lua
new file mode 100644 (file)
index 0000000..26f3ae4
--- /dev/null
@@ -0,0 +1,45 @@
+--[[ https://en.wikipedia.org/wiki/Normal_distribution ]]
+
+-- The Box–Muller method
+local function gaussian(mean, variance)
+  local U = math.random()
+  local V = math.random()
+  return  math.sqrt(-2.0 * variance * math.log(U)) *
+      math.cos(2.0 * math.pi * V) + mean
+end
+
+local function mean(t)
+  local sum = 0
+  local count = #t
+  for i = 1, count do
+    sum = sum + t[i]
+  end
+  return sum / count
+end
+
+local function std(t, mean)
+  local squares = 0.0
+  for i = 1, #t do
+    local deviation = math.abs(mean - t[i])
+    squares = squares + deviation * deviation
+  end
+  local variance = squares / #t
+  return math.sqrt(variance)
+end
+
+local function do_the_call()
+  local t = {}
+  local mu = 34.0
+  local sigma = 10.0
+
+  for i = 1, 5 do
+    table.insert(t, gaussian(mu, sigma))
+  end
+
+  return string.format("Got mean: %1.5f, mu: %1.5f\nstd deviance:%1.5f, expected: %1.5f",
+    mean(t), mu,
+    std(t, mu), math.sqrt(sigma))
+end
+
+math.randomseed(os.time())
+return do_the_call
diff --git a/test/rspamd_lua_pcall_vs_resume_test.c b/test/rspamd_lua_pcall_vs_resume_test.c
new file mode 100644 (file)
index 0000000..03f1d86
--- /dev/null
@@ -0,0 +1,139 @@
+/*-
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+#include "rspamd.h"
+#include "util.h"
+#include "lua/lua_common.h"
+#include "lua/lua_thread_pool.h"
+#include "unix-std.h"
+
+static const char *lua_src = BUILDROOT "/test/lua/pcall_test.lua";
+
+extern struct rspamd_main *rspamd_main;
+
+const int N = 20000;
+
+
+static gdouble
+test_pcall(lua_State *L, gint function_call)
+{
+       gdouble t1, t2;
+       gint i;
+       t1 = rspamd_get_virtual_ticks ();
+
+       for (i = 0; i < N; i ++) {
+               lua_rawgeti (L, LUA_REGISTRYINDEX, function_call);
+               lua_pcall (L, 0, 1, 0);
+               lua_pop (L, 1);
+       }
+
+       t2 = rspamd_get_virtual_ticks ();
+
+       return t2 - t1;
+}
+
+static gdouble
+test_resume(lua_State *L, gint function_call)
+{
+       gdouble t1, t2;
+       gint i;
+       t1 = rspamd_get_virtual_ticks ();
+
+       for (i = 0; i < N; i ++) {
+               lua_rawgeti (L, LUA_REGISTRYINDEX, function_call);
+               lua_resume (L, 0);
+               lua_pop (L, 1);
+       }
+
+       t2 = rspamd_get_virtual_ticks ();
+
+       return t2 - t1;
+}
+
+static gdouble
+test_resume_get_thread(gint function_call)
+{
+       gdouble t1, t2;
+       gint i;
+       struct thread_entry *ent;
+
+       t1 = rspamd_get_virtual_ticks ();
+
+       for (i = 0; i < N; i ++) {
+               ent = lua_thread_pool_get (rspamd_main->cfg->lua_thread_pool);
+
+               lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
+               lua_resume (ent->lua_state, 0);
+               lua_pop (ent->lua_state, 1);
+
+               lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent);
+       }
+
+       t2 = rspamd_get_virtual_ticks ();
+
+       return t2 - t1;
+}
+
+static gdouble
+test_resume_get_new_thread(gint function_call)
+{
+       gdouble t1, t2;
+       gint i;
+       struct thread_entry *ent;
+
+       t1 = rspamd_get_virtual_ticks ();
+
+       for (i = 0; i < N; i ++) {
+               ent = lua_thread_pool_get (rspamd_main->cfg->lua_thread_pool);
+
+               lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
+               lua_resume (ent->lua_state, 0);
+               lua_pop (ent->lua_state, 1);
+
+               /* lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); */
+       }
+
+       t2 = rspamd_get_virtual_ticks ();
+
+       return t2 - t1;
+}
+
+void
+rspamd_lua_lua_pcall_vs_resume_test_func (void)
+{
+       lua_State *L = rspamd_main->cfg->lua_state;
+       gdouble t1, reference;
+
+       if (luaL_dofile (L, lua_src) != 0) {
+               msg_err ("failed to load test file: %s ", lua_tostring (L, -1));
+               g_assert (0);
+       }
+
+       gint function_call = luaL_ref (L, LUA_REGISTRYINDEX);
+
+       msg_info ("calling");
+
+       reference = t1 = test_pcall(L, function_call);
+       msg_notice ("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
+
+       t1 = test_resume (L, function_call);
+       msg_notice ("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
+
+       t1 = test_resume_get_thread (function_call);
+       msg_notice ("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
+
+       t1 = test_resume_get_new_thread (function_call);
+       msg_notice ("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
+}
\ No newline at end of file
index 2f8e893deb18cf197dd072957450555764ba3627..2ea73eff39f335417452a4e30eb2b4dbd4b05b60 100644 (file)
@@ -10,7 +10,7 @@ worker_t *workers[] = { NULL };
 int
 main (int argc, char **argv)
 {
-       struct rspamd_config            *cfg;
+       struct rspamd_config *cfg;
 
        rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main));
        memset (rspamd_main, 0, sizeof (struct rspamd_main));
@@ -52,6 +52,7 @@ main (int argc, char **argv)
        g_test_add_func ("/rspamd/lua", rspamd_lua_test_func);
        g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func);
        g_test_add_func ("/rspamd/heap", rspamd_heap_test_func);
+       g_test_add_func ("/rspamd/lua_pcall", rspamd_lua_lua_pcall_vs_resume_test_func);
 
 #if 0
        g_test_add_func ("/rspamd/url", rspamd_url_test_func);
index 0ba03da8d290817f14cee799340e71ee26fd19e0..9a70d8d5df9ce470c24f4ecc63db5f3a8b766b08 100644 (file)
@@ -41,4 +41,6 @@ void rspamd_cryptobox_test_func (void);
 
 void rspamd_heap_test_func (void);
 
+void rspamd_lua_lua_pcall_vs_resume_test_func(void);
+
 #endif