From: Michael Tremer Date: Thu, 4 Feb 2021 14:12:18 +0000 (+0000) Subject: client: Print current bandwidth and total bytes sent X-Git-Tag: 0.1.0~8 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=dbc0eeb3d29116bea201f523c65c00ccb736d322;p=fireperf.git client: Print current bandwidth and total bytes sent Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index ffbf115..a6f1585 100644 --- a/src/client.c +++ b/src/client.c @@ -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; }