From: Michael Tremer Date: Thu, 19 Sep 2024 09:08:21 +0000 (+0000) Subject: stats: Move them into their own file X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=b5e6c34b5fe9551b102c8d8ccb49b9fe01bc537f;p=fireperf.git stats: Move them into their own file Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 3a65503..cda5137 100644 --- a/Makefile.am +++ b/Makefile.am @@ -73,6 +73,8 @@ fireperf_SOURCES = \ src/random.h \ src/server.c \ src/server.h \ + src/stats.c \ + src/stats.h \ src/util.c \ src/util.h \ src/worker.c \ diff --git a/src/main.c b/src/main.c index a7468be..6ab10e2 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "client.h" @@ -34,7 +33,7 @@ #include "logging.h" #include "random.h" #include "server.h" -#include "util.h" +#include "stats.h" static int set_limits(struct fireperf_ctx* ctx) { struct rlimit limit; @@ -144,70 +143,6 @@ ERROR: return r; } -int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode) { - struct timespec now; - - // Fetch the time - int r = clock_gettime(CLOCK_REALTIME, &now); - if (r) { - ERROR(ctx, "Could not fetch the time: %s\n", strerror(errno)); - return 1; - } - - double delta = timespec_delta(&now, &stats->last_printed); - - // Called too soon again? - if (delta < 0.1) - return 0; - - // Format timestamp - const char* timestamp = format_timespec(&now); - - INFO(ctx, "--- %s -------------------------\n", timestamp); - INFO(ctx, " : %12s %12s\n", "RX", "TX"); - INFO(ctx, " %-20s: %25u\n", "Open Connection(s)", stats->open_connections); - INFO(ctx, " %-20s: %23.2f/s\n", "New Connections", stats->connections / delta); - - // Show current bandwidth - char* bps_received = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS); - char* bps_sent = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS); - - if (bps_received || bps_sent) { - INFO(ctx, " %-20s: %10s/s %10s/s\n", "Current Bandwidth", bps_received, bps_sent); - - if (bps_received) - free(bps_received); - if (bps_sent) - free(bps_sent); - } - - // Total bytes - char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES); - char* total_bytes_sent = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES); - - if (total_bytes_received || total_bytes_sent) { - INFO(ctx, " %-20s: %12s %12s\n", "Total Bytes", total_bytes_received, total_bytes_sent); - - if (total_bytes_received) - free(total_bytes_received); - if (total_bytes_sent) - free(total_bytes_sent); - } - - // Empty line - INFO(ctx, "\n"); - - // Remember when this was printed last - stats->last_printed = now; - - // Reset statistics - stats->connections = 0; - stats->bytes_received = 0; - stats->bytes_sent = 0; - - return 0; -} - int set_socket_buffer_sizes(struct fireperf_ctx* ctx, int fd) { int r; diff --git a/src/main.h b/src/main.h index 203d9f1..f469144 100644 --- a/src/main.h +++ b/src/main.h @@ -21,28 +21,8 @@ #ifndef FIREPERF_MAIN_H #define FIREPERF_MAIN_H -#include - #include "ctx.h" - -// 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; -}; - -int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode); +#include "stats.h" int set_socket_buffer_sizes(struct fireperf_ctx* ctx, int fd); diff --git a/src/stats.c b/src/stats.c new file mode 100644 index 0000000..d7a80f5 --- /dev/null +++ b/src/stats.c @@ -0,0 +1,93 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include +#include + +#include "ctx.h" +#include "logging.h" +#include "stats.h" +#include "util.h" + +int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode) { + struct timespec now; + + // Fetch the time + int r = clock_gettime(CLOCK_REALTIME, &now); + if (r) { + ERROR(ctx, "Could not fetch the time: %s\n", strerror(errno)); + return 1; + } + + double delta = timespec_delta(&now, &stats->last_printed); + + // Called too soon again? + if (delta < 0.1) + return 0; + + // Format timestamp + const char* timestamp = format_timespec(&now); + + INFO(ctx, "--- %s -------------------------\n", timestamp); + INFO(ctx, " : %12s %12s\n", "RX", "TX"); + INFO(ctx, " %-20s: %25u\n", "Open Connection(s)", stats->open_connections); + INFO(ctx, " %-20s: %23.2f/s\n", "New Connections", stats->connections / delta); + + // Show current bandwidth + char* bps_received = format_size(stats->bytes_received * 8 / delta, FIREPERF_FORMAT_BITS); + char* bps_sent = format_size(stats->bytes_sent * 8 / delta, FIREPERF_FORMAT_BITS); + + if (bps_received || bps_sent) { + INFO(ctx, " %-20s: %10s/s %10s/s\n", "Current Bandwidth", bps_received, bps_sent); + + if (bps_received) + free(bps_received); + if (bps_sent) + free(bps_sent); + } + + // Total bytes + char* total_bytes_received = format_size(stats->total_bytes_received, FIREPERF_FORMAT_BYTES); + char* total_bytes_sent = format_size(stats->total_bytes_sent, FIREPERF_FORMAT_BYTES); + + if (total_bytes_received || total_bytes_sent) { + INFO(ctx, " %-20s: %12s %12s\n", "Total Bytes", total_bytes_received, total_bytes_sent); + + if (total_bytes_received) + free(total_bytes_received); + if (total_bytes_sent) + free(total_bytes_sent); + } + + // Empty line + INFO(ctx, "\n"); + + // Remember when this was printed last + stats->last_printed = now; + + // Reset statistics + stats->connections = 0; + stats->bytes_received = 0; + stats->bytes_sent = 0; + + return 0; +} diff --git a/src/stats.h b/src/stats.h new file mode 100644 index 0000000..21c2449 --- /dev/null +++ b/src/stats.h @@ -0,0 +1,47 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef FIREPERF_STATS_H +#define FIREPERF_STATS_H + +#include + +// 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; +}; + +#include "ctx.h" + +int fireperf_dump_stats(struct fireperf_ctx* ctx, struct fireperf_stats* stats, int mode); + +#endif /* FIREPERF_STATS_H */