]> git.ipfire.org Git - fireperf.git/commitdiff
client: Print current bandwidth and total bytes sent
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:12:18 +0000 (14:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Feb 2021 14:12:45 +0000 (14:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c

index ffbf11521ad9c0922d47e34508ad0f9041ee092d..a6f158513073ea9fd22f38f9bda811be853b7e05 100644 (file)
@@ -38,6 +38,9 @@ struct fireperf_client_stats {
 
        // Total number of open connections
        unsigned int connections;
+
+       size_t bytes_sent;
+       size_t total_bytes_sent;
 };
 
 // Set to one when the timeout has expired
@@ -123,11 +126,29 @@ static int dump_stats(struct fireperf_config* conf, struct fireperf_client_stats
        // Format timestamp
        const char* timestamp = format_timespec(&now);
 
-       INFO( conf, "--- %s --------------------\n", timestamp);
+       // Format total bytes sent
+       char* total_bytes_sent = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES);
+
+       // Calculate bandwidth
+       char* bps = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS);
+
+       INFO(conf, "--- %s --------------------\n", timestamp);
+       INFO(conf, "  %-20s: %18s/s\n", "Current Bandwidth", bps);
+       INFO(conf, "  %-20s: %20s\n", "Total Bytes Sent", total_bytes_sent);
 
        // Remember when this was printed last
        stats->last_printed = now;
 
+       // Reset statistics
+       stats->bytes_sent = 0;
+
+       // Cleanup
+       if (bps)
+               free(bps);
+
+       if (total_bytes_sent)
+               free(total_bytes_sent);
+
        return 0;
 }
 
@@ -222,8 +243,8 @@ ERROR:
        return -1;
 }
 
-static int send_data_to_server(struct fireperf_config* conf, int fd,
-               struct fireperf_random_pool* pool) {
+static int send_data_to_server(struct fireperf_config* conf,
+               struct fireperf_client_stats* stats, int fd, struct fireperf_random_pool* pool) {
        const char* buffer = ZERO;
        ssize_t bytes_sent;
 
@@ -237,6 +258,10 @@ static int send_data_to_server(struct fireperf_config* conf, int fd,
 
        DEBUG(conf, "bytes_sent = %zu\n", bytes_sent);
 
+       // Update statistics
+       stats->bytes_sent += bytes_sent;
+       stats->total_bytes_sent += bytes_sent;
+
        return 0;
 }
 
@@ -341,7 +366,7 @@ int fireperf_client(struct fireperf_config* conf, int epollfd, int timerfd) {
                                        stats.connections--;
 
                                } else if (events[i].events & EPOLLOUT) {
-                                       r = send_data_to_server(conf, fd, pool);
+                                       r = send_data_to_server(conf, &stats, fd, pool);
                                        if (r)
                                                goto ERROR;
                                }