amqp_producer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <rabbitmq-c/amqp.h>
  8. #include <rabbitmq-c/tcp_socket.h>
  9. #include "utils.h"
  10. #define SUMMARY_EVERY_US 1000000
  11. static void send_batch(amqp_connection_state_t conn, char const *queue_name,
  12. int rate_limit, int message_count) {
  13. uint64_t start_time = now_microseconds();
  14. int i;
  15. int sent = 0;
  16. int previous_sent = 0;
  17. uint64_t previous_report_time = start_time;
  18. uint64_t next_summary_time = start_time + SUMMARY_EVERY_US;
  19. char message[256];
  20. amqp_bytes_t message_bytes;
  21. for (i = 0; i < (int)sizeof(message); i++) {
  22. message[i] = i & 0xff;
  23. }
  24. message_bytes.len = sizeof(message);
  25. message_bytes.bytes = message;
  26. for (i = 0; i < message_count; i++) {
  27. uint64_t now = now_microseconds();
  28. die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes("amq.direct"),
  29. amqp_cstring_bytes(queue_name), 0, 0, NULL,
  30. message_bytes),
  31. "Publishing");
  32. sent++;
  33. if (now > next_summary_time) {
  34. int countOverInterval = sent - previous_sent;
  35. double intervalRate =
  36. countOverInterval / ((now - previous_report_time) / 1000000.0);
  37. printf("%d ms: Sent %d - %d since last report (%d Hz)\n",
  38. (int)(now - start_time) / 1000, sent, countOverInterval,
  39. (int)intervalRate);
  40. previous_sent = sent;
  41. previous_report_time = now;
  42. next_summary_time += SUMMARY_EVERY_US;
  43. }
  44. while (((i * 1000000.0) / (now - start_time)) > rate_limit) {
  45. microsleep(2000);
  46. now = now_microseconds();
  47. }
  48. }
  49. {
  50. uint64_t stop_time = now_microseconds();
  51. int total_delta = (int)(stop_time - start_time);
  52. printf("PRODUCER - Message count: %d\n", message_count);
  53. printf("Total time, milliseconds: %d\n", total_delta / 1000);
  54. printf("Overall messages-per-second: %g\n",
  55. (message_count / (total_delta / 1000000.0)));
  56. }
  57. }
  58. int main(int argc, char const *const *argv) {
  59. char const *hostname;
  60. int port, status;
  61. int rate_limit;
  62. int message_count;
  63. amqp_socket_t *socket = NULL;
  64. amqp_connection_state_t conn;
  65. if (argc < 5) {
  66. fprintf(stderr,
  67. "Usage: amqp_producer host port rate_limit message_count\n");
  68. return 1;
  69. }
  70. hostname = argv[1];
  71. port = atoi(argv[2]);
  72. rate_limit = atoi(argv[3]);
  73. message_count = atoi(argv[4]);
  74. conn = amqp_new_connection();
  75. socket = amqp_tcp_socket_new(conn);
  76. if (!socket) {
  77. die("creating TCP socket");
  78. }
  79. status = amqp_socket_open(socket, hostname, port);
  80. if (status) {
  81. die("opening TCP socket");
  82. }
  83. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  84. "guest", "guest"),
  85. "Logging in");
  86. amqp_channel_open(conn, 1);
  87. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  88. send_batch(conn, "test queue", rate_limit, message_count);
  89. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  90. "Closing channel");
  91. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  92. "Closing connection");
  93. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  94. return 0;
  95. }