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