]> git.ipfire.org Git - fireperf.git/commitdiff
stats: Merge client and server stats
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:35:05 +0000 (14:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:35:05 +0000 (14:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/client.h
src/main.c
src/main.h
src/server.c
src/server.h

index b149c208fd52739b1c351a549248d883b26632c3..57a0d1ff83b132acc2a70481445b9e6fa5c7a01b 100644 (file)
 #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;
                                }
index a9c6f7004a529f154c6b910a7bd6e4fb1ebb6536..28014752d340be0f23fdb9277732b9e055be42b1 100644 (file)
@@ -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 */
index 5aeeae8ffc1c345a04ab4f12cd25702b0acb03a1..c6b8e6dfc9d5649a044ef865c23f7d3b781f2f89 100644 (file)
@@ -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");
index 410710749ce1a11a795ab553b52a1b5786ca169f..bb736aacf141efbc6bd5cf5026964f23d59d66bb 100644 (file)
@@ -22,6 +22,7 @@
 #define FIREPERF_MAIN_H
 
 #include <netinet/in.h>
+#include <time.h>
 
 #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 */
index 2693e6c254d4247b7036395213d23df46daec5d1..9133a0d1624debaa3f3490aa60ce59d78bc86a3f 100644 (file)
 
 #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;
                                }
index b6984c0542568bd9f7eb09eddd02e388377db8f0..cd400901261b2ac08637a4d3a2770d9f2ed33444 100644 (file)
@@ -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 */