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

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