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_lua_pcall_vs_resume_test.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*-
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. #include "config.h"
  15. #include "rspamd.h"
  16. #include "util.h"
  17. #include "lua/lua_common.h"
  18. #include "lua/lua_thread_pool.h"
  19. #include "unix-std.h"
  20. static const char *lua_src_name = "lua/pcall_test.lua";
  21. extern gchar *argv0_dirname;
  22. extern struct rspamd_main *rspamd_main;
  23. const int N = 20000;
  24. static gdouble
  25. test_pcall(lua_State *L, gint function_call)
  26. {
  27. gdouble t1, t2;
  28. gint i;
  29. t1 = rspamd_get_virtual_ticks ();
  30. for (i = 0; i < N; i ++) {
  31. lua_rawgeti (L, LUA_REGISTRYINDEX, function_call);
  32. lua_pcall (L, 0, 1, 0);
  33. lua_pop (L, 1);
  34. }
  35. t2 = rspamd_get_virtual_ticks ();
  36. return t2 - t1;
  37. }
  38. static gdouble
  39. test_resume(lua_State *L, gint function_call)
  40. {
  41. gdouble t1, t2;
  42. gint i;
  43. t1 = rspamd_get_virtual_ticks ();
  44. for (i = 0; i < N; i ++) {
  45. lua_rawgeti (L, LUA_REGISTRYINDEX, function_call);
  46. #if LUA_VERSION_NUM < 502
  47. lua_resume (L, 0);
  48. #else
  49. lua_resume (L, NULL, 0);
  50. #endif
  51. lua_pop (L, 1);
  52. }
  53. t2 = rspamd_get_virtual_ticks ();
  54. return t2 - t1;
  55. }
  56. static gdouble
  57. test_resume_get_thread(gint function_call)
  58. {
  59. gdouble t1, t2;
  60. gint i;
  61. struct thread_entry *ent;
  62. t1 = rspamd_get_virtual_ticks ();
  63. for (i = 0; i < N; i ++) {
  64. ent = lua_thread_pool_get_for_config (rspamd_main->cfg);
  65. lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
  66. #if LUA_VERSION_NUM < 502
  67. lua_resume (ent->lua_state, 0);
  68. #else
  69. lua_resume (ent->lua_state, NULL, 0);
  70. #endif
  71. lua_pop (ent->lua_state, 1);
  72. lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent);
  73. }
  74. t2 = rspamd_get_virtual_ticks ();
  75. return t2 - t1;
  76. }
  77. static gdouble
  78. test_resume_get_new_thread(gint function_call)
  79. {
  80. gdouble t1, t2;
  81. gint i;
  82. struct thread_entry *ent;
  83. t1 = rspamd_get_virtual_ticks ();
  84. for (i = 0; i < N; i ++) {
  85. ent = lua_thread_pool_get_for_task (rspamd_main->cfg->lua_thread_pool);
  86. lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
  87. #if LUA_VERSION_NUM < 502
  88. lua_resume (ent->lua_state, 0);
  89. #else
  90. lua_resume (ent->lua_state, NULL, 0);
  91. #endif
  92. lua_pop (ent->lua_state, 1);
  93. /* lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); */
  94. }
  95. t2 = rspamd_get_virtual_ticks ();
  96. return t2 - t1;
  97. }
  98. void
  99. rspamd_lua_lua_pcall_vs_resume_test_func (void)
  100. {
  101. lua_State *L = rspamd_main->cfg->lua_state;
  102. gchar *lua_src;
  103. gdouble t1, reference;
  104. lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL);
  105. if (luaL_dofile (L, lua_src) != 0) {
  106. msg_err ("failed to load test file: %s ", lua_tostring (L, -1));
  107. g_assert (0);
  108. }
  109. g_free (lua_src);
  110. gint function_call = luaL_ref (L, LUA_REGISTRYINDEX);
  111. msg_info ("calling");
  112. reference = t1 = test_pcall(L, function_call);
  113. msg_notice ("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  114. t1 = test_resume (L, function_call);
  115. msg_notice ("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  116. t1 = test_resume_get_thread (function_call);
  117. msg_notice ("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  118. t1 = test_resume_get_new_thread (function_call);
  119. msg_notice ("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  120. }