From ca38ea16690a94ffdbb8f84a518bf97a665c551a Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 28 Jan 2019 18:20:39 +0100 Subject: [PATCH] Port load_test to SSL/non-SSL uSockets API --- benchmarks/Makefile | 2 +- benchmarks/load_test.c | 106 +++++++++++++++++++++++------------------ uSockets | 2 +- 3 files changed, 62 insertions(+), 48 deletions(-) diff --git a/benchmarks/Makefile b/benchmarks/Makefile index bd482ab..296d800 100644 --- a/benchmarks/Makefile +++ b/benchmarks/Makefile @@ -1,2 +1,2 @@ default: - clang -flto -O3 -DLIBUS_NO_SSL -I../uSockets/src ../uSockets/src/*.c ../uSockets/src/eventing/*.c load_test.c -o load_test + clang -flto -O3 -I../uSockets/src ../uSockets/src/*.c ../uSockets/src/eventing/*.c load_test.c -o load_test -lssl -lcrypto diff --git a/benchmarks/load_test.c b/benchmarks/load_test.c index f19af43..cdef569 100644 --- a/benchmarks/load_test.c +++ b/benchmarks/load_test.c @@ -1,7 +1,7 @@ /* This is a simple yet efficient WebSocket server benchmark much like WRK */ -#include -//#include "helper.h" +#include +int SSL; #include #include @@ -24,8 +24,11 @@ int connections; int responses; struct http_socket { - /* How far we have streamed our request */ + /* How far we have streamed our websocket request */ int offset; + + /* How far we have streamed our upgrade request */ + int upgrade_offset; }; /* We don't need any of these */ @@ -42,68 +45,82 @@ void on_post(struct us_loop *loop) { } -struct us_socket *on_http_socket_writable(struct us_socket *s) { - struct http_socket *http_socket = (struct http_socket *) us_socket_ext(s); +void next_connection(struct us_new_socket_t *s) { + /* We could wait with this until properly upgraded */ + if (--connections) { + us_new_socket_context_connect(SSL, us_new_socket_context(SSL, s), host, port, 0, sizeof(struct http_socket)); + } else { + printf("Running benchmark now...\n"); - /* Stream whatever is remaining of the request */ - http_socket->offset += us_socket_write(s, web_socket_request + http_socket->offset, sizeof(web_socket_request) - http_socket->offset, 0); + us_new_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY); + } +} + +struct us_new_socket_t *on_http_socket_writable(struct us_new_socket_t *s) { + struct http_socket *http_socket = (struct http_socket *) us_new_socket_ext(SSL, s); + + /* Are we still not upgraded yet? */ + if (http_socket->upgrade_offset < sizeof(request) - 1) { + http_socket->upgrade_offset += us_new_socket_write(SSL, s, request + http_socket->upgrade_offset, sizeof(request) - 1 - http_socket->upgrade_offset, 0); + + /* Now we should be */ + if (http_socket->upgrade_offset == sizeof(request) - 1) { + next_connection(s); + } + } else { + /* Stream whatever is remaining of the request */ + http_socket->offset += us_new_socket_write(SSL, s, web_socket_request + http_socket->offset, sizeof(web_socket_request) - http_socket->offset, 0); + } return s; } -struct us_socket *on_http_socket_close(struct us_socket *s) { +struct us_new_socket_t *on_http_socket_close(struct us_new_socket_t *s) { printf("Closed!\n"); return s; } -struct us_socket *on_http_socket_end(struct us_socket *s) { - return us_socket_close(s); +struct us_new_socket_t *on_http_socket_end(struct us_new_socket_t *s) { + return us_new_socket_close(SSL, s); } -struct us_socket *on_http_socket_data(struct us_socket *s, char *data, int length) { +struct us_new_socket_t *on_http_socket_data(struct us_new_socket_t *s, char *data, int length) { /* Get socket extension and the socket's context's extension */ - struct http_socket *http_socket = (struct http_socket *) us_socket_ext(s); - struct http_context *http_context = (struct http_context *) us_socket_context_ext(us_socket_get_context(s)); + struct http_socket *http_socket = (struct http_socket *) us_new_socket_ext(SSL, s); + //struct http_context *http_context = (struct http_context *) us_new_socket_context_ext(SSL, us_new_socket_context(SSL, s)); /* We treat all data events as a response */ - http_socket->offset = us_socket_write(s, web_socket_request, sizeof(web_socket_request), 0); + http_socket->offset = us_new_socket_write(SSL, s, web_socket_request, sizeof(web_socket_request), 0); /* */ responses++; - //printf("Fick ett svar\n"); - return s; } -struct us_socket *on_http_socket_open(struct us_socket *s, int is_client) { - struct http_socket *http_socket = (struct http_socket *) us_socket_ext(s); +struct us_new_socket_t *on_http_socket_open(struct us_new_socket_t *s, int is_client) { + struct http_socket *http_socket = (struct http_socket *) us_new_socket_ext(SSL, s); - /* Reset offset */ + /* Reset offsets */ http_socket->offset = 0; - /* Send a request */ - us_socket_write(s, request, sizeof(request) - 1, 0); - - if (--connections) { - us_socket_context_connect(us_socket_get_context(s), host, port, 0, sizeof(struct http_socket)); - } else { - printf("Running benchmark now...\n"); - - us_socket_timeout(s, LIBUS_TIMEOUT_GRANULARITY); + /* Send an upgrade request */ + http_socket->upgrade_offset = us_new_socket_write(SSL, s, request, sizeof(request) - 1, 0); + if (http_socket->upgrade_offset == sizeof(request) - 1) { + next_connection(s); } return s; } -struct us_socket *on_http_socket_timeout(struct us_socket *s) { +struct us_new_socket_t *on_http_socket_timeout(struct us_new_socket_t *s) { /* Print current statistics */ - printf("Req/sec: %f\n", ((float)responses) / LIBUS_TIMEOUT_GRANULARITY); + printf("Msg/sec: %f\n", ((float)responses) / LIBUS_TIMEOUT_GRANULARITY); responses = 0; - us_socket_timeout(s, LIBUS_TIMEOUT_GRANULARITY); + us_new_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY); return s; } @@ -111,8 +128,8 @@ struct us_socket *on_http_socket_timeout(struct us_socket *s) { int main(int argc, char **argv) { /* Parse host and port */ - if (argc != 4) { - printf("Usage: connections host port\n"); + if (argc != 5) { + printf("Usage: connections host port ssl\n"); return 0; } @@ -120,28 +137,25 @@ int main(int argc, char **argv) { host = malloc(strlen(argv[2]) + 1); memcpy(host, argv[2], strlen(argv[2]) + 1); connections = atoi(argv[1]); + SSL = atoi(argv[4]); /* Create the event loop */ struct us_loop *loop = us_create_loop(1, on_wakeup, on_pre, on_post, 0); /* Create a socket context for HTTP */ -#ifndef LIBUS_NO_SSL - struct us_ssl_socket_context_options ssl_options = {}; - struct us_socket_context *http_context = us_create_ssl_socket_context(loop, 0, ssl_options); -#else - struct us_socket_context *http_context = us_create_socket_context(loop, 0); -#endif + struct us_new_socket_context_options_t options = {}; + struct us_new_socket_context_t *http_context = us_new_create_socket_context(SSL, loop, 0, options); /* Set up event handlers */ - us_socket_context_on_open(http_context, on_http_socket_open); - us_socket_context_on_data(http_context, on_http_socket_data); - us_socket_context_on_writable(http_context, on_http_socket_writable); - us_socket_context_on_close(http_context, on_http_socket_close); - us_socket_context_on_timeout(http_context, on_http_socket_timeout); - us_socket_context_on_end(http_context, on_http_socket_end); + us_new_socket_context_on_open(SSL, http_context, on_http_socket_open); + us_new_socket_context_on_data(SSL, http_context, on_http_socket_data); + us_new_socket_context_on_writable(SSL, http_context, on_http_socket_writable); + us_new_socket_context_on_close(SSL, http_context, on_http_socket_close); + us_new_socket_context_on_timeout(SSL, http_context, on_http_socket_timeout); + us_new_socket_context_on_end(SSL, http_context, on_http_socket_end); /* Start making HTTP connections */ - us_socket_context_connect(http_context, host, port, 0, sizeof(struct http_socket)); + us_new_socket_context_connect(SSL, http_context, host, port, 0, sizeof(struct http_socket)); us_loop_run(loop); } diff --git a/uSockets b/uSockets index ee6b115..bf6b858 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit ee6b1154583b8a18a4b2c6de1ced5acc722a2dae +Subproject commit bf6b858c37cdc549b9d7bfa530710aaa068ef35b