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_http_bench.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include "util.h"
  19. #include "http.h"
  20. #include "ottery.h"
  21. #include "cryptobox.h"
  22. #include "unix-std.h"
  23. #include <math.h>
  24. #include <netinet/tcp.h>
  25. #ifdef HAVE_SYS_WAIT_H
  26. #include <sys/wait.h>
  27. #endif
  28. static guint port = 43000;
  29. static gchar *host = "127.0.0.1";
  30. static gchar *server_key = NULL;
  31. static guint cache_size = 10;
  32. static guint nworkers = 1;
  33. static gboolean openssl_mode = FALSE;
  34. static guint file_size = 500;
  35. static guint pconns = 100;
  36. static gdouble test_time = 10.0;
  37. static gchar *latencies_file = NULL;
  38. static gboolean csv_output = FALSE;
  39. /* Dynamic vars */
  40. static rspamd_inet_addr_t *addr;
  41. static guint32 workers_left = 0;
  42. static guint32 *conns_done = NULL;
  43. static const guint store_latencies = 1000;
  44. static guint32 conns_pending = 0;
  45. static GOptionEntry entries[] = {
  46. {"port", 'p', 0, G_OPTION_ARG_INT, &port,
  47. "Port number (default: 43000)", NULL},
  48. {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
  49. "Keys cache size (default: 10)", NULL},
  50. {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
  51. "Number of workers to start (default: 1)", NULL},
  52. {"size", 's', 0, G_OPTION_ARG_INT, &file_size,
  53. "Size of payload to transfer (default: 500)", NULL},
  54. {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns,
  55. "Number of parallel connections (default: 100)", NULL},
  56. {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time,
  57. "Time to run tests (default: 10.0 sec)", NULL},
  58. {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
  59. "Use openssl crypto", NULL},
  60. {"host", 'h', 0, G_OPTION_ARG_STRING, &host,
  61. "Connect to the specified host (default: localhost)", NULL},
  62. {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key,
  63. "Use the specified key (base32 encoded)", NULL},
  64. {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file,
  65. "Write latencies to the specified file", NULL},
  66. {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output,
  67. "Output CSV", NULL},
  68. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  69. };
  70. struct lat_elt {
  71. gdouble lat;
  72. guchar checked;
  73. };
  74. static struct lat_elt *latencies;
  75. static gint
  76. rspamd_client_body (struct rspamd_http_connection *conn,
  77. struct rspamd_http_message *msg,
  78. const gchar *chunk, gsize len)
  79. {
  80. g_assert (chunk[0] == '\0');
  81. return 0;
  82. }
  83. struct client_cbdata {
  84. struct lat_elt *lat;
  85. guint32 *wconns;
  86. gdouble ts;
  87. struct event_base *ev_base;
  88. };
  89. static void
  90. rspamd_client_err (struct rspamd_http_connection *conn, GError *err)
  91. {
  92. msg_info ("abnormally closing connection from: error: %s",
  93. err->message);
  94. g_assert (0);
  95. close (conn->fd);
  96. rspamd_http_connection_unref (conn);
  97. }
  98. static gint
  99. rspamd_client_finish (struct rspamd_http_connection *conn,
  100. struct rspamd_http_message *msg)
  101. {
  102. struct client_cbdata *cb = conn->ud;
  103. cb->lat->lat = rspamd_get_ticks () - cb->ts;
  104. cb->lat->checked = TRUE;
  105. (*cb->wconns) ++;
  106. conns_pending --;
  107. close (conn->fd);
  108. rspamd_http_connection_unref (conn);
  109. g_free (cb);
  110. if (conns_pending == 0) {
  111. event_base_loopexit (cb->ev_base, NULL);
  112. }
  113. return 0;
  114. }
  115. static void
  116. rspamd_http_client_func (struct event_base *ev_base, struct lat_elt *latency,
  117. guint32 *wconns,
  118. gpointer peer_key, gpointer client_key, struct rspamd_keypair_cache *c)
  119. {
  120. struct rspamd_http_message *msg;
  121. struct rspamd_http_connection *conn;
  122. gchar urlbuf[PATH_MAX];
  123. struct client_cbdata *cb;
  124. gint fd, flags;
  125. g_assert (
  126. (fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE)) != -1);
  127. flags = 1;
  128. setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof (flags));
  129. conn = rspamd_http_connection_new (rspamd_client_body, rspamd_client_err,
  130. rspamd_client_finish, RSPAMD_HTTP_CLIENT_SIMPLE,
  131. RSPAMD_HTTP_CLIENT, c);
  132. rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://%s/%d", host, file_size);
  133. msg = rspamd_http_message_from_url (urlbuf);
  134. g_assert (conn != NULL && msg != NULL);
  135. if (peer_key != NULL) {
  136. g_assert (client_key != NULL);
  137. rspamd_http_connection_set_key (conn, client_key);
  138. msg->peer_key = rspamd_http_connection_key_ref (peer_key);
  139. }
  140. cb = g_malloc (sizeof (*cb));
  141. cb->ts = rspamd_get_ticks ();
  142. cb->lat = latency;
  143. cb->ev_base = ev_base;
  144. cb->wconns = wconns;
  145. latency->checked = FALSE;
  146. rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb,
  147. fd, NULL, ev_base);
  148. }
  149. static void
  150. rspamd_worker_func (struct lat_elt *plat, guint32 *wconns)
  151. {
  152. guint i, j;
  153. struct event_base *ev_base;
  154. struct itimerval itv;
  155. struct rspamd_keypair_cache *c = NULL;
  156. gpointer client_key = NULL;
  157. gpointer peer_key = NULL;
  158. if (server_key) {
  159. peer_key = rspamd_http_connection_make_peer_key (server_key);
  160. g_assert (peer_key != NULL);
  161. client_key = rspamd_http_connection_gen_key ();
  162. if (cache_size > 0) {
  163. c = rspamd_keypair_cache_new (cache_size);
  164. }
  165. }
  166. memset (&itv, 0, sizeof (itv));
  167. double_to_tv (test_time, &itv.it_value);
  168. ev_base = event_init ();
  169. g_assert (setitimer (ITIMER_REAL, &itv, NULL) != -1);
  170. for (i = 0; ; i = (i + 1) % store_latencies) {
  171. for (j = 0; j < pconns; j++) {
  172. rspamd_http_client_func (ev_base, &plat[i * pconns + j],
  173. wconns, peer_key, client_key, c);
  174. }
  175. conns_pending = pconns;
  176. event_base_loop (ev_base, 0);
  177. }
  178. }
  179. static int
  180. cmpd (const void *p1, const void *p2)
  181. {
  182. const struct lat_elt *d1 = p1, *d2 = p2;
  183. return (d1->lat) - (d2->lat);
  184. }
  185. double
  186. rspamd_http_calculate_mean (struct lat_elt *lats, double *std)
  187. {
  188. guint i, cnt, checked = 0;
  189. gdouble mean = 0., dev = 0.;
  190. cnt = store_latencies * pconns;
  191. qsort (lats, cnt, sizeof (*lats), cmpd);
  192. for (i = 0; i < cnt; i++) {
  193. if (lats[i].checked) {
  194. mean += lats[i].lat;
  195. checked ++;
  196. }
  197. }
  198. g_assert (checked > 0);
  199. mean /= checked;
  200. for (i = 0; i < cnt; i++) {
  201. if (lats[i].checked) {
  202. dev += pow ((lats[i].lat - mean), 2);
  203. }
  204. }
  205. dev /= checked;
  206. *std = sqrt (dev);
  207. return mean;
  208. }
  209. static void
  210. rspamd_http_start_workers (pid_t *sfd)
  211. {
  212. guint i;
  213. for (i = 0; i < nworkers; i++) {
  214. sfd[i] = fork ();
  215. g_assert (sfd[i] != -1);
  216. if (sfd[i] == 0) {
  217. gperf_profiler_init (NULL, "http-bench");
  218. rspamd_worker_func (&latencies[i * pconns * store_latencies],
  219. &conns_done[i]);
  220. gperf_profiler_stop ();
  221. exit (EXIT_SUCCESS);
  222. }
  223. workers_left ++;
  224. }
  225. }
  226. static void
  227. rspamd_http_stop_workers (pid_t *sfd)
  228. {
  229. guint i;
  230. gint res;
  231. for (i = 0; i < nworkers; i++) {
  232. kill (sfd[i], SIGTERM);
  233. wait (&res);
  234. }
  235. }
  236. static void
  237. rspamd_http_bench_term (int fd, short what, void *arg)
  238. {
  239. pid_t *sfd = arg;
  240. rspamd_http_stop_workers (sfd);
  241. event_loopexit (NULL);
  242. }
  243. static void
  244. rspamd_http_bench_cld (int fd, short what, void *arg)
  245. {
  246. gint res;
  247. while (waitpid (-1, &res, WNOHANG) > 0) {
  248. if (--workers_left == 0) {
  249. event_loopexit (NULL);
  250. }
  251. }
  252. }
  253. int
  254. main (int argc, char **argv)
  255. {
  256. GOptionContext *context;
  257. GError *error = NULL;
  258. pid_t *sfd;
  259. struct event_base *ev_base;
  260. rspamd_mempool_t *pool = rspamd_mempool_new (8192, "http-bench");
  261. struct event term_ev, int_ev, cld_ev;
  262. guint64 total_done;
  263. FILE *lat_file;
  264. gdouble mean, std;
  265. guint i;
  266. rspamd_init_libs ();
  267. context = g_option_context_new (
  268. "rspamd-http-bench - test server for benchmarks");
  269. g_option_context_set_summary (context,
  270. "Summary:\n Rspamd test HTTP benchmark "
  271. RVERSION
  272. "\n Release id: "
  273. RID);
  274. g_option_context_add_main_entries (context, entries, NULL);
  275. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  276. rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
  277. g_error_free (error);
  278. exit (1);
  279. }
  280. if (openssl_mode) {
  281. g_assert (rspamd_cryptobox_openssl_mode (TRUE));
  282. }
  283. rspamd_parse_inet_address (&addr, host, 0);
  284. g_assert (addr != NULL);
  285. rspamd_inet_address_set_port (addr, port);
  286. latencies = rspamd_mempool_alloc_shared (pool,
  287. nworkers * pconns * store_latencies * sizeof (*latencies));
  288. sfd = g_malloc (sizeof (*sfd) * nworkers);
  289. conns_done = rspamd_mempool_alloc_shared (pool, sizeof (guint32) * nworkers);
  290. memset (conns_done, 0, sizeof (guint32) * nworkers);
  291. rspamd_http_start_workers (sfd);
  292. ev_base = event_init ();
  293. event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd);
  294. event_base_set (ev_base, &term_ev);
  295. event_add (&term_ev, NULL);
  296. event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd);
  297. event_base_set (ev_base, &int_ev);
  298. event_add (&int_ev, NULL);
  299. event_set (&cld_ev, SIGCHLD, EV_SIGNAL|EV_PERSIST,
  300. rspamd_http_bench_cld, NULL);
  301. event_base_set (ev_base, &cld_ev);
  302. event_add (&cld_ev, NULL);
  303. event_base_loop (ev_base, 0);
  304. total_done = 0;
  305. for (i = 0; i < nworkers; i ++) {
  306. total_done += conns_done[i];
  307. }
  308. mean = rspamd_http_calculate_mean (latencies, &std);
  309. if (!csv_output) {
  310. rspamd_printf (
  311. "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n",
  312. total_done,
  313. file_size,
  314. test_time,
  315. total_done / test_time,
  316. total_done * file_size / test_time / (1024.0 * 1024.0));
  317. rspamd_printf ("Latency: %.6f ms mean, %.6f dev\n",
  318. mean * 1000.0, std * 1000.0);
  319. }
  320. else {
  321. /* size,connections,time,mean,stddev,conns,workers */
  322. rspamd_printf ("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n",
  323. file_size,
  324. total_done,
  325. test_time,
  326. mean*1000.0,
  327. std*1000.0,
  328. pconns,
  329. nworkers);
  330. }
  331. if (latencies_file) {
  332. lat_file = fopen (latencies_file, "w");
  333. if (lat_file) {
  334. for (i = 0; i < store_latencies * pconns; i ++) {
  335. if (latencies[i].checked) {
  336. rspamd_fprintf (lat_file, "%.6f\n", latencies[i].lat);
  337. }
  338. }
  339. fclose (lat_file);
  340. }
  341. }
  342. rspamd_mempool_delete (pool);
  343. return 0;
  344. }