msgsps_pub.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <stdlib.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_publish_callback(struct mosquitto *mosq, void *obj, int mid)
  12. {
  13. message_count++;
  14. }
  15. int main(int argc, char *argv[])
  16. {
  17. struct mosquitto *mosq;
  18. int i;
  19. uint8_t buf[MESSAGE_SIZE];
  20. mosquitto_lib_init();
  21. mosq = mosquitto_new(NULL, true, NULL);
  22. mosquitto_publish_callback_set(mosq, my_publish_callback);
  23. mosquitto_connect(mosq, HOST, PORT, 600);
  24. mosquitto_loop_start(mosq);
  25. i=0;
  26. while(1){
  27. mosquitto_publish(mosq, NULL, "perf/test", sizeof(buf), buf, PUB_QOS, false);
  28. usleep(100);
  29. i++;
  30. if(i == 10000){
  31. /* Crude "messages per second" count */
  32. i = message_count;
  33. message_count = 0;
  34. printf("%d\n", i);
  35. i = 0;
  36. }
  37. }
  38. mosquitto_loop_stop(mosq, false);
  39. mosquitto_destroy(mosq);
  40. mosquitto_lib_cleanup();
  41. return 0;
  42. }