UNPKG

2.08 kBtext/x-cView Raw
1/* This provides a crude manner of testing the performance of a broker in messages/s. */
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <sys/time.h>
7#include <unistd.h>
8#include <mosquitto.h>
9
10#include <msgsps_common.h>
11
12static bool run = true;
13static int message_count = 0;
14static struct timeval start, stop;
15FILE *fptr = NULL;
16
17
18void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
19{
20 printf("rc: %d\n", rc);
21}
22
23void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
24{
25 run = false;
26}
27
28void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
29{
30 if(message_count == 0){
31 gettimeofday(&start, NULL);
32 }
33 fwrite(msg->payload, sizeof(uint8_t), msg->payloadlen, fptr);
34 message_count++;
35 if(message_count == MESSAGE_COUNT){
36 gettimeofday(&stop, NULL);
37 mosquitto_disconnect((struct mosquitto *)obj);
38 }
39}
40
41int main(int argc, char *argv[])
42{
43 struct mosquitto *mosq;
44 double dstart, dstop, diff;
45 int mid = 0;
46 char id[50];
47 int rc;
48
49 start.tv_sec = 0;
50 start.tv_usec = 0;
51 stop.tv_sec = 0;
52 stop.tv_usec = 0;
53
54 fptr = fopen("msgsps_sub.dat", "wb");
55 if(!fptr){
56 printf("Error: Unable to write to msgsps_sub.dat.\n");
57 return 1;
58 }
59 mosquitto_lib_init();
60
61 snprintf(id, 50, "msgps_sub_%d", getpid());
62 mosq = mosquitto_new(id, true, NULL);
63 mosquitto_connect_callback_set(mosq, my_connect_callback);
64 mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
65 mosquitto_message_callback_set(mosq, my_message_callback);
66
67 mosquitto_connect(mosq, "127.0.0.1", 1884, 600);
68 mosquitto_subscribe(mosq, &mid, "perf/test", 0);
69
70 do{
71 rc = mosquitto_loop(mosq, 1, 10);
72 }while(rc == MOSQ_ERR_SUCCESS && run);
73 printf("rc: %d\n", rc);
74
75 dstart = (double)start.tv_sec*1.0e6 + (double)start.tv_usec;
76 dstop = (double)stop.tv_sec*1.0e6 + (double)stop.tv_usec;
77 diff = (dstop-dstart)/1.0e6;
78
79 printf("Start: %g\nStop: %g\nDiff: %g\nMessages/s: %g\n", dstart, dstop, diff, (double)MESSAGE_COUNT/diff);
80
81 mosquitto_destroy(mosq);
82 mosquitto_lib_cleanup();
83 fclose(fptr);
84
85 return 0;
86}