gint fuzzy_check_module_init (struct rspamd_config *cfg, struct module_ctx **ctx) { fuzzy_module_ctx = g_malloc0 (sizeof (struct fuzzy_ctx)); fuzzy_module_ctx->fuzzy_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); fuzzy_module_ctx->cfg = cfg; /* TODO: this should match rules count actually */ fuzzy_module_ctx->keypairs_cache = rspamd_keypair_cache_new (32); *ctx = (struct module_ctx *)fuzzy_module_ctx; return 0; }
struct rspamd_client_connection * rspamd_client_init (struct event_base *ev_base, const gchar *name, guint16 port, gdouble timeout, const gchar *key) { struct rspamd_client_connection *conn; gint fd; fd = rspamd_socket (name, port, SOCK_STREAM, TRUE, FALSE, TRUE); if (fd == -1) { return NULL; } conn = g_slice_alloc0 (sizeof (struct rspamd_client_connection)); conn->ev_base = ev_base; conn->fd = fd; conn->req_sent = FALSE; conn->keys_cache = rspamd_keypair_cache_new (32); conn->http_conn = rspamd_http_connection_new (rspamd_client_body_handler, rspamd_client_error_handler, rspamd_client_finish_handler, 0, RSPAMD_HTTP_CLIENT, conn->keys_cache, NULL); conn->server_name = g_string_new (name); if (port != 0) { rspamd_printf_gstring (conn->server_name, ":%d", (int)port); } double_to_tv (timeout, &conn->timeout); if (key) { conn->key = rspamd_pubkey_from_base32 (key, 0, RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); if (conn->key) { conn->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); rspamd_http_connection_set_key (conn->http_conn, conn->keypair); } else { rspamd_client_destroy (conn); return NULL; } } return conn; }
void rspamd_http_test_func (void) { struct event_base *ev_base = event_init (); rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); gpointer serv_key, client_key, peer_key; struct rspamd_keypair_cache *c; rspamd_mempool_mutex_t *mtx; rspamd_inet_addr_t addr; struct timespec ts1, ts2; gchar filepath[PATH_MAX], buf[512]; gint fd, i, j; pid_t sfd; GString *b32_key; double diff, total_diff = 0.0, latency[pconns * ntests], mean, std; rspamd_cryptobox_init (); rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX"); g_assert ((fd = mkstemp (filepath)) != -1); for (i = 0; i < file_blocks; i ++) { memset (buf, 0, sizeof (buf)); g_assert (write (fd, buf, sizeof (buf)) == sizeof (buf)); } mtx = rspamd_mempool_get_mutex (pool); rspamd_parse_inet_address (&addr, "127.0.0.1"); rspamd_inet_address_set_port (&addr, ottery_rand_range (30000) + 32768); serv_key = rspamd_http_connection_gen_key (); client_key = rspamd_http_connection_gen_key (); c = rspamd_keypair_cache_new (16); rspamd_mempool_lock_mutex (mtx); sfd = fork (); g_assert (sfd != -1); if (sfd == 0) { rspamd_http_server_func ("/tmp/", &addr, mtx, serv_key, c); exit (EXIT_SUCCESS); } rspamd_mempool_lock_mutex (mtx); /* Do client stuff */ for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr, NULL, NULL, c, ev_base, &latency[i * pconns + j]); } clock_gettime (CLOCK_MONOTONIC, &ts1); event_base_loop (ev_base, 0); clock_gettime (CLOCK_MONOTONIC, &ts2); diff = (ts2.tv_sec - ts1.tv_sec) * 1000. + /* Seconds */ (ts2.tv_nsec - ts1.tv_nsec) / 1000000.; /* Nanoseconds */ total_diff += diff; } msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps", ntests * pconns, sizeof (buf) * file_blocks, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); /* Now test encrypted */ b32_key = rspamd_http_connection_print_key (serv_key, RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); g_assert (b32_key != NULL); peer_key = rspamd_http_connection_make_peer_key (b32_key->str); g_assert (peer_key != NULL); total_diff = 0.0; for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr, client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } clock_gettime (CLOCK_MONOTONIC, &ts1); event_base_loop (ev_base, 0); clock_gettime (CLOCK_MONOTONIC, &ts2); diff = (ts2.tv_sec - ts1.tv_sec) * 1000. + /* Seconds */ (ts2.tv_nsec - ts1.tv_nsec) / 1000000.; /* Nanoseconds */ total_diff += diff; } msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, sizeof (buf) * file_blocks, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); /* Restart server */ kill (sfd, SIGTERM); wait (&i); sfd = fork (); g_assert (sfd != -1); if (sfd == 0) { rspamd_http_server_func ("/tmp/", &addr, mtx, serv_key, NULL); exit (EXIT_SUCCESS); } rspamd_mempool_lock_mutex (mtx); total_diff = 0.0; for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, &addr, client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } clock_gettime (CLOCK_MONOTONIC, &ts1); event_base_loop (ev_base, 0); clock_gettime (CLOCK_MONOTONIC, &ts2); diff = (ts2.tv_sec - ts1.tv_sec) * 1000. + /* Seconds */ (ts2.tv_nsec - ts1.tv_nsec) / 1000000.; /* Nanoseconds */ total_diff += diff; } msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, sizeof (buf) * file_blocks, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); close (fd); unlink (filepath); kill (sfd, SIGTERM); }
int main (int argc, gchar **argv) { GOptionContext *context; GError *error = NULL; struct event_base *ev_base; GString *b32_key; pid_t *sfd; rspamd_inet_addr_t *addr; struct event term_ev, int_ev; struct in_addr ina = {INADDR_ANY}; rspamd_init_libs (); context = g_option_context_new ( "rspamd-http-server - test server for benchmarks"); g_option_context_set_summary (context, "Summary:\n Rspamd test HTTP server " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } maps = g_hash_table_new (g_int_hash, g_int_equal); if (key == NULL) { server_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, openssl_mode ? RSPAMD_CRYPTOBOX_MODE_NIST : RSPAMD_CRYPTOBOX_MODE_25519); b32_key = rspamd_keypair_print (server_key, RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); rspamd_printf ("key: %v\n", b32_key); } else { /* TODO: add key loading */ } if (cache_size > 0) { c = rspamd_keypair_cache_new (cache_size); } sfd = g_alloca (sizeof (*sfd) * nworkers); addr = rspamd_inet_address_new (AF_INET, &ina); rspamd_inet_address_set_port (addr, port); rspamd_http_start_servers (sfd, addr); /* Just wait for workers */ ev_base = event_init (); event_set (&term_ev, SIGTERM, EV_SIGNAL, rspamd_http_server_term, sfd); event_base_set (ev_base, &term_ev); event_add (&term_ev, NULL); event_set (&int_ev, SIGINT, EV_SIGNAL, rspamd_http_server_term, sfd); event_base_set (ev_base, &int_ev); event_add (&int_ev, NULL); event_base_loop (ev_base, 0); return 0; }
void rspamd_http_test_func (void) { struct event_base *ev_base = event_init (); rspamd_mempool_t *pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); struct rspamd_cryptobox_keypair *serv_key, *client_key; struct rspamd_cryptobox_pubkey *peer_key; struct rspamd_keypair_cache *c; rspamd_mempool_mutex_t *mtx; rspamd_inet_addr_t *addr; gdouble ts1, ts2; gchar filepath[PATH_MAX], *buf; gchar *env; gint fd; guint i, j; pid_t *sfd; GString *b32_key; double diff, total_diff = 0.0, *latency, mean, std; /* Read environment */ if ((env = getenv ("RSPAMD_HTTP_CONNS")) != NULL) { pconns = strtoul (env, NULL, 10); } else { return; } if ((env = getenv ("RSPAMD_HTTP_TESTS")) != NULL) { ntests = strtoul (env, NULL, 10); } if ((env = getenv ("RSPAMD_HTTP_SIZE")) != NULL) { file_size = strtoul (env, NULL, 10); } if ((env = getenv ("RSPAMD_HTTP_SERVERS")) != NULL) { nservers = strtoul (env, NULL, 10); } rspamd_cryptobox_init (); rspamd_snprintf (filepath, sizeof (filepath), "/tmp/http-test-XXXXXX"); g_assert ((fd = mkstemp (filepath)) != -1); sfd = g_alloca (sizeof (*sfd) * nservers); latency = g_malloc0 (pconns * ntests * sizeof (gdouble)); buf = g_malloc (file_size); memset (buf, 0, file_size); g_assert (write (fd, buf, file_size) == file_size); g_free (buf); mtx = rspamd_mempool_get_mutex (pool); rspamd_parse_inet_address (&addr, "127.0.0.1", 0); rspamd_inet_address_set_port (addr, 43898); serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); c = rspamd_keypair_cache_new (16); rspamd_http_start_servers (sfd, addr, serv_key, NULL); usleep (100000); /* Do client stuff */ gperf_profiler_init (NULL, "plain-http-client"); for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, NULL, NULL, c, ev_base, &latency[i * pconns + j]); } ts1 = rspamd_get_ticks (); event_base_loop (ev_base, 0); ts2 = rspamd_get_ticks (); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } gperf_profiler_stop (); msg_info ("Made %d connections of size %d in %.6f ms, %.6f cps", ntests * pconns, file_size, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); rspamd_http_stop_servers (sfd); rspamd_http_start_servers (sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); usleep (100000); b32_key = rspamd_keypair_print (serv_key, RSPAMD_KEYPAIR_PUBKEY|RSPAMD_KEYPAIR_BASE32); g_assert (b32_key != NULL); peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_25519); g_assert (peer_key != NULL); total_diff = 0.0; gperf_profiler_init (NULL, "cached-http-client"); for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } ts1 = rspamd_get_ticks (); event_base_loop (ev_base, 0); ts2 = rspamd_get_ticks (); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } gperf_profiler_stop (); msg_info ("Made %d encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, file_size, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); /* Restart server */ rspamd_http_stop_servers (sfd); /* No keypairs cache */ rspamd_http_start_servers (sfd, addr, serv_key, NULL); usleep (100000); total_diff = 0.0; gperf_profiler_init (NULL, "fair-http-client"); for (i = 0; i < ntests; i ++) { for (j = 0; j < pconns; j ++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } ts1 = rspamd_get_ticks (); event_base_loop (ev_base, 0); ts2 = rspamd_get_ticks (); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } gperf_profiler_stop (); msg_info ("Made %d uncached encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, file_size, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); /* AES mode */ serv_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); client_key = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); c = rspamd_keypair_cache_new (16); /* Restart server */ rspamd_http_stop_servers (sfd); /* No keypairs cache */ rspamd_http_start_servers (sfd, addr, serv_key, c); //rspamd_mempool_lock_mutex (mtx); usleep (100000); b32_key = rspamd_keypair_print (serv_key, RSPAMD_KEYPAIR_PUBKEY | RSPAMD_KEYPAIR_BASE32); g_assert (b32_key != NULL); peer_key = rspamd_pubkey_from_base32 (b32_key->str, b32_key->len, RSPAMD_KEYPAIR_KEX, RSPAMD_CRYPTOBOX_MODE_NIST); g_assert (peer_key != NULL); total_diff = 0.0; gperf_profiler_init (NULL, "cached-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, client_key, peer_key, NULL, ev_base, &latency[i * pconns + j]); } ts1 = rspamd_get_ticks (); event_base_loop (ev_base, 0); ts2 = rspamd_get_ticks (); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } gperf_profiler_stop (); msg_info ( "Made %d aes encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, file_size, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); /* Restart server */ rspamd_http_stop_servers (sfd); /* No keypairs cache */ rspamd_http_start_servers (sfd, addr, serv_key, NULL); //rspamd_mempool_lock_mutex (mtx); usleep (100000); total_diff = 0.0; gperf_profiler_init (NULL, "fair-http-client-aes"); for (i = 0; i < ntests; i++) { for (j = 0; j < pconns; j++) { rspamd_http_client_func (filepath + sizeof ("/tmp") - 1, addr, client_key, peer_key, c, ev_base, &latency[i * pconns + j]); } ts1 = rspamd_get_ticks (); event_base_loop (ev_base, 0); ts2 = rspamd_get_ticks (); diff = (ts2 - ts1) * 1000.0; total_diff += diff; } gperf_profiler_stop (); msg_info ( "Made %d uncached aes encrypted connections of size %d in %.6f ms, %.6f cps", ntests * pconns, file_size, total_diff, ntests * pconns / total_diff * 1000.); mean = rspamd_http_calculate_mean (latency, &std); msg_info ("Latency: %.6f ms mean, %.6f dev", mean, std); close (fd); unlink (filepath); rspamd_http_stop_servers (sfd); }