8000 [pull] master from uNetworking:master by pull[bot] · Pull Request #10 · SINHASantos/uSockets · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[pull] master from uNetworking:master #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions examples/http_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ 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_template_post[] = "POST / HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\nContent-Length: 10\r\n\r\n{\"key\":13}";

char *request;
int request_size;
char *host;
int port;
int connections;

int responses;
int pipeline = 1;
int is_post = 0;

struct http_socket {
/* How far we have streamed our request */
Expand All @@ -37,7 +43,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 +61,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 +76,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 +100,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 +117,34 @@ 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 && argc != 6) {
printf("Usage: connections host port [pipeline factor] [with body]\n");
return 0;
}

if (argc >= 5) {
pipeline = atoi(argv[4]);
printf("Using pipeline factor of %d\n", pipeline);
10000 }

const char *selected_request = request_template;
int selected_request_size = sizeof(request_template) - 1;

if (argc >= 6) {
is_post = atoi(argv[5]);
printf("Using post with body\n");

selected_request = request_template_post;
selected_request_size = sizeof(request_template_post) - 1;
}
/* Pipeline to 16 */
request_size = pipeline * selected_request_size;
printf("request size %d\n", request_size);
request = malloc(request_size);
for (int i = 0; i < pipeline; i++) {
memcpy(request + i * selected_request_size, selected_request, selected_request_size);
}

port = atoi(argv[3]);
host = malloc(strlen(argv[2]) + 1);
memcpy(host, argv[2], strlen(argv[2]) + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct us_listen_socket_t {
unsigned int socket_ext_size;
};

/* Listen sockets are keps in their own list */
/* Listen sockets are kept in their own list */
void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);
void us_internal_socket_context_unlink_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);

Expand Down
4 changes: 2 additions & 2 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int us_socket_is_established(int ssl, struct us_socket_t *s) {
return us_internal_poll_type((struct us_poll_t *) s) != POLL_TYPE_SEMI_SOCKET;
}

/* Exactly the same as us_socket_close but does not emit on_close event */
/* Exactly the same as us_socket_close but does not check priority or emit on_close event */
struct us_socket_t *us_socket_close_connecting(int ssl, struct us_socket_t *s) {
if (!us_socket_is_closed(0, s)) {
us_internal_socket_context_unlink_socket(s->context, s);
Expand All @@ -112,7 +112,7 @@ struct us_socket_t *us_socket_close_connecting(int ssl, struct us_socket_t *s) {
return s;
}

/* Same as above but emits on_close */
/* Same as above but check priority and emits on_close */
struct us_socket_t *us_socket_close(int ssl, struct us_socket_t *s, int code, void *reason) {
if (!us_socket_is_closed(0, s)) {
if (s->low_prio_state == 1) {
Expand Down
Loading
0