Skip to content

Commit 55134b4

Browse files
committed
mosquitto_rr: Add --latency option
1 parent 90d2721 commit 55134b4

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

ChangeLog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Clients:
194194
- mosquitto_sub payload hex output can now be split by fixed field length.
195195
- Add `--message-rate` option to mosquitto_sub, for printing the count of
196196
messages received each second.
197+
- Add `--latency` option to mosquitto_rr, for printing the request/response
198+
latency.
197199
- Remove support for TLS v1.1.
198200

199201
DB Dump:

client/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ endif()
5454

5555
target_link_libraries(mosquitto_pub PRIVATE client-common)
5656
target_link_libraries(mosquitto_sub PRIVATE client-common)
57-
target_link_libraries(mosquitto_rr PRIVATE client-common)
57+
target_link_libraries(mosquitto_rr PRIVATE client-common libmosquitto_common)
5858

5959
install(TARGETS mosquitto_pub RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
6060
install(TARGETS mosquitto_sub RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")

client/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ endif
1010
LOCAL_CFLAGS+=
1111
LOCAL_CPPFLAGS+=
1212
LOCAL_LDFLAGS+=
13-
LOCAL_LDADD+=-lcjson ${SHARED_DEP}
14-
STATIC_LDADD+=-lcjson
13+
LOCAL_LDADD+=-lcjson ${SHARED_DEP} ${LIBMOSQ_COMMON}
14+
STATIC_LDADD+=-lcjson ${LIBMOSQ_COMMON}
1515

1616
ifeq ($(WITH_SOCKS),yes)
1717
LOCAL_CPPFLAGS+=-DWITH_SOCKS

client/client_shared.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,12 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
831831
}else{
832832
cfg->pub_mode = MSGMODE_STDIN_LINE;
833833
}
834+
}else if(!strcmp(argv[i], "--latency")){
835+
if(pub_or_sub != CLIENT_RR){
836+
goto unknown_option;
837+
}
838+
cfg->measure_latency = true;
839+
cfg->tcp_nodelay = true; /* Remove influence of nagle */
834840
}else if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--message")){
835841
if(pub_or_sub == CLIENT_SUB){
836842
goto unknown_option;

client/client_shared.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct mosq_config {
143143
bool tcp_nodelay;
144144
bool no_tls;
145145
bool message_rate; /* sub */
146+
bool measure_latency; /* rr */
146147
};
147148

148149
extern const char hexseplist[32];

client/rr_client.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ int msg_count = 0;
5353
struct mosquitto *g_mosq = NULL;
5454
static bool timed_out = false;
5555
static int connack_result = 0;
56+
static struct timespec publish_send_time;
57+
static struct timespec publish_recv_time;
5658

5759
#ifndef WIN32
5860
static void my_signal_handler(int signum)
@@ -68,6 +70,8 @@ static void my_signal_handler(int signum)
6870

6971
int my_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, void *payload, int qos, bool retain)
7072
{
73+
mosquitto_time_ns(&publish_send_time.tv_sec, &publish_send_time.tv_nsec);
74+
7175
if(cfg.protocol_version < MQTT_PROTOCOL_V5){
7276
return mosquitto_publish_v5(mosq, mid, topic, payloadlen, payload, qos, retain, NULL);
7377
}else{
@@ -85,6 +89,8 @@ static void my_message_callback(struct mosquitto *mosq, void *obj, const struct
8589
if(process_messages == false) return;
8690
if(message->retain && cfg.no_retain) return;
8791

92+
mosquitto_time_ns(&publish_recv_time.tv_sec, &publish_recv_time.tv_nsec);
93+
8894
print_message(&cfg, message, properties);
8995

9096
switch(cfg.pub_mode){
@@ -267,6 +273,33 @@ static void print_usage(void)
267273
printf("\nSee https://mosquitto.org/ for more information.\n\n");
268274
}
269275

276+
static void report_latency(void)
277+
{
278+
if(cfg.measure_latency){
279+
time_t s = publish_recv_time.tv_sec - publish_send_time.tv_sec;
280+
long ns = publish_recv_time.tv_nsec - publish_send_time.tv_nsec;
281+
282+
if(s < 0){
283+
if(ns < 0){
284+
s++;
285+
ns -= 1000000000;
286+
}
287+
}
288+
289+
if(s > 0){
290+
printf("Latency: %ld.%09ld\n", s, ns);
291+
}else{
292+
if(ns < 1000){
293+
printf("Latency: %ldns\n", ns);
294+
}else if(ns < 1000000){
295+
printf("Latency: %fµs\n", ((double)ns)/1000.0);
296+
}else{
297+
printf("Latency: %fms\n", ((double)ns)/1000000.0);
298+
}
299+
}
300+
}
301+
}
302+
270303
int main(int argc, char *argv[])
271304
{
272305
int rc;
@@ -385,6 +418,8 @@ int main(int argc, char *argv[])
385418
}
386419
}while(rc == MOSQ_ERR_SUCCESS && client_state != rr_s_disconnect);
387420

421+
report_latency();
422+
388423
mosquitto_destroy(g_mosq);
389424
mosquitto_lib_cleanup();
390425

man/mosquitto_rr.1.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<cmdsynopsis>
6464
<command>misc-options:</command>
6565
<sbr/>
66+
<arg><option>--latency</option></arg>
6667
<arg><option>-W</option> <replaceable>message-processing-timeout</replaceable></arg>
6768
</cmdsynopsis>
6869
<cmdsynopsis>
@@ -424,6 +425,28 @@
424425
</para>
425426
</listitem>
426427
</varlistentry>
428+
<varlistentry>
429+
<term><option>--latency</option></term>
430+
<listitem>
431+
<para>
432+
If this option is specified, mosquitto_rr will print out
433+
the latency between it starting to publish a request and
434+
the response arriving. This number includes both the
435+
broker and client processing times, as well as any
436+
inherent network latency.
437+
</para>
438+
439+
<para>
440+
This can be used to measure message delivery latency
441+
very simply by specifying an identical request and
442+
response topic.
443+
</para>
444+
<para>
445+
The <option>--nodelay</option> option will be automatically
446+
used when <option>--latency</option> is in use.
447+
</para>
448+
</listitem>
449+
</varlistentry>
427450
<varlistentry>
428451
<term><option>-m</option></term>
429452
<term><option>--message</option></term>

0 commit comments

Comments
 (0)