Skip to content

Commit

Permalink
Support benchmarking pipelined http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Oct 18, 2024
1 parent 89db170 commit 9e1ccc7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions examples/http_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ const int SSL = 1;
#include <stdlib.h>
#include <string.h>

char request[] = "GET / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n";
char request_template[] = "GET / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n";
char *request;
int request_size;
char *host;
int port;
int connections;

int responses;
int pipeline = 1;

struct http_socket {
/* How far we have streamed our request */
Expand All @@ -37,7 +40,7 @@ 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);

/* Stream whatever is remaining of the request */
http_socket->offset += us_socket_write(SSL, s, request + http_socket->offset, (sizeof(request) - 1) - http_socket->offset, 0);
http_socket->offset += us_socket_write(SSL, s, request + http_socket->offset, (request_size) - http_socket->offset, 0);

return s;
}
Expand All @@ -55,7 +58,7 @@ struct us_socket_t *on_http_socket_data(struct us_socket_t *s, char *data, int l
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);

/* We treat all data events as a response */
http_socket->offset = us_socket_write(SSL, s, request, sizeof(request) - 1, 0);
http_socket->offset = us_socket_write(SSL, s, request, request_size, 0);

/* */
responses++;
Expand All @@ -70,7 +73,7 @@ struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, ch
http_socket->offset = 0;

/* Send a request */
us_socket_write(SSL, s, request, sizeof(request) - 1, 0);
us_socket_write(SSL, s, request, request_size, 0);

if (--connections) {
us_socket_context_connect(SSL, us_socket_context(SSL, s), host, port, NULL, 0, sizeof(struct http_socket));
Expand All @@ -94,7 +97,7 @@ struct us_socket_t *on_http_socket_long_timeout(struct us_socket_t *s) {

struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
/* Print current statistics */
printf("Req/sec: %f\n", ((float)responses) / LIBUS_TIMEOUT_GRANULARITY);
printf("Req/sec: %f\n", ((float)pipeline) * ((float)responses) / LIBUS_TIMEOUT_GRANULARITY);

responses = 0;
us_socket_timeout(SSL, s, LIBUS_TIMEOUT_GRANULARITY);
Expand All @@ -111,11 +114,23 @@ struct us_socket_t *on_http_socket_connect_error(struct us_socket_t *s, int code
int main(int argc, char **argv) {

/* Parse host and port */
if (argc != 4) {
printf("Usage: connections host port\n");
if (argc != 5 && argc != 4) {
printf("Usage: connections host port [pipeline factor] \n");
return 0;
}

if (argc == 5) {
pipeline = atoi(argv[4]);
printf("Using pipeline factor of %d\n", pipeline);
}
/* Pipeline to 16 */
request_size = pipeline * (sizeof(request_template) - 1);
printf("request size %d\n", request_size);
request = malloc(request_size);
for (int i = 0; i < pipeline; i++) {
memcpy(request + i * (sizeof(request_template) - 1), request_template, sizeof(request_template) - 1);
}

port = atoi(argv[3]);
host = malloc(strlen(argv[2]) + 1);
memcpy(host, argv[2], strlen(argv[2]) + 1);
Expand Down

0 comments on commit 9e1ccc7

Please sign in to comment.