msgsps_sub.c 906 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* This provides a crude manner of testing the performance of a broker in messages/s. */
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7. #include <mosquitto.h>
  8. #include <stdatomic.h>
  9. #include <msgsps_common.h>
  10. static atomic_int message_count = 0;
  11. void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  12. {
  13. message_count++;
  14. }
  15. int main(int argc, char *argv[])
  16. {
  17. struct mosquitto *mosq;
  18. int c;
  19. mosquitto_lib_init();
  20. mosq = mosquitto_new(NULL, true, NULL);
  21. mosquitto_message_callback_set(mosq, my_message_callback);
  22. mosquitto_connect(mosq, HOST, PORT, 600);
  23. mosquitto_subscribe(mosq, NULL, "perf/test", SUB_QOS);
  24. mosquitto_loop_start(mosq);
  25. while(1){
  26. sleep(1);
  27. c = message_count;
  28. message_count = 0;
  29. printf("%d\n", c);
  30. }
  31. mosquitto_destroy(mosq);
  32. mosquitto_lib_cleanup();
  33. return 0;
  34. }