Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rspamd_http_test.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 ev_loop *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 ev_loop *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 ev_loop *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. rspamd_http_server_func (fd, "/tmp/", addr, serv_key, c);
  193. exit (EXIT_SUCCESS);
  194. }
  195. }
  196. close (fd);
  197. }
  198. static void
  199. rspamd_http_stop_servers (pid_t *sfd)
  200. {
  201. guint i;
  202. gint res;
  203. for (i = 0; i < nservers; i++) {
  204. kill (sfd[i], SIGTERM);
  205. wait (&res);
  206. }
  207. }
  208. void
  209. rspamd_http_test_func (void)
  210. {
  211. struct ev_loop *ev_base = event_init ();
  212. rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL);
  213. struct rspamd_cryptobox_keypair *serv_key, *client_key;
  214. struct rspamd_cryptobox_pubkey *peer_key;
  215. struct rspamd_keypair_cache *c;
  216. rspamd_mempool_mutex_t *mtx;
  217. rspamd_inet_addr_t *addr;
  218. gdouble ts1, ts2;
  219. gchar filepath[PATH_MAX], *buf;
  220. gchar *env;
  221. gint fd;
  222. guint i, j;
  223. pid_t *sfd;
  224. GString *b32_key;
  225. double diff, total_diff = 0.0, *latency, mean, std;
  226. /* Read environment */
  227. if ((env = getenv ("RSPAMD_HTTP_CONNS")) != NULL) {
  228. pconns = strtoul (env, NULL, 10);
  229. }
  230. else {
  231. return;
  232. }
  233. if ((env = getenv ("RSPAMD_HTTP_TESTS")) != NULL) {
  234. ntests = strtoul (env, NULL, 10);
  235. }
  236. if ((env = getenv ("RSPAMD_HTTP_SIZE")) != NULL) {
  237. file_size = strtoul (env, NULL, 10);
  238. }
  239. if ((env = getenv ("RSPAMD_HTTP_SERVERS")) != NULL) {
  240. nservers = strtoul (env, NULL, 10);
  241. }
  242. rspamd_cryptobox_init ();
  243. rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX");
  244. g_assert ((fd = mkstemp (filepath)) != -1);
  245. sfd = g_alloca (sizeof (*sfd) * nservers);
  246. latency = g_malloc0 (pconns * ntests * sizeof (gdouble));
  247. buf = g_malloc (file_size);
  248. memset (buf, 0, file_size);
  249. g_assert (write (fd, buf, file_size) == file_size);
  250. g_free (buf);
  251. mtx = rspamd_mempool_get_mutex (pool);
  252. rspamd_parse_inet_address (&addr, "127.0.0.1", 0);
  253. rspamd_inet_address_set_port (addr, 43898);
  254. serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  255. RSPAMD_CRYPTOBOX_MODE_25519);
  256. client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  257. RSPAMD_CRYPTOBOX_MODE_25519);
  258. c = rspamd_keypair_cache_new (16);
  259. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  260. usleep (100000);
  261. /* Do client stuff */
  262. gperf_profiler_init (NULL, "plain-http-client");
  263. for (i = 0; i < ntests; i ++) {
  264. for (j = 0; j < pconns; j ++) {
  265. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  266. NULL, NULL, c, ev_base, &latency[i * pconns + j]);
  267. }
  268. ts1 = rspamd_get_ticks (FALSE);
  269. event_base_loop (ev_base, 0);
  270. ts2 = rspamd_get_ticks (FALSE);
  271. diff = (ts2 - ts1) * 1000.0;
  272. total_diff += diff;
  273. }
  274. gperf_profiler_stop ();
  275. msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps",
  276. ntests * pconns,
  277. file_size,
  278. total_diff, ntests * pconns / total_diff * 1000.);
  279. mean = rspamd_http_calculate_mean (latency, &std);
  280. msg_info ("Latency: %.6f ms mean, %.6f dev",
  281. mean, std);
  282. rspamd_http_stop_servers (sfd);
  283. rspamd_http_start_servers (sfd, addr, serv_key, c);
  284. //rspamd_mempool_lock_mutex (mtx);
  285. usleep (100000);
  286. b32_key = rspamd_keypair_print (serv_key,
  287. RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32);
  288. g_assert (b32_key != NULL);
  289. peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len,
  290. RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519);
  291. g_assert (peer_key != NULL);
  292. total_diff = 0.0;
  293. gperf_profiler_init (NULL, "cached-http-client");
  294. for (i = 0; i < ntests; i ++) {
  295. for (j = 0; j < pconns; j ++) {
  296. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  297. client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
  298. }
  299. ts1 = rspamd_get_ticks (FALSE);
  300. event_base_loop (ev_base, 0);
  301. ts2 = rspamd_get_ticks (FALSE);
  302. diff = (ts2 - ts1) * 1000.0;
  303. total_diff += diff;
  304. }
  305. gperf_profiler_stop ();
  306. msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps",
  307. ntests * pconns,
  308. file_size,
  309. total_diff, ntests * pconns / total_diff * 1000.);
  310. mean = rspamd_http_calculate_mean (latency, &std);
  311. msg_info ("Latency: %.6f ms mean, %.6f dev",
  312. mean, std);
  313. /* Restart server */
  314. rspamd_http_stop_servers (sfd);
  315. /* No keypairs cache */
  316. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  317. usleep (100000);
  318. total_diff = 0.0;
  319. gperf_profiler_init (NULL, "fair-http-client");
  320. for (i = 0; i < ntests; i ++) {
  321. for (j = 0; j < pconns; j ++) {
  322. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr,
  323. client_key, peer_key, c, ev_base, &latency[i * pconns + j]);
  324. }
  325. ts1 = rspamd_get_ticks (FALSE);
  326. event_base_loop (ev_base, 0);
  327. ts2 = rspamd_get_ticks (FALSE);
  328. diff = (ts2 - ts1) * 1000.0;
  329. total_diff += diff;
  330. }
  331. gperf_profiler_stop ();
  332. msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps",
  333. ntests * pconns,
  334. file_size,
  335. total_diff, ntests * pconns / total_diff * 1000.);
  336. mean = rspamd_http_calculate_mean (latency, &std);
  337. msg_info ("Latency: %.6f ms mean, %.6f dev",
  338. mean, std);
  339. /* AES mode */
  340. serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  341. RSPAMD_CRYPTOBOX_MODE_NIST);
  342. client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
  343. RSPAMD_CRYPTOBOX_MODE_NIST);
  344. c = rspamd_keypair_cache_new (16);
  345. /* Restart server */
  346. rspamd_http_stop_servers (sfd);
  347. /* No keypairs cache */
  348. rspamd_http_start_servers (sfd, addr, serv_key, c);
  349. //rspamd_mempool_lock_mutex (mtx);
  350. usleep (100000);
  351. b32_key = rspamd_keypair_print (serv_key,
  352. RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32);
  353. g_assert (b32_key != NULL);
  354. peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len,
  355. RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST);
  356. g_assert (peer_key != NULL);
  357. total_diff = 0.0;
  358. gperf_profiler_init (NULL, "cached-http-client-aes");
  359. for (i = 0; i < ntests; i++) {
  360. for (j = 0; j < pconns; j++) {
  361. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1,
  362. addr,
  363. client_key,
  364. peer_key,
  365. NULL,
  366. ev_base,
  367. &latency[i * pconns + j]);
  368. }
  369. ts1 = rspamd_get_ticks (FALSE);
  370. event_base_loop (ev_base, 0);
  371. ts2 = rspamd_get_ticks (FALSE);
  372. diff = (ts2 - ts1) * 1000.0;
  373. total_diff += diff;
  374. }
  375. gperf_profiler_stop ();
  376. msg_info (
  377. "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps",
  378. ntests * pconns,
  379. file_size,
  380. total_diff,
  381. ntests * pconns / total_diff * 1000.);
  382. mean = rspamd_http_calculate_mean (latency, &std);
  383. msg_info ("Latency: %.6f ms mean, %.6f dev",
  384. mean, std);
  385. /* Restart server */
  386. rspamd_http_stop_servers (sfd);
  387. /* No keypairs cache */
  388. rspamd_http_start_servers (sfd, addr, serv_key, NULL);
  389. //rspamd_mempool_lock_mutex (mtx);
  390. usleep (100000);
  391. total_diff = 0.0;
  392. gperf_profiler_init (NULL, "fair-http-client-aes");
  393. for (i = 0; i < ntests; i++) {
  394. for (j = 0; j < pconns; j++) {
  395. rspamd_http_client_func (filepath + sizeof ("/tmp") - 1,
  396. addr,
  397. client_key,
  398. peer_key,
  399. c,
  400. ev_base,
  401. &latency[i * pconns + j]);
  402. }
  403. ts1 = rspamd_get_ticks (FALSE);
  404. event_base_loop (ev_base, 0);
  405. ts2 = rspamd_get_ticks (FALSE);
  406. diff = (ts2 - ts1) * 1000.0;
  407. total_diff += diff;
  408. }
  409. gperf_profiler_stop ();
  410. msg_info (
  411. "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps",
  412. ntests * pconns,
  413. file_size,
  414. total_diff,
  415. ntests * pconns / total_diff * 1000.);
  416. mean = rspamd_http_calculate_mean (latency, &std);
  417. msg_info ("Latency: %.6f ms mean, %.6f dev",
  418. mean, std);
  419. close (fd);
  420. unlink (filepath);
  421. rspamd_http_stop_servers (sfd);
  422. }