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_bench.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 "ottery.h"
  22. #include "cryptobox.h"
  23. #include "unix-std.h"
  24. #include <math.h>
  25. #include <netinet/tcp.h>
  26. #ifdef HAVE_SYS_WAIT_H
  27. #include <sys/wait.h>
  28. #endif
  29. static unsigned int port = 43000;
  30. static char *host = "127.0.0.1";
  31. static char *server_key = NULL;
  32. static unsigned int cache_size = 10;
  33. static unsigned int nworkers = 1;
  34. static gboolean openssl_mode = FALSE;
  35. static unsigned int file_size = 500;
  36. static unsigned int pconns = 100;
  37. static double test_time = 10.0;
  38. static char *latencies_file = NULL;
  39. static gboolean csv_output = FALSE;
  40. /* Dynamic vars */
  41. static rspamd_inet_addr_t *addr;
  42. static uint32_t workers_left = 0;
  43. static uint32_t *conns_done = NULL;
  44. static const unsigned int store_latencies = 1000;
  45. static uint32_t conns_pending = 0;
  46. static GOptionEntry entries[] = {
  47. {"port", 'p', 0, G_OPTION_ARG_INT, &port,
  48. "Port number (default: 43000)", NULL},
  49. {"cache", 'c', 0, G_OPTION_ARG_INT, &cache_size,
  50. "Keys cache size (default: 10)", NULL},
  51. {"workers", 'n', 0, G_OPTION_ARG_INT, &nworkers,
  52. "Number of workers to start (default: 1)", NULL},
  53. {"size", 's', 0, G_OPTION_ARG_INT, &file_size,
  54. "Size of payload to transfer (default: 500)", NULL},
  55. {"conns", 'C', 0, G_OPTION_ARG_INT, &pconns,
  56. "Number of parallel connections (default: 100)", NULL},
  57. {"time", 't', 0, G_OPTION_ARG_DOUBLE, &test_time,
  58. "Time to run tests (default: 10.0 sec)", NULL},
  59. {"openssl", 'o', 0, G_OPTION_ARG_NONE, &openssl_mode,
  60. "Use openssl crypto", NULL},
  61. {"host", 'h', 0, G_OPTION_ARG_STRING, &host,
  62. "Connect to the specified host (default: localhost)", NULL},
  63. {"key", 'k', 0, G_OPTION_ARG_STRING, &server_key,
  64. "Use the specified key (base32 encoded)", NULL},
  65. {"latency", 'l', 0, G_OPTION_ARG_FILENAME, &latencies_file,
  66. "Write latencies to the specified file", NULL},
  67. {"csv", 0, 0, G_OPTION_ARG_NONE, &csv_output,
  68. "Output CSV", NULL},
  69. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
  70. struct lat_elt {
  71. double lat;
  72. unsigned char checked;
  73. };
  74. static struct lat_elt *latencies;
  75. static int
  76. rspamd_client_body(struct rspamd_http_connection *conn,
  77. struct rspamd_http_message *msg,
  78. const char *chunk, gsize len)
  79. {
  80. g_assert(chunk[0] == '\0');
  81. return 0;
  82. }
  83. struct client_cbdata {
  84. struct lat_elt *lat;
  85. uint32_t *wconns;
  86. double ts;
  87. struct ev_loop *ev_base;
  88. };
  89. static void
  90. rspamd_client_err(struct rspamd_http_connection *conn, GError *err)
  91. {
  92. msg_info("abnormally closing connection from: error: %s",
  93. err->message);
  94. g_assert(0);
  95. close(conn->fd);
  96. rspamd_http_connection_unref(conn);
  97. }
  98. static int
  99. rspamd_client_finish(struct rspamd_http_connection *conn,
  100. struct rspamd_http_message *msg)
  101. {
  102. struct client_cbdata *cb = conn->ud;
  103. cb->lat->lat = rspamd_get_ticks() - cb->ts;
  104. cb->lat->checked = TRUE;
  105. (*cb->wconns)++;
  106. conns_pending--;
  107. close(conn->fd);
  108. rspamd_http_connection_unref(conn);
  109. g_free(cb);
  110. if (conns_pending == 0) {
  111. event_base_loopexit(cb->ev_base, NULL);
  112. }
  113. return 0;
  114. }
  115. static void
  116. rspamd_http_client_func(struct ev_loop *ev_base, struct lat_elt *latency,
  117. uint32_t *wconns,
  118. struct rspamd_cryptobox_pubkey *peer_key,
  119. struct rspamd_cryptobox_keypair *client_key,
  120. struct rspamd_keypair_cache *c)
  121. {
  122. struct rspamd_http_message *msg;
  123. struct rspamd_http_connection *conn;
  124. char urlbuf[PATH_MAX];
  125. struct client_cbdata *cb;
  126. int fd, flags;
  127. fd = rspamd_inet_address_connect(addr, SOCK_STREAM, TRUE);
  128. g_assert(fd != -1);
  129. flags = 1;
  130. (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
  131. conn = rspamd_http_connection_new(rspamd_client_body,
  132. rspamd_client_err,
  133. rspamd_client_finish,
  134. RSPAMD_HTTP_CLIENT_SIMPLE,
  135. RSPAMD_HTTP_CLIENT,
  136. c,
  137. NULL);
  138. rspamd_snprintf(urlbuf, sizeof(urlbuf), "http://%s/%d", host, file_size);
  139. msg = rspamd_http_message_from_url(urlbuf);
  140. g_assert(conn != NULL && msg != NULL);
  141. if (peer_key != NULL) {
  142. g_assert(client_key != NULL);
  143. rspamd_http_connection_set_key(conn, client_key);
  144. msg->peer_key = rspamd_pubkey_ref(peer_key);
  145. }
  146. cb = g_malloc(sizeof(*cb));
  147. cb->ts = rspamd_get_ticks();
  148. cb->lat = latency;
  149. cb->ev_base = ev_base;
  150. cb->wconns = wconns;
  151. latency->checked = FALSE;
  152. rspamd_http_connection_write_message(conn, msg, NULL, NULL, cb,
  153. fd, NULL, ev_base);
  154. }
  155. static void
  156. rspamd_worker_func(struct lat_elt *plat, uint32_t *wconns)
  157. {
  158. unsigned int i, j;
  159. struct ev_loop *ev_base;
  160. struct itimerval itv;
  161. struct rspamd_keypair_cache *c = NULL;
  162. struct rspamd_cryptobox_keypair *client_key = NULL;
  163. struct rspamd_cryptobox_pubkey *peer_key = NULL;
  164. if (server_key) {
  165. peer_key = rspamd_pubkey_from_base32(server_key, 0, RSPAMD_KEYPAIR_KEX,
  166. openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
  167. g_assert(peer_key != NULL);
  168. client_key = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX,
  169. openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519);
  170. if (cache_size > 0) {
  171. c = rspamd_keypair_cache_new(cache_size);
  172. }
  173. }
  174. memset(&itv, 0, sizeof(itv));
  175. double_to_tv(test_time, &itv.it_value);
  176. ev_base = event_init();
  177. g_assert(setitimer(ITIMER_REAL, &itv, NULL) != -1);
  178. for (i = 0;; i = (i + 1) % store_latencies) {
  179. for (j = 0; j < pconns; j++) {
  180. rspamd_http_client_func(ev_base, &plat[i * pconns + j],
  181. wconns, peer_key, client_key, c);
  182. }
  183. conns_pending = pconns;
  184. event_base_loop(ev_base, 0);
  185. }
  186. }
  187. static int
  188. cmpd(const void *p1, const void *p2)
  189. {
  190. const struct lat_elt *d1 = p1, *d2 = p2;
  191. return (d1->lat) - (d2->lat);
  192. }
  193. double
  194. rspamd_http_calculate_mean(struct lat_elt *lats, double *std)
  195. {
  196. unsigned int i, cnt, checked = 0;
  197. double mean = 0., dev = 0.;
  198. cnt = store_latencies * pconns;
  199. qsort(lats, cnt, sizeof(*lats), cmpd);
  200. for (i = 0; i < cnt; i++) {
  201. if (lats[i].checked) {
  202. mean += lats[i].lat;
  203. checked++;
  204. }
  205. }
  206. g_assert(checked > 0);
  207. mean /= checked;
  208. for (i = 0; i < cnt; i++) {
  209. if (lats[i].checked) {
  210. dev += pow((lats[i].lat - mean), 2);
  211. }
  212. }
  213. dev /= checked;
  214. *std = sqrt(dev);
  215. return mean;
  216. }
  217. static void
  218. rspamd_http_start_workers(pid_t *sfd)
  219. {
  220. unsigned int i;
  221. for (i = 0; i < nworkers; i++) {
  222. sfd[i] = fork();
  223. g_assert(sfd[i] != -1);
  224. if (sfd[i] == 0) {
  225. gperf_profiler_init(NULL, "http-bench");
  226. rspamd_worker_func(&latencies[i * pconns * store_latencies],
  227. &conns_done[i]);
  228. gperf_profiler_stop();
  229. exit(EXIT_SUCCESS);
  230. }
  231. workers_left++;
  232. }
  233. }
  234. static void
  235. rspamd_http_stop_workers(pid_t *sfd)
  236. {
  237. unsigned int i;
  238. int res;
  239. for (i = 0; i < nworkers; i++) {
  240. kill(sfd[i], SIGTERM);
  241. wait(&res);
  242. }
  243. }
  244. static void
  245. rspamd_http_bench_term(int fd, short what, void *arg)
  246. {
  247. pid_t *sfd = arg;
  248. rspamd_http_stop_workers(sfd);
  249. event_loopexit(NULL);
  250. }
  251. static void
  252. rspamd_http_bench_cld(int fd, short what, void *arg)
  253. {
  254. int res;
  255. while (waitpid(-1, &res, WNOHANG) > 0) {
  256. if (--workers_left == 0) {
  257. event_loopexit(NULL);
  258. }
  259. }
  260. }
  261. int main(int argc, char **argv)
  262. {
  263. GOptionContext *context;
  264. GError *error = NULL;
  265. pid_t *sfd;
  266. struct ev_loop *ev_base;
  267. rspamd_mempool_t *pool = rspamd_mempool_new(8192, "http-bench");
  268. struct event term_ev, int_ev, cld_ev;
  269. uint64_t total_done;
  270. FILE *lat_file;
  271. double mean, std;
  272. unsigned int i;
  273. rspamd_init_libs();
  274. context = g_option_context_new(
  275. "rspamd-http-bench - test server for benchmarks");
  276. g_option_context_set_summary(context,
  277. "Summary:\n Rspamd test HTTP benchmark " RVERSION
  278. "\n Release id: " RID);
  279. g_option_context_add_main_entries(context, entries, NULL);
  280. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  281. rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message);
  282. g_error_free(error);
  283. exit(EXIT_FAILURE);
  284. }
  285. rspamd_parse_inet_address(&addr, host, 0);
  286. g_assert(addr != NULL);
  287. rspamd_inet_address_set_port(addr, port);
  288. latencies = rspamd_mempool_alloc_shared(pool,
  289. nworkers * pconns * store_latencies * sizeof(*latencies));
  290. sfd = g_malloc(sizeof(*sfd) * nworkers);
  291. conns_done = rspamd_mempool_alloc_shared(pool, sizeof(uint32_t) * nworkers);
  292. memset(conns_done, 0, sizeof(uint32_t) * nworkers);
  293. rspamd_http_start_workers(sfd);
  294. ev_base = event_init();
  295. event_set(&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_bench_term, sfd);
  296. event_base_set(ev_base, &term_ev);
  297. event_add(&term_ev, NULL);
  298. event_set(&int_ev, SIGINT, EV_SIGNAL, rspamd_http_bench_term, sfd);
  299. event_base_set(ev_base, &int_ev);
  300. event_add(&int_ev, NULL);
  301. event_set(&cld_ev, SIGCHLD, EV_SIGNAL | EV_PERSIST,
  302. rspamd_http_bench_cld, NULL);
  303. event_base_set(ev_base, &cld_ev);
  304. event_add(&cld_ev, NULL);
  305. event_base_loop(ev_base, 0);
  306. total_done = 0;
  307. for (i = 0; i < nworkers; i++) {
  308. total_done += conns_done[i];
  309. }
  310. mean = rspamd_http_calculate_mean(latencies, &std);
  311. if (!csv_output) {
  312. rspamd_printf(
  313. "Made %L connections of size %d in %.6fs, %.6f cps, %.6f MB/sec\n",
  314. total_done,
  315. file_size,
  316. test_time,
  317. total_done / test_time,
  318. total_done * file_size / test_time / (1024.0 * 1024.0));
  319. rspamd_printf("Latency: %.6f ms mean, %.6f dev\n",
  320. mean * 1000.0, std * 1000.0);
  321. }
  322. else {
  323. /* size,connections,time,mean,stddev,conns,workers */
  324. rspamd_printf("%ud,%L,%.1f,%.6f,%.6f,%ud,%ud\n",
  325. file_size,
  326. total_done,
  327. test_time,
  328. mean * 1000.0,
  329. std * 1000.0,
  330. pconns,
  331. nworkers);
  332. }
  333. if (latencies_file) {
  334. lat_file = fopen(latencies_file, "w");
  335. if (lat_file) {
  336. for (i = 0; i < store_latencies * pconns; i++) {
  337. if (latencies[i].checked) {
  338. rspamd_fprintf(lat_file, "%.6f\n", latencies[i].lat);
  339. }
  340. }
  341. fclose(lat_file);
  342. }
  343. }
  344. rspamd_mempool_delete(pool);
  345. return 0;
  346. }