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_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 = NULL;
  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. rspamd_inet_address_free (addr);
  61. return;
  62. }
  63. rspamd_inet_address_free (addr);
  64. rspamd_http_router_handle_socket (rt, nfd, NULL);
  65. }
  66. static void
  67. rspamd_http_term_handler (gint fd, short what, void *arg)
  68. {
  69. struct ev_loop *ev_base = arg;
  70. struct timeval tv = {0, 0};
  71. event_base_loopexit (ev_base, &tv);
  72. }
  73. static void
  74. rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr,
  75. struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c)
  76. {
  77. struct rspamd_http_connection_router *rt;
  78. struct ev_loop *ev_base = event_init ();
  79. struct event accept_ev, term_ev;
  80. rt = rspamd_http_router_new (rspamd_server_error, rspamd_server_finish,
  81. NULL, ev_base, path, c);
  82. g_assert (rt != NULL);
  83. rspamd_http_router_set_key (rt, kp);
  84. event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt);
  85. event_base_set (ev_base, &accept_ev);
  86. event_add (&accept_ev, NULL);
  87. evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
  88. event_base_set (ev_base, &term_ev);
  89. event_add (&term_ev, NULL);
  90. event_base_loop (ev_base, 0);
  91. }
  92. static gint
  93. rspamd_client_body (struct rspamd_http_connection *conn,
  94. struct rspamd_http_message *msg,
  95. const gchar *chunk, gsize len)
  96. {
  97. g_assert (chunk[0] == '\0');
  98. return 0;
  99. }
  100. struct client_cbdata {
  101. double *lat;
  102. gdouble ts;
  103. };
  104. static void
  105. rspamd_client_err (struct rspamd_http_connection *conn, GError *err)
  106. {
  107. msg_info ("abnormally closing connection from: error: %s",
  108. err->message);
  109. g_assert (0);
  110. close (conn->fd);
  111. rspamd_http_connection_unref (conn);
  112. }
  113. static gint
  114. rspamd_client_finish (struct rspamd_http_connection *conn,
  115. struct rspamd_http_message *msg)
  116. {
  117. struct client_cbdata *cb = conn->ud;
  118. *(cb->lat) = rspamd_get_ticks (FALSE) * 1000. - cb->ts;
  119. close (conn->fd);
  120. rspamd_http_connection_unref (conn);
  121. g_free (cb);
  122. return 0;
  123. }
  124. static void
  125. rspamd_http_client_func (const gchar *path, rspamd_inet_addr_t *addr,
  126. struct rspamd_cryptobox_keypair *kp,
  127. struct rspamd_cryptobox_pubkey *peer_kp,
  128. struct rspamd_keypair_cache *c,
  129. struct ev_loop *ev_base, double *latency)
  130. {
  131. struct rspamd_http_message *msg;
  132. struct rspamd_http_connection *conn;
  133. gchar urlbuf[PATH_MAX];
  134. struct client_cbdata *cb;
  135. gint fd;
  136. g_assert ((fd = rspamd_inet_address_connect (addr, SOCK_STREAM, TRUE)) != -1);
  137. conn = rspamd_http_connection_new (rspamd_client_body,
  138. rspamd_client_err,
  139. rspamd_client_finish,
  140. RSPAMD_HTTP_CLIENT_SIMPLE,
  141. RSPAMD_HTTP_CLIENT,
  142. c,
  143. NULL);
  144. rspamd_snprintf (urlbuf, sizeof (urlbuf), "http://127.0.0.1/%s", path);
  145. msg = rspamd_http_message_from_url (urlbuf);
  146. g_assert (conn != NULL && msg != NULL);
  147. if (kp != NULL) {
  148. g_assert (peer_kp != NULL);
  149. rspamd_http_connection_set_key (conn, kp);
  150. msg->peer_key = rspamd_pubkey_ref (peer_kp);
  151. }
  152. cb = g_malloc (sizeof (*cb));
  153. cb->ts = rspamd_get_ticks (FALSE) * 1000.;
  154. cb->lat = latency;
  155. rspamd_http_connection_write_message (conn, msg, NULL, NULL, cb,
  156. fd, NULL, ev_base);
  157. }
  158. static int
  159. cmpd (const void *p1, const void *p2)
  160. {
  161. const double *d1 = p1, *d2 = p2;
  162. return (*d1) - (*d2);
  163. }
  164. double
  165. rspamd_http_calculate_mean (double *lats, double *std)
  166. {
  167. guint i;
  168. gdouble mean = 0., dev = 0.;
  169. qsort (lats, ntests * pconns, sizeof (double), cmpd);
  170. for (i = 0; i < ntests * pconns; i ++) {
  171. mean += lats[i];
  172. }
  173. mean /= ntests * pconns;
  174. for (i = 0; i < ntests * pconns; i ++) {
  175. dev += (lats[i] - mean) * (lats[i] - mean);
  176. }
  177. dev /= ntests * pconns;
  178. *std = sqrt (dev);
  179. return mean;
  180. }
  181. static void
  182. rspamd_http_start_servers (pid_t *sfd, rspamd_inet_addr_t *addr,
  183. struct rspamd_cryptobox_keypair *serv_key,
  184. struct rspamd_keypair_cache *c)
  185. {
  186. guint i;
  187. gint fd;
  188. g_assert ((fd = rspamd_inet_address_listen (addr, SOCK_STREAM, TRUE)) != -1);
  189. for (i = 0; i < nservers; i ++) {
  190. sfd[i] = fork ();
  191. g_assert (sfd[i] != -1);
  192. if (sfd[i] == 0) {
  193. rspamd_http_server_func (fd, "/tmp/", addr, serv_key, c);
  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 ev_loop *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. }