Merge commit 'c992cb4e42cc223f67ede0e48d7ff3f4947af0c6' as 'test/compatibility/C/uWebSockets'
This commit is contained in:
4
test/compatibility/C/uWebSockets/benchmarks/Makefile
Normal file
4
test/compatibility/C/uWebSockets/benchmarks/Makefile
Normal file
@ -0,0 +1,4 @@
|
||||
default:
|
||||
clang -flto -O3 -DLIBUS_USE_OPENSSL -I../uSockets/src ../uSockets/src/*.c ../uSockets/src/eventing/*.c ../uSockets/src/crypto/*.c broadcast_test.c -o broadcast_test -lssl -lcrypto
|
||||
clang -flto -O3 -DLIBUS_USE_OPENSSL -I../uSockets/src ../uSockets/src/*.c ../uSockets/src/eventing/*.c ../uSockets/src/crypto/*.c load_test.c -o load_test -lssl -lcrypto
|
||||
clang -flto -O3 -DLIBUS_USE_OPENSSL -I../uSockets/src ../uSockets/src/*.c ../uSockets/src/eventing/*.c ../uSockets/src/crypto/*.c scale_test.c -o scale_test -lssl -lcrypto
|
17
test/compatibility/C/uWebSockets/benchmarks/README.md
Normal file
17
test/compatibility/C/uWebSockets/benchmarks/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
# Benchmark-driven development
|
||||
|
||||
Just like testing code for correctness and stability, testing for performance is just as important if performance is a goal. You cannot really argue or reason about performance without having tests for it.
|
||||
|
||||
* Do not trust anyone who claims performance of any kind unless they provide benchmarks. Do not listen to people who talk about performance without having actual scientific data to back their claims up.
|
||||
* Never accept absolute numbers without a direct comparison with an alternative solution. Many projects can give you a number, X, which can be "50 billion messages per second". How much is this? What kind of worth does this number have? Impossible to know without a comparison. Absolute numbers mean nothing, relative comparisons are what you should look for.
|
||||
* Make sure to benchmark the correct thing. This is an extremely common mistake, done by many of the most well-known developers out there. If you measure for CPU-time efficiency (which you do) then normalizing for spent CPU-time is the difference between a completely invalid, botched and bogus test and something that might be valid.
|
||||
|
||||
Here are the current relative comparisons:
|
||||
|
||||
Http | WebSockets
|
||||
--- | ---
|
||||
 | 
|
||||
|
||||
Over the period of a few years I have never come across any web server which can score as high as µWebSockets do. This is not to say that µWebSockets is fastest, as "fastest" is a silly superlative nobody should *ever* use.
|
||||
|
||||
Never trust anyone using superlatives to describe their work; they are more often wrong than right.
|
196
test/compatibility/C/uWebSockets/benchmarks/broadcast_test.c
Normal file
196
test/compatibility/C/uWebSockets/benchmarks/broadcast_test.c
Normal file
@ -0,0 +1,196 @@
|
||||
/* This benchmark establishes _connections_ number of WebSocket
|
||||
clients, then iteratively performs the following:
|
||||
|
||||
1. Send one message for every client.
|
||||
2. Wait for the quadratic (_connections_^2) amount of responses from the server.
|
||||
3. Once received all expected bytes, repeat by going to step 1.
|
||||
|
||||
Every 4 seconds we print the current average "iterations per second".
|
||||
*/
|
||||
|
||||
#include <libusockets.h>
|
||||
int SSL;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
unsigned char web_socket_request[26] = {130, 128 | 20, 1, 2, 3, 4};
|
||||
|
||||
char request[] = "GET / HTTP/1.1\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
|
||||
"Host: server.example.com\r\n"
|
||||
"Sec-WebSocket-Version: 13\r\n\r\n";
|
||||
char *host;
|
||||
int port;
|
||||
int connections;
|
||||
|
||||
int satisfied_sockets;
|
||||
int iterations;
|
||||
|
||||
struct http_socket {
|
||||
/* How far we have streamed our websocket request */
|
||||
int offset;
|
||||
|
||||
/* How far we have streamed our upgrade request */
|
||||
int upgrade_offset;
|
||||
|
||||
/* Are we upgraded? */
|
||||
int is_upgraded;
|
||||
|
||||
/* Bytes received */
|
||||
int bytes_received;
|
||||
};
|
||||
|
||||
/* We track upgraded websockets */
|
||||
void **web_sockets;
|
||||
int num_web_sockets;
|
||||
|
||||
/* We don't need any of these */
|
||||
void noop(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
void start_iteration() {
|
||||
for (int i = 0; i < num_web_sockets; i++) {
|
||||
struct us_socket_t *s = (struct us_socket_t *) web_sockets[i];
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
http_socket->offset = us_socket_write(SSL, s, (char *) web_socket_request, sizeof(web_socket_request), 0);
|
||||
}
|
||||
}
|
||||
|
||||
void next_connection(struct us_socket_t *s) {
|
||||
/* Add this connection to our array */
|
||||
web_sockets[num_web_sockets++] = s;
|
||||
|
||||
/* We could wait with this until properly upgraded */
|
||||
if (--connections) {
|
||||
us_socket_context_connect(SSL, us_socket_context(SSL, s), host, port, 0, sizeof(struct http_socket));
|
||||
} else {
|
||||
printf("Running benchmark now...\n");
|
||||
start_iteration();
|
||||
|
||||
us_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY);
|
||||
}
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Are we still not upgraded yet? */
|
||||
if (http_socket->upgrade_offset < sizeof(request) - 1) {
|
||||
http_socket->upgrade_offset += us_socket_write(SSL, s, request + http_socket->upgrade_offset, sizeof(request) - 1 - http_socket->upgrade_offset, 0);
|
||||
} else {
|
||||
/* Stream whatever is remaining of the request */
|
||||
http_socket->offset += us_socket_write(SSL, s, (char *) web_socket_request + http_socket->offset, sizeof(web_socket_request) - http_socket->offset, 0);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
|
||||
|
||||
printf("Client was disconnected, exiting!\n");
|
||||
exit(-1);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
|
||||
return us_socket_close(SSL, s);
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_data(struct us_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(SSL, s);
|
||||
|
||||
/* Are we already upgraded? */
|
||||
if (http_socket->is_upgraded) {
|
||||
http_socket->bytes_received += length;
|
||||
|
||||
if (http_socket->bytes_received == (sizeof(web_socket_request) - 4) * num_web_sockets) {
|
||||
satisfied_sockets++;
|
||||
http_socket->bytes_received = 0;
|
||||
|
||||
if (satisfied_sockets == num_web_sockets) {
|
||||
iterations++;
|
||||
satisfied_sockets = 0;
|
||||
|
||||
start_iteration();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* We assume the server is not sending anything immediately following upgrade and that we get rnrn in one chunk */
|
||||
if (length >= 4 && data[length - 1] == '\n' && data[length - 2] == '\r' && data[length - 3] == '\n' && data[length - 4] == '\r') {
|
||||
http_socket->is_upgraded = 1;
|
||||
next_connection(s);
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Reset offsets */
|
||||
http_socket->offset = 0;
|
||||
http_socket->is_upgraded = 0;
|
||||
http_socket->bytes_received = 0;
|
||||
|
||||
/* Send an upgrade request */
|
||||
http_socket->upgrade_offset = us_socket_write(SSL, s, request, sizeof(request) - 1, 0);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
|
||||
/* Print current statistics */
|
||||
printf("Iterations/second (%d clients): %f\n", num_web_sockets, ((float)iterations) / LIBUS_TIMEOUT_GRANULARITY);
|
||||
|
||||
iterations = 0;
|
||||
us_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
/* Parse host and port */
|
||||
if (argc != 5) {
|
||||
printf("Usage: connections host port ssl\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
port = atoi(argv[3]);
|
||||
host = malloc(strlen(argv[2]) + 1);
|
||||
memcpy(host, argv[2], strlen(argv[2]) + 1);
|
||||
connections = atoi(argv[1]);
|
||||
SSL = atoi(argv[4]);
|
||||
|
||||
/* Allocate room for every socket */
|
||||
web_sockets = (void **) malloc(sizeof(void *) * connections);
|
||||
|
||||
/* Create the event loop */
|
||||
struct us_loop_t *loop = us_create_loop(0, noop, noop, noop, 0);
|
||||
|
||||
/* Create a socket context for HTTP */
|
||||
struct us_socket_context_options_t options = {};
|
||||
struct us_socket_context_t *http_context = us_create_socket_context(SSL, loop, 0, options);
|
||||
|
||||
/* Set up event handlers */
|
||||
us_socket_context_on_open(SSL, http_context, on_http_socket_open);
|
||||
us_socket_context_on_data(SSL, http_context, on_http_socket_data);
|
||||
us_socket_context_on_writable(SSL, http_context, on_http_socket_writable);
|
||||
us_socket_context_on_close(SSL, http_context, on_http_socket_close);
|
||||
us_socket_context_on_timeout(SSL, http_context, on_http_socket_timeout);
|
||||
us_socket_context_on_end(SSL, http_context, on_http_socket_end);
|
||||
|
||||
/* Start making HTTP connections */
|
||||
us_socket_context_connect(SSL, http_context, host, port, 0, sizeof(struct http_socket));
|
||||
|
||||
us_loop_run(loop);
|
||||
}
|
161
test/compatibility/C/uWebSockets/benchmarks/load_test.c
Normal file
161
test/compatibility/C/uWebSockets/benchmarks/load_test.c
Normal file
@ -0,0 +1,161 @@
|
||||
/* This is a simple yet efficient WebSocket server benchmark much like WRK */
|
||||
|
||||
#include <libusockets.h>
|
||||
int SSL;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// request eller upgradeRequest samt webSocketFrame
|
||||
|
||||
unsigned char web_socket_request[26] = {130, 128 | 20, 1, 2, 3, 4};
|
||||
|
||||
char request[] = "GET / HTTP/1.1\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
|
||||
"Host: server.example.com\r\n"
|
||||
"Sec-WebSocket-Version: 13\r\n\r\n";
|
||||
char *host;
|
||||
int port;
|
||||
int connections;
|
||||
|
||||
int responses;
|
||||
|
||||
struct http_socket {
|
||||
/* 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 */
|
||||
void on_wakeup(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
void on_pre(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
/* This is not HTTP POST, it is merely an event emitted post loop iteration */
|
||||
void on_post(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
void next_connection(struct us_socket_t *s) {
|
||||
/* We could wait with this until properly upgraded */
|
||||
if (--connections) {
|
||||
us_socket_context_connect(SSL, us_socket_context(SSL, s), host, port, 0, sizeof(struct http_socket));
|
||||
} else {
|
||||
printf("Running benchmark now...\n");
|
||||
|
||||
us_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY);
|
||||
}
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Are we still not upgraded yet? */
|
||||
if (http_socket->upgrade_offset < sizeof(request) - 1) {
|
||||
http_socket->upgrade_offset += us_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_socket_write(SSL, s, (char *) web_socket_request + http_socket->offset, sizeof(web_socket_request) - http_socket->offset, 0);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
|
||||
|
||||
printf("Closed!\n");
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
|
||||
return us_socket_close(SSL, s);
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_data(struct us_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(SSL, s);
|
||||
//struct http_context *http_context = (struct http_context *) us_socket_context_ext(SSL, us_socket_context(SSL, s));
|
||||
|
||||
/* We treat all data events as a response */
|
||||
http_socket->offset = us_socket_write(SSL, s, (char *) web_socket_request, sizeof(web_socket_request), 0);
|
||||
|
||||
/* */
|
||||
responses++;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Reset offsets */
|
||||
http_socket->offset = 0;
|
||||
|
||||
/* Send an upgrade request */
|
||||
http_socket->upgrade_offset = us_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_t *on_http_socket_timeout(struct us_socket_t *s) {
|
||||
/* Print current statistics */
|
||||
printf("Msg/sec: %f\n", ((float)responses) / LIBUS_TIMEOUT_GRANULARITY);
|
||||
|
||||
responses = 0;
|
||||
us_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
/* Parse host and port */
|
||||
if (argc != 5) {
|
||||
printf("Usage: connections host port ssl\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
port = atoi(argv[3]);
|
||||
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_t *loop = us_create_loop(0, on_wakeup, on_pre, on_post, 0);
|
||||
|
||||
/* Create a socket context for HTTP */
|
||||
struct us_socket_context_options_t options = {};
|
||||
struct us_socket_context_t *http_context = us_create_socket_context(SSL, loop, 0, options);
|
||||
|
||||
/* Set up event handlers */
|
||||
us_socket_context_on_open(SSL, http_context, on_http_socket_open);
|
||||
us_socket_context_on_data(SSL, http_context, on_http_socket_data);
|
||||
us_socket_context_on_writable(SSL, http_context, on_http_socket_writable);
|
||||
us_socket_context_on_close(SSL, http_context, on_http_socket_close);
|
||||
us_socket_context_on_timeout(SSL, http_context, on_http_socket_timeout);
|
||||
us_socket_context_on_end(SSL, http_context, on_http_socket_end);
|
||||
|
||||
/* Start making HTTP connections */
|
||||
us_socket_context_connect(SSL, http_context, host, port, 0, sizeof(struct http_socket));
|
||||
|
||||
us_loop_run(loop);
|
||||
}
|
185
test/compatibility/C/uWebSockets/benchmarks/scale_test.c
Normal file
185
test/compatibility/C/uWebSockets/benchmarks/scale_test.c
Normal file
@ -0,0 +1,185 @@
|
||||
/* This is a scalability test for testing million(s) of pinging connections */
|
||||
|
||||
#include <libusockets.h>
|
||||
int SSL;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
unsigned char web_socket_request[26] = {130, 128 | 20, 1, 2, 3, 4};
|
||||
|
||||
char request[] = "GET / HTTP/1.1\r\n"
|
||||
"Upgrade: websocket\r\n"
|
||||
"Connection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
|
||||
"Host: server.example.com\r\n"
|
||||
"Sec-WebSocket-Version: 13\r\n\r\n";
|
||||
char *host;
|
||||
int port;
|
||||
int connections;
|
||||
|
||||
/* Send ping every 16 seconds */
|
||||
int WEBSOCKET_PING_INTERVAL = 16;
|
||||
|
||||
/* We only establish 20k connections per address */
|
||||
int CONNECTIONS_PER_ADDRESS = 20000;
|
||||
|
||||
/* How many connections a time */
|
||||
int BATCH_CONNECT = 1;
|
||||
|
||||
/* Currently open and alive connections */
|
||||
int opened_connections;
|
||||
/* Dead connections */
|
||||
int closed_connections;
|
||||
|
||||
struct http_socket {
|
||||
/* 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 */
|
||||
void on_wakeup(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
void on_pre(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
/* This is not HTTP POST, it is merely an event emitted post loop iteration */
|
||||
void on_post(struct us_loop_t *loop) {
|
||||
|
||||
}
|
||||
|
||||
void next_connection(struct us_socket_t *s) {
|
||||
/* We could wait with this until properly upgraded */
|
||||
if (--connections/* > BATCH_CONNECT*/) {
|
||||
/* Swap address */
|
||||
int address = opened_connections / CONNECTIONS_PER_ADDRESS + 1;
|
||||
char buf[16];
|
||||
sprintf(buf, "127.0.0.%d", address);
|
||||
|
||||
us_socket_context_connect(SSL, us_socket_context(SSL, s), buf, port, 0, sizeof(struct http_socket));
|
||||
}
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Are we still not upgraded yet? */
|
||||
if (http_socket->upgrade_offset < sizeof(request) - 1) {
|
||||
http_socket->upgrade_offset += us_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);
|
||||
|
||||
/* Make sure to send ping */
|
||||
us_socket_timeout(SSL, s, WEBSOCKET_PING_INTERVAL);
|
||||
}
|
||||
} else {
|
||||
/* Stream whatever is remaining of the request */
|
||||
http_socket->offset += us_socket_write(SSL, s, (char *) web_socket_request + http_socket->offset, sizeof(web_socket_request) - http_socket->offset, 0);
|
||||
if (http_socket->offset == sizeof(web_socket_request)) {
|
||||
/* Reset timeout if we managed to */
|
||||
us_socket_timeout(SSL, s, WEBSOCKET_PING_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
|
||||
|
||||
closed_connections++;
|
||||
if (closed_connections % 1000 == 0) {
|
||||
printf("Alive: %d, dead: %d\n", opened_connections, closed_connections);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
|
||||
return us_socket_close(SSL, s);
|
||||
}
|
||||
|
||||
// should never get a response!
|
||||
struct us_socket_t *on_http_socket_data(struct us_socket_t *s, char *data, int length) {
|
||||
return s;
|
||||
}
|
||||
|
||||
struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Display number of opened connections */
|
||||
opened_connections++;
|
||||
if (opened_connections % 1000 == 0) {
|
||||
printf("Alive: %d, dead: %d\n", opened_connections, closed_connections);
|
||||
}
|
||||
|
||||
/* Send an upgrade request */
|
||||
http_socket->upgrade_offset = us_socket_write(SSL, s, request, sizeof(request) - 1, 0);
|
||||
if (http_socket->upgrade_offset == sizeof(request) - 1) {
|
||||
next_connection(s);
|
||||
|
||||
/* Make sure to send ping */
|
||||
us_socket_timeout(SSL, s, WEBSOCKET_PING_INTERVAL);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
// here we should send a message as ping (part of the test)
|
||||
struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
|
||||
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
|
||||
|
||||
/* Send ping here */
|
||||
http_socket->offset = us_socket_write(SSL, s, (char *) web_socket_request, sizeof(web_socket_request), 0);
|
||||
if (http_socket->offset == sizeof(web_socket_request)) {
|
||||
/* Reset timeout if we managed to */
|
||||
us_socket_timeout(SSL, s, WEBSOCKET_PING_INTERVAL);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
/* Parse host and port */
|
||||
if (argc != 5) {
|
||||
printf("Usage: connections host port ssl\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
port = atoi(argv[3]);
|
||||
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_t *loop = us_create_loop(0, on_wakeup, on_pre, on_post, 0);
|
||||
|
||||
/* Create a socket context for HTTP */
|
||||
struct us_socket_context_options_t options = {};
|
||||
struct us_socket_context_t *http_context = us_create_socket_context(SSL, loop, 0, options);
|
||||
|
||||
/* Set up event handlers */
|
||||
us_socket_context_on_open(SSL, http_context, on_http_socket_open);
|
||||
us_socket_context_on_data(SSL, http_context, on_http_socket_data);
|
||||
us_socket_context_on_writable(SSL, http_context, on_http_socket_writable);
|
||||
us_socket_context_on_close(SSL, http_context, on_http_socket_close);
|
||||
us_socket_context_on_timeout(SSL, http_context, on_http_socket_timeout);
|
||||
us_socket_context_on_end(SSL, http_context, on_http_socket_end);
|
||||
|
||||
/* Start making HTTP connections */
|
||||
for (int i = 0; i < BATCH_CONNECT; i++) {
|
||||
us_socket_context_connect(SSL, http_context, host, port, 0, sizeof(struct http_socket));
|
||||
}
|
||||
|
||||
us_loop_run(loop);
|
||||
}
|
Reference in New Issue
Block a user