| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <inttypes.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <rabbitmq-c/amqp.h> |
| 11 |
#include <rabbitmq-c/tcp_socket.h> |
| 12 |
|
| 13 |
#include "utils.h" |
| 14 |
|
| 15 |
#if ((defined(_WIN32)) || (defined(__MINGW32__)) || (defined(__MINGW64__))) |
| 16 |
#ifndef WINVER |
| 17 |
#define WINVER 0x0502 |
| 18 |
#endif |
| 19 |
#ifndef WIN32_LEAN_AND_MEAN |
| 20 |
#define WIN32_LEAN_AND_MEAN |
| 21 |
#endif |
| 22 |
#include <winsock2.h> |
| 23 |
#else |
| 24 |
#include <sys/time.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#define SUMMARY_EVERY_US 5000 |
| 28 |
|
| 29 |
static void send_batch(amqp_connection_state_t conn, amqp_bytes_t queue_name, |
| 30 |
int rate_limit, int message_count) { |
| 31 |
uint64_t start_time = now_microseconds(); |
| 32 |
int i; |
| 33 |
int sent = 0; |
| 34 |
int previous_sent = 0; |
| 35 |
uint64_t previous_report_time = start_time; |
| 36 |
uint64_t next_summary_time = start_time + SUMMARY_EVERY_US; |
| 37 |
|
| 38 |
char message[256]; |
| 39 |
amqp_bytes_t message_bytes; |
| 40 |
|
| 41 |
for (i = 0; i < (int)sizeof(message); i++) { |
| 42 |
message[i] = i & 0xff; |
| 43 |
} |
| 44 |
|
| 45 |
message_bytes.len = sizeof(message); |
| 46 |
message_bytes.bytes = message; |
| 47 |
|
| 48 |
for (i = 0; i < message_count; i++) { |
| 49 |
uint64_t now = now_microseconds(); |
| 50 |
|
| 51 |
die_on_error(amqp_basic_publish(conn, 1, amqp_literal_bytes("amq.direct"), |
| 52 |
queue_name, 0, 0, NULL, message_bytes), |
| 53 |
"Publishing"); |
| 54 |
sent++; |
| 55 |
if (now > next_summary_time) { |
| 56 |
int countOverInterval = sent - previous_sent; |
| 57 |
double intervalRate = |
| 58 |
countOverInterval / ((now - previous_report_time) / 1000000.0); |
| 59 |
printf("%d ms: Sent %d - %d since last report (%d Hz)\n", |
| 60 |
(int)(now - start_time) / 1000, sent, countOverInterval, |
| 61 |
(int)intervalRate); |
| 62 |
|
| 63 |
previous_sent = sent; |
| 64 |
previous_report_time = now; |
| 65 |
next_summary_time += SUMMARY_EVERY_US; |
| 66 |
} |
| 67 |
|
| 68 |
while (((i * 1000000.0) / (now - start_time)) > rate_limit) { |
| 69 |
microsleep(2000); |
| 70 |
now = now_microseconds(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
{ |
| 75 |
uint64_t stop_time = now_microseconds(); |
| 76 |
int total_delta = (int)(stop_time - start_time); |
| 77 |
|
| 78 |
printf("PRODUCER - Message count: %d\n", message_count); |
| 79 |
printf("Total time, milliseconds: %d\n", total_delta / 1000); |
| 80 |
printf("Overall messages-per-second: %g\n", |
| 81 |
(message_count / (total_delta / 1000000.0))); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
#define CONSUME_TIMEOUT_USEC 100 |
| 86 |
#define WAITING_TIMEOUT_USEC (30 * 1000) |
| 87 |
void wait_for_acks(amqp_connection_state_t conn) { |
| 88 |
uint64_t start_time = now_microseconds(); |
| 89 |
struct timeval timeout = {0, CONSUME_TIMEOUT_USEC}; |
| 90 |
uint64_t now = 0; |
| 91 |
amqp_publisher_confirm_t result = {}; |
| 92 |
|
| 93 |
for (;;) { |
| 94 |
amqp_rpc_reply_t ret; |
| 95 |
|
| 96 |
now = now_microseconds(); |
| 97 |
|
| 98 |
if (now > start_time + WAITING_TIMEOUT_USEC) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
amqp_maybe_release_buffers(conn); |
| 103 |
ret = amqp_publisher_confirm_wait(conn, &timeout, &result); |
| 104 |
|
| 105 |
if (AMQP_RESPONSE_LIBRARY_EXCEPTION == ret.reply_type) { |
| 106 |
if (AMQP_STATUS_UNEXPECTED_STATE == ret.library_error) { |
| 107 |
fprintf(stderr, "An unexpected method was received\n"); |
| 108 |
return; |
| 109 |
} else if (AMQP_STATUS_TIMEOUT == ret.library_error) { |
| 110 |
|
| 111 |
return; |
| 112 |
} else { |
| 113 |
die_on_amqp_error(ret, "Waiting for publisher confirmation"); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
switch (result.method) { |
| 118 |
case AMQP_BASIC_ACK_METHOD: |
| 119 |
fprintf(stderr, "Got an ACK!\n"); |
| 120 |
fprintf(stderr, "Here's the ACK:\n"); |
| 121 |
fprintf(stderr, "\tdelivery_tag: «%" PRIu64 "»\n", |
| 122 |
result.payload.ack.delivery_tag); |
| 123 |
fprintf(stderr, "\tmultiple: «%d»\n", result.payload.ack.multiple); |
| 124 |
break; |
| 125 |
case AMQP_BASIC_NACK_METHOD: |
| 126 |
fprintf(stderr, "NACK\n"); |
| 127 |
break; |
| 128 |
case AMQP_BASIC_REJECT_METHOD: |
| 129 |
fprintf(stderr, "REJECT\n"); |
| 130 |
break; |
| 131 |
default: |
| 132 |
fprintf(stderr, "Unexpected method «%s» is.\n", |
| 133 |
amqp_method_name(result.method)); |
| 134 |
}; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
int main(int argc, char const *const *argv) { |
| 139 |
char const *hostname; |
| 140 |
int port, status; |
| 141 |
int rate_limit; |
| 142 |
int message_count; |
| 143 |
amqp_socket_t *socket = NULL; |
| 144 |
amqp_connection_state_t conn; |
| 145 |
|
| 146 |
if (argc < 5) { |
| 147 |
fprintf(stderr, |
| 148 |
"Usage: amqp_producer host port rate_limit message_count\n"); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
hostname = argv[1]; |
| 153 |
port = atoi(argv[2]); |
| 154 |
rate_limit = atoi(argv[3]); |
| 155 |
message_count = atoi(argv[4]); |
| 156 |
|
| 157 |
conn = amqp_new_connection(); |
| 158 |
|
| 159 |
socket = amqp_tcp_socket_new(conn); |
| 160 |
if (!socket) { |
| 161 |
die("creating TCP socket"); |
| 162 |
} |
| 163 |
|
| 164 |
status = amqp_socket_open(socket, hostname, port); |
| 165 |
if (status) { |
| 166 |
die("opening TCP socket"); |
| 167 |
} |
| 168 |
|
| 169 |
die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, |
| 170 |
"guest", "guest"), |
| 171 |
"Logging in"); |
| 172 |
amqp_channel_open(conn, 1); |
| 173 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); |
| 174 |
|
| 175 |
|
| 176 |
amqp_confirm_select(conn, 1); |
| 177 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Enable confirm-select"); |
| 178 |
|
| 179 |
send_batch(conn, amqp_literal_bytes("test queue"), rate_limit, message_count); |
| 180 |
|
| 181 |
wait_for_acks(conn); |
| 182 |
die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), |
| 183 |
"Closing channel"); |
| 184 |
die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), |
| 185 |
"Closing connection"); |
| 186 |
die_on_error(amqp_destroy_connection(conn), "Ending connection"); |
| 187 |
return 0; |
| 188 |
} |