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

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