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 11KB

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