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_test.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Copyright (c) 2015, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "config.h"
  24. #include "rspamd.h"
  25. #include "util.h"
  26. #include "http.h"
  27. #include "tests.h"
  28. #include "ottery.h"
  29. #include "cryptobox.h"
  30. #include "unix-std.h"
  31. #include <math.h>
  32. #ifdef HAVE_SYS_WAIT_H
  33. #include <sys/wait.h>
  34. #endif
  35. static guint file_size = 500;
  36. static guint pconns = 100;
  37. static guint ntests = 3000;
  38. static guint nservers = 1;
  39. static void
  40. rspamd_server_error (struct rspamd_http_connection_entry *conn_ent,
  41. GError *err)
  42. {
  43. msg_err ("http error occurred: %s", err->message);
  44. g_assert (0);
  45. }
  46. static void
  47. rspamd_server_finish (struct rspamd_http_connection_entry *conn_ent)
  48. {
  49. /* Do nothing here */
  50. }
  51. static void
  52. rspamd_server_accept (gint fd, short what, void *arg)
  53. {
  54. struct rspamd_http_connection_router *rt = arg;
  55. rspamd_inet_addr_t *addr;
  56. gint nfd;
  57. if ((nfd =
  58. rspamd_accept_from_socket (fd, &addr)) == -1) {
  59. msg_warn ("accept failed: %s", strerror (errno));
  60. return;
  61. }
  62. /* Check for EAGAIN */
  63. if (nfd == 0) {
  64. return;
  65. }
  66. rspamd_inet_address_destroy (addr);
  67. rspamd_http_router_handle_socket (rt, nfd, NULL);
  68. }
  69. static void
  70. rspamd_http_term_handler (gint fd, short what, void *arg)
  71. {
  72. struct event_base *ev_base = arg;
  73. struct timeval tv = {0, 0};
  74. event_base_loopexit (ev_base, &tv);
  75. }
  76. static void
  77. rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr,
  78. gpointer kp, struct rspamd_keypair_cache *c)
  79. {
  80. struct rspamd_http_connection_router *rt;
  81. struct event_base *ev_base = event_init ();
  82. struct event accept_ev, term_ev;
  83. rt = rspamd_http_router_new (rspamd_server_error, rspamd_server_finish,
  84. NULL, ev_base, path, c);
  85. g_assert (rt != NULL);
  86. rspamd_http_router_set_key (rt, kp);
  87. event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt);
  88. event_base_set (ev_base, &accept_ev);
  89. event_add (&accept_ev, NULL);
  90. evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
  91. event_base_set (ev_base, &term_ev);
  92. event_add (&term_ev, NULL);
  93. event_base_loop (ev_base, 0);
  94. }
  95. static gint
  96. rspamd_client_body (struct rspamd_http_connection *conn,
  97. struct rspamd_http_message *msg,
  98. const gchar *chunk, gsize len)
  99. {
  100. g_assert (chunk[0] == '\0');
  101. return 0;
  102. }
  103. struct client_cbdata {
  104. double *lat;
  105. gdouble ts;
  106. };
  107. static void
  108. rspamd_client_err (struct rspamd_http_connection *conn, GError *err)
  109. {
  110. msg_info ("abnormally closing connection from: error: %s",
  111. err->message);
  112. g_assert (0);
  113. close (conn->fd);
  114. rspamd_http_connection_unref (conn);
  115. }
  116. static gint
  117. rspamd_client_finish (struct rspamd_http_connection *conn,
  118. struct rspamd_http_message *msg)
  119. {
  120. struct client_cbdata *cb = conn->ud;
  121. *(cb->lat) = rspamd_get_ticks () * 1000. - cb->ts;
  122. close (conn->fd);
  123. rspamd_http_connection_unref (conn);
  124. g_free (cb);
  125. return 0;
  126. }
  127. static void
  128. rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr,
  129. gpointer kp, gpointer peer_kp, struct rspamd_keypair_cache *c,
  130. struct event_base *ev_base, double *latency)
  131. {
  132. struct rspamd_http_message *msg;
  133. struct rspamd_http_connection *conn;
  134. gchar urlbuf[PATH_MAX];
  135. struct client_cbdata *cb;
  136. gint fd;
  137. g_assert ((fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE)) != -1);
  138. conn = rspamd_http_connection_new (rspamd_client_body, rspamd_client_err,
  139. rspamd_client_finish, RSPAMD_HTTP_CLIENT_SIMPLE,
  140. RSPAMD_HTTP_CLIENT, c);
  141. rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://127.0.0.1/%s", path);
  142. msg = rspamd_http_message_from_url (urlbuf);
  143. g_assert (conn != NULL && msg != NULL);
  144. if (kp != NULL) {
  145. g_assert (peer_kp != NULL);
  146. rspamd_http_connection_set_key (conn, kp);
  147. msg->peer_key = rspamd_http_connection_key_ref (peer_kp);
  148. }
  149. cb = g_malloc (sizeof (*cb));
  150. cb->ts = rspamd_get_ticks () * 1000.;
  151. cb->lat = latency;
  152. rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb,
  153. fd, NULL, ev_base);
  154. }
  155. static int
  156. cmpd (const void *p1, const void *p2)
  157. {
  158. const double *d1 = p1, *d2 = p2;
  159. return (*d1) - (*d2);
  160. }
  161. double
  162. rspamd_http_calculate_mean (double *lats, double *std)
  163. {
  164. guint i;
  165. gdouble mean = 0., dev = 0.;
  166. qsort (lats, ntests * pconns, sizeof (double), cmpd);
  167. for (i = 0; i < ntests * pconns; i ++) {
  168. mean += lats[i];
  169. }
  170. mean /= ntests * pconns;
  171. for (i = 0; i < ntests * pconns; i ++) {
  172. dev += (lats[i] - mean) * (lats[i] - mean);
  173. }
  174. dev /= ntests * pconns;
  175. *std = sqrt (dev);
  176. return mean;
  177. }
  178. static void
  179. rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr,
  180. gpointer serv_key, struct rspamd_keypair_cache *c)
  181. {
  182. guint i;
  183. gint fd;
  184. g_assert ((fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1);
  185. for (i = 0; i < nservers; i ++) {
  186. sfd[i] = fork ();
  187. g_assert (sfd[i] != -1);
  188. if (sfd[i] == 0) {
  189. gperf_profiler_init (NULL, "plain-http-server");
  190. rspamd_http_server_func (fd, "/tmp/", addr, serv_key, c);
  191. gperf_profiler_stop ();
  192. exit (EXIT_SUCCESS);
  193. }
  194. }
  195. close (fd);
  196. }
  197. static void
  198. rspamd_http_stop_servers (pid_t *sfd)
  199. {
  200. guint i;
  201. gint res;
  202. for (i = 0; i < nservers; i++) {
  203. kill (sfd[i], SIGTERM);
  204. wait (&res);
  205. }
  206. }
  207. void
  208. rspamd_http_test_func (void)
  209. {
  210. struct event_base *ev_base = event_init ();
  211. rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL);
  212. gpointer serv_key, client_key, peer_key;
  213. struct rspamd_keypair_cache *c;
  214. rspamd_mempool_mutex_t *mtx;
  215. rspamd_inet_addr_t *addr;
  216. gdouble ts1, ts2;
  217. gchar filepath[PATH_MAX], *buf;
  218. gchar *env;
  219. gint fd;
  220. guint i, j;
  221. pid_t *sfd;
  222. GString *b32_key;
  223. double diff, total_diff = 0.0, *latency, mean, std;
  224. rspamd_cryptobox_init ();
  225. rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX");
  226. g_assert ((fd = mkstemp (filepath)) != -1);
  227. /* Read environment */
  228. if ((env = getenv ("RSPAMD_HTTP_CONNS")) != NULL) {
  229. pconns = strtoul (env, NULL, 10);
  230. }
  231. if ((env = getenv ("RSPAMD_HTTP_TESTS")) != NULL) {
  232. ntests = strtoul (env, NULL, 10);
  233. }
  234. if ((env = getenv ("RSPAMD_HTTP_SIZE")) != NULL) {
  235. file_size = strtoul (env, NULL, 10);
  236. }
  237. if ((env = getenv ("RSPAMD_HTTP_SERVERS")) != NULL) {
  238. nservers = strtoul (env, NULL, 10);
  239. }
  240. sfd = g_alloca (sizeof (*sfd) * nservers);
  241. latency = g_malloc0 (pconns * ntests * sizeof (gdouble));
  242. buf = g_malloc (file_size);
  243. memset (buf, 0, file_size);
  244. g_assert (write (fd, buf, file_size) == file_size);
  245. g_free (buf);
  246. mtx = rspamd_mempool_get_mutex (pool);
  247. rspamd_parse_inet_address (&addr, "127.0.0.1", 0);
  248. rspamd_inet_address_set_port (addr, 43898);
  249. serv_key = rspamd_http_connection_gen_key ();
  250. client_key = rspamd_http_connection_gen_key ();
  251. c = rspamd_keypair_cache_new (16);
  252. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  253. usleep (100000);
  254. /* Do client stuff */
  255. gperf_profiler_init (NULL, "plain-http-client");
  256. for (i = 0; i < ntests; i ++) {
  257. for (j = 0; j < pconns; j ++) {
  258. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  259. NULL, NULL, c, ev_base, &latency[i * pconns + j]);
  260. }
  261. ts1 = rspamd_get_ticks ();
  262. event_base_loop (ev_base, 0);
  263. ts2 = rspamd_get_ticks ();
  264. diff = (ts2 - ts1) * 1000.0;
  265. total_diff += diff;
  266. }
  267. gperf_profiler_stop ();
  268. msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps",
  269. ntests * pconns,
  270. file_size,
  271. total_diff, ntests * pconns / total_diff * 1000.);
  272. mean = rspamd_http_calculate_mean (latency, &std);
  273. msg_info ("Latency: %.6f ms mean, %.6f dev",
  274. mean, std);
  275. rspamd_http_stop_servers (sfd);
  276. rspamd_http_start_servers (sfd, addr, serv_key, c);
  277. //rspamd_mempool_lock_mutex (mtx);
  278. usleep (100000);
  279. b32_key = rspamd_http_connection_print_key (serv_key,
  280. RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32);
  281. g_assert (b32_key != NULL);
  282. peer_key = rspamd_http_connection_make_peer_key (b32_key->str);
  283. g_assert (peer_key != NULL);
  284. total_diff = 0.0;
  285. gperf_profiler_init (NULL, "cached-http-client");
  286. for (i = 0; i < ntests; i ++) {
  287. for (j = 0; j < pconns; j ++) {
  288. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  289. client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
  290. }
  291. ts1 = rspamd_get_ticks ();
  292. event_base_loop (ev_base, 0);
  293. ts2 = rspamd_get_ticks ();
  294. diff = (ts2 - ts1) * 1000.0;
  295. total_diff += diff;
  296. }
  297. gperf_profiler_stop ();
  298. msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps",
  299. ntests * pconns,
  300. file_size,
  301. total_diff, ntests * pconns / total_diff * 1000.);
  302. mean = rspamd_http_calculate_mean (latency, &std);
  303. msg_info ("Latency: %.6f ms mean, %.6f dev",
  304. mean, std);
  305. /* Restart server */
  306. rspamd_http_stop_servers (sfd);
  307. /* No keypairs cache */
  308. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  309. usleep (100000);
  310. total_diff = 0.0;
  311. gperf_profiler_init (NULL, "fair-http-client");
  312. for (i = 0; i < ntests; i ++) {
  313. for (j = 0; j < pconns; j ++) {
  314. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  315. client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
  316. }
  317. ts1 = rspamd_get_ticks ();
  318. event_base_loop (ev_base, 0);
  319. ts2 = rspamd_get_ticks ();
  320. diff = (ts2 - ts1) * 1000.0;
  321. total_diff += diff;
  322. }
  323. gperf_profiler_stop ();
  324. msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps",
  325. ntests * pconns,
  326. file_size,
  327. total_diff, ntests * pconns / total_diff * 1000.);
  328. mean = rspamd_http_calculate_mean (latency, &std);
  329. msg_info ("Latency: %.6f ms mean, %.6f dev",
  330. mean, std);
  331. /* AES mode */
  332. if (rspamd_cryptobox_openssl_mode (TRUE)) {
  333. serv_key = rspamd_http_connection_gen_key ();
  334. client_key = rspamd_http_connection_gen_key ();
  335. c = rspamd_keypair_cache_new (16);
  336. /* Restart server */
  337. rspamd_http_stop_servers (sfd);
  338. /* No keypairs cache */
  339. rspamd_http_start_servers (sfd, addr, serv_key, c);
  340. //rspamd_mempool_lock_mutex (mtx);
  341. usleep (100000);
  342. b32_key = rspamd_http_connection_print_key (serv_key,
  343. RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
  344. g_assert (b32_key != NULL);
  345. peer_key = rspamd_http_connection_make_peer_key (b32_key->str);
  346. g_assert (peer_key != NULL);
  347. total_diff = 0.0;
  348. gperf_profiler_init (NULL, "cached-http-client-aes");
  349. for (i = 0; i < ntests; i++) {
  350. for (j = 0; j < pconns; j++) {
  351. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1,
  352. addr,
  353. client_key,
  354. peer_key,
  355. NULL,
  356. ev_base,
  357. &latency[i * pconns + j]);
  358. }
  359. ts1 = rspamd_get_ticks ();
  360. event_base_loop (ev_base, 0);
  361. ts2 = rspamd_get_ticks ();
  362. diff = (ts2 - ts1) * 1000.0;
  363. total_diff += diff;
  364. }
  365. gperf_profiler_stop ();
  366. msg_info (
  367. "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps",
  368. ntests * pconns,
  369. file_size,
  370. total_diff,
  371. ntests * pconns / total_diff * 1000.);
  372. mean = rspamd_http_calculate_mean (latency, &std);
  373. msg_info ("Latency: %.6f ms mean, %.6f dev",
  374. mean, std);
  375. /* Restart server */
  376. rspamd_http_stop_servers (sfd);
  377. /* No keypairs cache */
  378. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  379. //rspamd_mempool_lock_mutex (mtx);
  380. usleep (100000);
  381. total_diff = 0.0;
  382. gperf_profiler_init (NULL, "fair-http-client-aes");
  383. for (i = 0; i < ntests; i++) {
  384. for (j = 0; j < pconns; j++) {
  385. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1,
  386. addr,
  387. client_key,
  388. peer_key,
  389. c,
  390. ev_base,
  391. &latency[i * pconns + j]);
  392. }
  393. ts1 = rspamd_get_ticks ();
  394. event_base_loop (ev_base, 0);
  395. ts2 = rspamd_get_ticks ();
  396. diff = (ts2 - ts1) * 1000.0;
  397. total_diff += diff;
  398. }
  399. gperf_profiler_stop ();
  400. msg_info (
  401. "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps",
  402. ntests * pconns,
  403. file_size,
  404. total_diff,
  405. ntests * pconns / total_diff * 1000.);
  406. mean = rspamd_http_calculate_mean (latency, &std);
  407. msg_info ("Latency: %.6f ms mean, %.6f dev",
  408. mean, std);
  409. }
  410. close (fd);
  411. unlink (filepath);
  412. rspamd_http_stop_servers (sfd);
  413. }