From: Michael Tremer Date: Thu, 4 Feb 2021 14:35:05 +0000 (+0000) Subject: stats: Merge client and server stats X-Git-Tag: 0.1.0~5 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=c10e809c1be6cfc7981f28d57981b48ccba11b0b;p=fireperf.git stats: Merge client and server stats Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index b149c20..57a0d1f 100644 --- a/src/client.c +++ b/src/client.c @@ -32,20 +32,6 @@ #include "main.h" #include "util.h" -// Struct to collect client statistics -struct fireperf_client_stats { - struct timespec last_printed; - - // Total number of open connections - unsigned int open_connections; - - // New connections - unsigned int connections; - - size_t bytes_sent; - size_t total_bytes_sent; -}; - // Set to one when the timeout has expired static int timeout_expired = 0; @@ -110,7 +96,7 @@ static const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* p return pool->data + offset; } -static int dump_stats(struct fireperf_config* conf, struct fireperf_client_stats* stats) { +static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) { struct timespec now; // Fetch the time @@ -249,7 +235,7 @@ ERROR: } static int send_data_to_server(struct fireperf_config* conf, - struct fireperf_client_stats* stats, int fd, struct fireperf_random_pool* pool) { + struct fireperf_stats* stats, int fd, struct fireperf_random_pool* pool) { const char* buffer = ZERO; ssize_t bytes_sent; @@ -270,8 +256,8 @@ static int send_data_to_server(struct fireperf_config* conf, return 0; } -int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) { - struct fireperf_client_stats stats = { 0 }; +int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, + int epollfd, int timerfd) { struct fireperf_random_pool* pool = NULL; DEBUG(conf, "Launching " PACKAGE_NAME " in client mode\n"); @@ -310,7 +296,7 @@ int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) { while (!conf->terminated && !timeout_expired) { // Open connections - while (stats.open_connections < conf->parallel) { + while (stats->open_connections < conf->parallel) { int fd = open_connection(conf); if (fd < 0) continue; @@ -323,8 +309,8 @@ int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) { goto ERROR; } - stats.open_connections++; - stats.connections++; + stats->open_connections++; + stats->connections++; } int fds = epoll_wait(epollfd, events, EPOLL_MAX_EVENTS, -1); @@ -353,7 +339,7 @@ int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) { goto ERROR; } - r = dump_stats(conf, &stats); + r = dump_stats(conf, stats); if (r) goto ERROR; @@ -369,10 +355,10 @@ int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) { close(fd); - stats.open_connections--; + stats->open_connections--; } else if (events[i].events & EPOLLOUT) { - r = send_data_to_server(conf, &stats, fd, pool); + r = send_data_to_server(conf, stats, fd, pool); if (r) goto ERROR; } diff --git a/src/client.h b/src/client.h index a9c6f70..2801475 100644 --- a/src/client.h +++ b/src/client.h @@ -23,6 +23,7 @@ #include "main.h" -int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd); +int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, + int epollfd, int timerfd); #endif /* FIREPERF_CLIENT_H */ diff --git a/src/main.c b/src/main.c index 5aeeae8..c6b8e6d 100644 --- a/src/main.c +++ b/src/main.c @@ -245,6 +245,7 @@ int main(int argc, char* argv[]) { .parallel = DEFAULT_PARALLEL, .timeout = DEFAULT_TIMEOUT, }; + struct fireperf_stats stats = { 0 }; int r; // Parse command line @@ -302,10 +303,10 @@ int main(int argc, char* argv[]) { switch (conf.mode) { case FIREPERF_MODE_CLIENT: - return fireperf_client(&conf, epollfd, timerfd); + return fireperf_client(&conf, &stats, epollfd, timerfd); case FIREPERF_MODE_SERVER: - return fireperf_server(&conf, epollfd, timerfd); + return fireperf_server(&conf, &stats, epollfd, timerfd); case FIREPERF_MODE_NONE: fprintf(stderr, "No mode selected\n"); diff --git a/src/main.h b/src/main.h index 4107107..bb736aa 100644 --- a/src/main.h +++ b/src/main.h @@ -22,6 +22,7 @@ #define FIREPERF_MAIN_H #include +#include #define DEFAULT_KEEPALIVE_COUNT 3 #define DEFAULT_KEEPALIVE_INTERVAL 10 @@ -62,4 +63,21 @@ struct fireperf_config { int zero; }; +// Struct to collect statistics +struct fireperf_stats { + struct timespec last_printed; + + // Total number of open connections + unsigned int open_connections; + + // New connections + unsigned int connections; + + // Total transferred data + size_t bytes_received; + size_t total_bytes_received; + size_t bytes_sent; + size_t total_bytes_sent; +}; + #endif /* FIREPERF_MAIN_H */ diff --git a/src/server.c b/src/server.c index 2693e6c..9133a0d 100644 --- a/src/server.c +++ b/src/server.c @@ -33,21 +33,7 @@ #define SOCKET_BACKLOG 1024 -// Struct to collect server statistics -struct fireperf_server_stats { - struct timespec last_printed; - - // Total number of open connections - unsigned int open_connections; - - // New connections - unsigned int connections; - - size_t bytes_received; - size_t total_bytes_received; -}; - -static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats* stats) { +static int dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats) { struct timespec now; // Fetch the time @@ -166,7 +152,7 @@ static int accept_connection(struct fireperf_config* conf, int sockfd) { } static int handle_io_on_connection(struct fireperf_config* conf, - struct fireperf_server_stats* stats, int fd) { + struct fireperf_stats* stats, int fd) { char buffer[SOCKET_RECV_BUFFER_SIZE]; ssize_t bytes_read; @@ -199,9 +185,8 @@ static int is_listening_socket(struct fireperf_config* conf, int* sockets, int f return 0; } -int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) { - struct fireperf_server_stats stats = { 0 }; - +int fireperf_server(struct fireperf_config* conf, struct fireperf_stats* stats, + int epollfd, int timerfd) { DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n"); int listening_sockets[conf->listening_sockets]; @@ -262,8 +247,8 @@ int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) { } // A connection has been opened - stats.open_connections++; - stats.connections++; + stats->open_connections++; + stats->connections++; // Handle timer events } else if (fd == timerfd) { @@ -276,7 +261,7 @@ int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) { goto ERROR; } - r = dump_stats(conf, &stats); + r = dump_stats(conf, stats); if (r) goto ERROR; @@ -295,7 +280,7 @@ int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) { close(fd); // This connection is now closed - stats.open_connections--; + stats->open_connections--; // Skip processing anything else, because it would be pointless continue; @@ -303,7 +288,7 @@ int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd) { // One of the connections had IO if (events[i].events & EPOLLIN) { - r = handle_io_on_connection(conf, &stats, fd); + r = handle_io_on_connection(conf, stats, fd); if (r < 0) goto ERROR; } diff --git a/src/server.h b/src/server.h index b6984c0..cd40090 100644 --- a/src/server.h +++ b/src/server.h @@ -23,6 +23,7 @@ #include "main.h" -int fireperf_server(struct fireperf_config* conf, int epollfd, int timerfd); +int fireperf_server(struct fireperf_config* conf, struct fireperf_stats* stats, + int epollfd, int timerfd); #endif /* FIREPERF_SERVER_H */