From dcbe922ed067378c2b5e00dfb6eb68d5f910a28f Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 28 Mar 2025 10:50:17 +0000 Subject: [PATCH] Add `--message-rate` option to mosquitto_sub When active, this prints the count of messages received each second. --- ChangeLog.txt | 2 ++ client/client_shared.c | 5 +++++ client/client_shared.h | 1 + client/sub_client.c | 25 ++++++++++++++++++++++++- man/mosquitto_sub.1.xml | 12 ++++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 6a531236..d9b762e1 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -190,6 +190,8 @@ Clients: - Add `--tls-keylog` option which can be used to generate a file that can be used by wireshark to decrypt TLS traffic for debugging purposes. - mosquitto_sub payload hex output can now be split by fixed field length. +- Add `--message-rate` option to mosquitto_sub, for printing the count of + messages received each second. DB Dump: - Add `--json` output mode. diff --git a/client/client_shared.c b/client/client_shared.c index f7d2a31b..1f874b71 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -869,6 +869,11 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c cfg->max_inflight = (unsigned int )tmpi; } i++; + }else if(!strcmp(argv[i], "--message-rate")){ + if(pub_or_sub != CLIENT_SUB){ + goto unknown_option; + } + cfg->message_rate = true; }else if(!strcmp(argv[i], "--nodelay")){ cfg->tcp_nodelay = true; }else if(!strcmp(argv[i], "--no-tls")){ diff --git a/client/client_shared.h b/client/client_shared.h index 1e4ecc30..0e9b699c 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -142,6 +142,7 @@ struct mosq_config { bool have_topic_alias; /* pub */ bool tcp_nodelay; bool no_tls; + bool message_rate; /* sub */ }; extern const char hexseplist[32]; diff --git a/client/sub_client.c b/client/sub_client.c index 002c1d21..aac45199 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -37,8 +37,10 @@ Contributors: #include "sub_client_output.h" struct mosq_config cfg; +static bool run = true; bool process_messages = true; int msg_count = 0; +int message_rate_msg_count = 0; struct mosquitto *g_mosq = NULL; int last_mid = 0; static bool timed_out = false; @@ -76,6 +78,7 @@ static void my_signal_handler(int signum) }else{ exit(-1); } + run = false; } if(signum == SIGALRM){ timed_out = true; @@ -92,6 +95,8 @@ static void my_message_callback(struct mosquitto *mosq, void *obj, const struct UNUSED(obj); UNUSED(properties); + message_rate_msg_count++; + if(process_messages == false) return; if(cfg.retained_only && !message->retain && process_messages){ @@ -393,6 +398,10 @@ int main(int argc, char *argv[]) if(cfg.debug){ mosquitto_log_callback_set(g_mosq, my_log_callback); } + if(cfg.message_rate){ + process_messages = false; + cfg.watch = false; + } mosquitto_subscribe_callback_set(g_mosq, my_subscribe_callback); mosquitto_connect_v5_callback_set(g_mosq, my_connect_callback); mosquitto_message_v5_callback_set(g_mosq, my_message_callback); @@ -436,7 +445,21 @@ int main(int argc, char *argv[]) } #endif - rc = mosquitto_loop_forever(g_mosq, -1, 1); + if(cfg.message_rate){ + rc = mosquitto_loop_start(g_mosq); + if(rc){ + return rc; + } + while(run){ + struct timespec ts = {1,0}; + nanosleep(&ts, NULL); + int message_count = message_rate_msg_count; + message_rate_msg_count = 0; + printf("%d msgs/s\n", message_count); + } + }else{ + rc = mosquitto_loop_forever(g_mosq, -1, 1); + } mosquitto_destroy(g_mosq); mosquitto_lib_cleanup(); diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 44c32721..f1aded01 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -43,6 +43,7 @@ client-id client-id-prefix keepalive-time + @@ -431,6 +432,17 @@ + + + + + Instead of printing the messages received, print a count + of the messages received at one second intervals. Other + options related to output formatting are not valid when + this option is active. + + +