Skip to content

Commit 9e1ccc7

Browse files
committed
Support benchmarking pipelined http requests
1 parent 89db170 commit 9e1ccc7

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

examples/http_load_test.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ const int SSL = 1;
77
#include <stdlib.h>
88
#include <string.h>
99

10-
char request[] = "GET / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n";
10+
char request_template[] = "GET / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n";
11+
char *request;
12+
int request_size;
1113
char *host;
1214
int port;
1315
int connections;
1416

1517
int responses;
18+
int pipeline = 1;
1619

1720
struct http_socket {
1821
/* How far we have streamed our request */
@@ -37,7 +40,7 @@ struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
3740
struct http_socket *http_socket = (struct http_socket *) us_socket_ext(SSL, s);
3841

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

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

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

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

7275
/* Send a request */
73-
us_socket_write(SSL, s, request, sizeof(request) - 1, 0);
76+
us_socket_write(SSL, s, request, request_size, 0);
7477

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

9598
struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
9699
/* Print current statistics */
97-
printf("Req/sec: %f\n", ((float)responses) / LIBUS_TIMEOUT_GRANULARITY);
100+
printf("Req/sec: %f\n", ((float)pipeline) * ((float)responses) / LIBUS_TIMEOUT_GRANULARITY);
98101

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

113116
/* Parse host and port */
114-
if (argc != 4) {
115-
printf("Usage: connections host port\n");
117+
if (argc != 5 && argc != 4) {
118+
printf("Usage: connections host port [pipeline factor] \n");
116119
return 0;
117120
}
118121

122+
if (argc == 5) {
123+
pipeline = atoi(argv[4]);
124+
printf("Using pipeline factor of %d\n", pipeline);
125+
}
126+
/* Pipeline to 16 */
127+
request_size = pipeline * (sizeof(request_template) - 1);
128+
printf("request size %d\n", request_size);
129+
request = malloc(request_size);
130+
for (int i = 0; i < pipeline; i++) {
131+
memcpy(request + i * (sizeof(request_template) - 1), request_template, sizeof(request_template) - 1);
132+
}
133+
119134
port = atoi(argv[3]);
120135
host = malloc(strlen(argv[2]) + 1);
121136
memcpy(host, argv[2], strlen(argv[2]) + 1);

0 commit comments

Comments
 (0)