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

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