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

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