選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

rspamd_http_bench.c 10KB

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. struct rspamd_cryptobox_pubkey *peer_key,
  119. struct rspamd_cryptobox_keypair* client_key,
  120. struct rspamd_keypair_cache *c)
  121. {
  122. struct rspamd_http_message *msg;
  123. struct rspamd_http_connection *conn;
  124. gchar urlbuf[PATH_MAX];
  125. struct client_cbdata *cb;
  126. gint fd, flags;
  127. fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE);
  128. g_assert (fd != -1);
  129. flags = 1;
  130. (void)setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof (flags));
  131. conn = rspamd_http_connection_new (rspamd_client_body, rspamd_client_err,
  132. rspamd_client_finish, RSPAMD_HTTP_CLIENT_SIMPLE,
  133. RSPAMD_HTTP_CLIENT, c);
  134. rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://%s/%d", host, file_size);
  135. msg = rspamd_http_message_from_url (urlbuf);
  136. g_assert (conn != NULL && msg != NULL);
  137. if (peer_key != NULL) {
  138. g_assert (client_key != NULL);
  139. rspamd_http_connection_set_key (conn, client_key);
  140. msg->peer_key = rspamd_pubkey_ref (peer_key);
  141. }
  142. cb = g_malloc (sizeof (*cb));
  143. cb->ts = rspamd_get_ticks ();
  144. cb->lat = latency;
  145. cb->ev_base = ev_base;
  146. cb->wconns = wconns;
  147. latency->checked = FALSE;
  148. rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb,
  149. fd, NULL, ev_base);
  150. }
  151. static void
  152. rspamd_worker_func (struct lat_elt *plat, guint32 *wconns)
  153. {
  154. guint i, j;
  155. struct event_base *ev_base;
  156. struct itimerval itv;
  157. struct rspamd_keypair_cache *c = NULL;
  158. struct rspamd_cryptobox_keypair *client_key = NULL;
  159. struct rspamd_cryptobox_pubkey *peer_key = NULL;
  160. if (server_key) {
  161. peer_key = rspamd_pubkey_from_base32 (server_key, 0, RSPAMD_KEYPAIR_KEX,
  162. openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
  163. g_assert (peer_key != NULL);
  164. client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  165. openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
  166. if (cache_size > 0) {
  167. c = rspamd_keypair_cache_new (cache_size);
  168. }
  169. }
  170. memset (&itv, 0, sizeof (itv));
  171. double_to_tv (test_time, &itv.it_value);
  172. ev_base = event_init ();
  173. g_assert (setitimer (ITIMER_REAL, &itv, NULL) != -1);
  174. for (i = 0; ; i = (i + 1) % store_latencies) {
  175. for (j = 0; j < pconns; j++) {
  176. rspamd_http_client_func (ev_base, &plat[i * pconns + j],
  177. wconns, peer_key, client_key, c);
  178. }
  179. conns_pending = pconns;
  180. event_base_loop (ev_base, 0);
  181. }
  182. }
  183. static int
  184. cmpd (const void *p1, const void *p2)
  185. {
  186. const struct lat_elt *d1 = p1, *d2 = p2;
  187. return (d1->lat) - (d2->lat);
  188. }
  189. double
  190. rspamd_http_calculate_mean (struct lat_elt *lats, double *std)
  191. {
  192. guint i, cnt, checked = 0;
  193. gdouble mean = 0., dev = 0.;
  194. cnt = store_latencies * pconns;
  195. qsort (lats, cnt, sizeof (*lats), cmpd);
  196. for (i = 0; i < cnt; i++) {
  197. if (lats[i].checked) {
  198. mean += lats[i].lat;
  199. checked ++;
  200. }
  201. }
  202. g_assert (checked > 0);
  203. mean /= checked;
  204. for (i = 0; i < cnt; i++) {
  205. if (lats[i].checked) {
  206. dev += pow ((lats[i].lat - mean), 2);
  207. }
  208. }
  209. dev /= checked;
  210. *std = sqrt (dev);
  211. return mean;
  212. }
  213. static void
  214. rspamd_http_start_workers (pid_t *sfd)
  215. {
  216. guint i;
  217. for (i = 0; i < nworkers; i++) {
  218. sfd[i] = fork ();
  219. g_assert (sfd[i] != -1);
  220. if (sfd[i] == 0) {
  221. gperf_profiler_init (NULL, "http-bench");
  222. rspamd_worker_func (&latencies[i * pconns * store_latencies],
  223. &conns_done[i]);
  224. gperf_profiler_stop ();
  225. exit (EXIT_SUCCESS);
  226. }
  227. workers_left ++;
  228. }
  229. }
  230. static void
  231. rspamd_http_stop_workers (pid_t *sfd)
  232. {
  233. guint i;
  234. gint res;
  235. for (i = 0; i < nworkers; i++) {
  236. kill (sfd[i], SIGTERM);
  237. wait (&res);
  238. }
  239. }
  240. static void
  241. rspamd_http_bench_term (int fd, short what, void *arg)
  242. {
  243. pid_t *sfd = arg;
  244. rspamd_http_stop_workers (sfd);
  245. event_loopexit (NULL);
  246. }
  247. static void
  248. rspamd_http_bench_cld (int fd, short what, void *arg)
  249. {
  250. gint res;
  251. while (waitpid (-1, &res, WNOHANG) > 0) {
  252. if (--workers_left == 0) {
  253. event_loopexit (NULL);
  254. }
  255. }
  256. }
  257. int
  258. main (int argc, char **argv)
  259. {
  260. GOptionContext *context;
  261. GError *error = NULL;
  262. pid_t *sfd;
  263. struct event_base *ev_base;
  264. rspamd_mempool_t *pool = rspamd_mempool_new (8192, "http-bench");
  265. struct event term_ev, int_ev, cld_ev;
  266. guint64 total_done;
  267. FILE *lat_file;
  268. gdouble mean, std;
  269. guint i;
  270. rspamd_init_libs ();
  271. context = g_option_context_new (
  272. "rspamd-http-bench - test server for benchmarks");
  273. g_option_context_set_summary (context,
  274. "Summary:\n Rspamd test HTTP benchmark "
  275. RVERSION
  276. "\n Release id: "
  277. RID);
  278. g_option_context_add_main_entries (context, entries, NULL);
  279. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  280. rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message);
  281. g_error_free (error);
  282. exit (1);
  283. }
  284. rspamd_parse_inet_address (&addr, host, 0);
  285. g_assert (addr != NULL);
  286. rspamd_inet_address_set_port (addr, port);
  287. latencies = rspamd_mempool_alloc_shared (pool,
  288. nworkers * pconns * store_latencies * sizeof (*latencies));
  289. sfd = g_malloc (sizeof (*sfd) * nworkers);
  290. conns_done = rspamd_mempool_alloc_shared (pool, sizeof (guint32) * nworkers);
  291. memset (conns_done, 0, sizeof (guint32) * nworkers);
  292. rspamd_http_start_workers (sfd);
  293. ev_base = event_init ();
  294. event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd);
  295. event_base_set (ev_base, &term_ev);
  296. event_add (&term_ev, NULL);
  297. event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd);
  298. event_base_set (ev_base, &int_ev);
  299. event_add (&int_ev, NULL);
  300. event_set (&cld_ev, SIGCHLD, EV_SIGNAL|EV_PERSIST,
  301. rspamd_http_bench_cld, NULL);
  302. event_base_set (ev_base, &cld_ev);
  303. event_add (&cld_ev, NULL);
  304. event_base_loop (ev_base, 0);
  305. total_done = 0;
  306. for (i = 0; i < nworkers; i ++) {
  307. total_done += conns_done[i];
  308. }
  309. mean = rspamd_http_calculate_mean (latencies, &std);
  310. if (!csv_output) {
  311. rspamd_printf (
  312. "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n",
  313. total_done,
  314. file_size,
  315. test_time,
  316. total_done / test_time,
  317. total_done * file_size / test_time / (1024.0 * 1024.0));
  318. rspamd_printf ("Latency: %.6f ms mean, %.6f dev\n",
  319. mean * 1000.0, std * 1000.0);
  320. }
  321. else {
  322. /* size,connections,time,mean,stddev,conns,workers */
  323. rspamd_printf ("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n",
  324. file_size,
  325. total_done,
  326. test_time,
  327. mean*1000.0,
  328. std*1000.0,
  329. pconns,
  330. nworkers);
  331. }
  332. if (latencies_file) {
  333. lat_file = fopen (latencies_file, "w");
  334. if (lat_file) {
  335. for (i = 0; i < store_latencies * pconns; i ++) {
  336. if (latencies[i].checked) {
  337. rspamd_fprintf (lat_file, "%.6f\n", latencies[i].lat);
  338. }
  339. }
  340. fclose (lat_file);
  341. }
  342. }
  343. rspamd_mempool_delete (pool);
  344. return 0;
  345. }