2
0

amqp_producer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <amqp.h>
  40. #include <amqp_tcp_socket.h>
  41. #include "utils.h"
  42. #define SUMMARY_EVERY_US 1000000
  43. static void send_batch(amqp_connection_state_t conn, char const *queue_name,
  44. int rate_limit, int message_count) {
  45. uint64_t start_time = now_microseconds();
  46. int i;
  47. int sent = 0;
  48. int previous_sent = 0;
  49. uint64_t previous_report_time = start_time;
  50. uint64_t next_summary_time = start_time + SUMMARY_EVERY_US;
  51. char message[256];
  52. amqp_bytes_t message_bytes;
  53. for (i = 0; i < (int)sizeof(message); i++) {
  54. message[i] = i & 0xff;
  55. }
  56. message_bytes.len = sizeof(message);
  57. message_bytes.bytes = message;
  58. for (i = 0; i < message_count; i++) {
  59. uint64_t now = now_microseconds();
  60. die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes("amq.direct"),
  61. amqp_cstring_bytes(queue_name), 0, 0, NULL,
  62. message_bytes),
  63. "Publishing");
  64. sent++;
  65. if (now > next_summary_time) {
  66. int countOverInterval = sent - previous_sent;
  67. double intervalRate =
  68. countOverInterval / ((now - previous_report_time) / 1000000.0);
  69. printf("%d ms: Sent %d - %d since last report (%d Hz)\n",
  70. (int)(now - start_time) / 1000, sent, countOverInterval,
  71. (int)intervalRate);
  72. previous_sent = sent;
  73. previous_report_time = now;
  74. next_summary_time += SUMMARY_EVERY_US;
  75. }
  76. while (((i * 1000000.0) / (now - start_time)) > rate_limit) {
  77. microsleep(2000);
  78. now = now_microseconds();
  79. }
  80. }
  81. {
  82. uint64_t stop_time = now_microseconds();
  83. int total_delta = (int)(stop_time - start_time);
  84. printf("PRODUCER - Message count: %d\n", message_count);
  85. printf("Total time, milliseconds: %d\n", total_delta / 1000);
  86. printf("Overall messages-per-second: %g\n",
  87. (message_count / (total_delta / 1000000.0)));
  88. }
  89. }
  90. int main(int argc, char const *const *argv) {
  91. char const *hostname;
  92. int port, status;
  93. int rate_limit;
  94. int message_count;
  95. amqp_socket_t *socket = NULL;
  96. amqp_connection_state_t conn;
  97. if (argc < 5) {
  98. fprintf(stderr,
  99. "Usage: amqp_producer host port rate_limit message_count\n");
  100. return 1;
  101. }
  102. hostname = argv[1];
  103. port = atoi(argv[2]);
  104. rate_limit = atoi(argv[3]);
  105. message_count = atoi(argv[4]);
  106. conn = amqp_new_connection();
  107. socket = amqp_tcp_socket_new(conn);
  108. if (!socket) {
  109. die("creating TCP socket");
  110. }
  111. status = amqp_socket_open(socket, hostname, port);
  112. if (status) {
  113. die("opening TCP socket");
  114. }
  115. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  116. "guest", "guest"),
  117. "Logging in");
  118. amqp_channel_open(conn, 1);
  119. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  120. send_batch(conn, "test queue", rate_limit, message_count);
  121. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  122. "Closing channel");
  123. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  124. "Closing connection");
  125. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  126. return 0;
  127. }