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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #elif LUA_VERSION_NUM >= 504
  49. lua_resume (L, NULL, 0, NULL);
  50. #else
  51. lua_resume (L, NULL, 0);
  52. #endif
  53. lua_pop (L, 1);
  54. }
  55. t2 = rspamd_get_virtual_ticks ();
  56. return t2 - t1;
  57. }
  58. static gdouble
  59. test_resume_get_thread(gint function_call)
  60. {
  61. gdouble t1, t2;
  62. gint i;
  63. struct thread_entry *ent;
  64. t1 = rspamd_get_virtual_ticks ();
  65. for (i = 0; i < N; i ++) {
  66. ent = lua_thread_pool_get_for_config (rspamd_main->cfg);
  67. lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
  68. #if LUA_VERSION_NUM < 502
  69. lua_resume (ent->lua_state, 0);
  70. #elif LUA_VERSION_NUM >= 504
  71. lua_resume (ent->lua_state, NULL, 0, NULL);
  72. #else
  73. lua_resume (ent->lua_state, NULL, 0);
  74. #endif
  75. lua_pop (ent->lua_state, 1);
  76. lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent);
  77. }
  78. t2 = rspamd_get_virtual_ticks ();
  79. return t2 - t1;
  80. }
  81. static gdouble
  82. test_resume_get_new_thread(gint function_call)
  83. {
  84. gdouble t1, t2;
  85. gint i;
  86. struct thread_entry *ent;
  87. t1 = rspamd_get_virtual_ticks ();
  88. for (i = 0; i < N; i ++) {
  89. ent = lua_thread_pool_get_for_task (rspamd_main->cfg->lua_thread_pool);
  90. lua_rawgeti (ent->lua_state, LUA_REGISTRYINDEX, function_call);
  91. #if LUA_VERSION_NUM < 502
  92. lua_resume (ent->lua_state, 0);
  93. #elif LUA_VERSION_NUM >= 504
  94. lua_resume (ent->lua_state, NULL, 0, NULL);
  95. #else
  96. lua_resume (ent->lua_state, NULL, 0);
  97. #endif
  98. lua_pop (ent->lua_state, 1);
  99. /* lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, ent); */
  100. }
  101. t2 = rspamd_get_virtual_ticks ();
  102. return t2 - t1;
  103. }
  104. void
  105. rspamd_lua_lua_pcall_vs_resume_test_func (void)
  106. {
  107. lua_State *L = rspamd_main->cfg->lua_state;
  108. gchar *lua_src;
  109. gdouble t1, reference;
  110. lua_src = g_build_filename (argv0_dirname, lua_src_name, NULL);
  111. if (luaL_dofile (L, lua_src) != 0) {
  112. msg_err ("failed to load test file: %s ", lua_tostring (L, -1));
  113. g_assert (0);
  114. }
  115. g_free (lua_src);
  116. gint function_call = luaL_ref (L, LUA_REGISTRYINDEX);
  117. msg_info ("calling");
  118. reference = t1 = test_pcall(L, function_call);
  119. msg_notice ("pcall stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  120. t1 = test_resume (L, function_call);
  121. msg_notice ("resume stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  122. t1 = test_resume_get_thread (function_call);
  123. msg_notice ("resume+get thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  124. t1 = test_resume_get_new_thread (function_call);
  125. msg_notice ("resume+get [new] thread stat: ts: %1.5f, avg:%1.5f, slow=%1.2f", t1, t1/(gdouble)N, t1 / reference);
  126. }