| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/clock.h> |
| 7 |
#include <aws/common/command_line_parser.h> |
| 8 |
#include <aws/common/condition_variable.h> |
| 9 |
#include <aws/common/hash_table.h> |
| 10 |
#include <aws/common/log_channel.h> |
| 11 |
#include <aws/common/log_formatter.h> |
| 12 |
#include <aws/common/log_writer.h> |
| 13 |
#include <aws/common/mutex.h> |
| 14 |
#include <aws/common/string.h> |
| 15 |
|
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/event_loop.h> |
| 18 |
#include <aws/io/logging.h> |
| 19 |
#include <aws/io/socket.h> |
| 20 |
#include <aws/io/stream.h> |
| 21 |
#include <aws/io/tls_channel_handler.h> |
| 22 |
#include <aws/io/uri.h> |
| 23 |
|
| 24 |
#include <aws/mqtt/client.h> |
| 25 |
#include <aws/mqtt/mqtt.h> |
| 26 |
|
| 27 |
#include <inttypes.h> |
| 28 |
|
| 29 |
#ifdef _MSC_VER |
| 30 |
# pragma warning(disable : 4996) |
| 31 |
# pragma warning(disable : 4204) |
| 32 |
# pragma warning(disable : 4221) |
| 33 |
#endif |
| 34 |
|
| 35 |
struct app_ctx { |
| 36 |
struct aws_allocator *allocator; |
| 37 |
struct aws_mutex lock; |
| 38 |
struct aws_condition_variable signal; |
| 39 |
struct aws_uri uri; |
| 40 |
uint32_t port; |
| 41 |
const char *cacert; |
| 42 |
const char *cert; |
| 43 |
const char *key; |
| 44 |
int connect_timeout; |
| 45 |
int iterations; |
| 46 |
int connection_count; |
| 47 |
int message_count; |
| 48 |
int target_cops; |
| 49 |
int target_pops; |
| 50 |
int pending_publish_completions; |
| 51 |
int publish_successes; |
| 52 |
int publish_failures; |
| 53 |
int received_messages; |
| 54 |
|
| 55 |
struct aws_tls_connection_options tls_connection_options; |
| 56 |
|
| 57 |
struct aws_linked_list pending_connection_list; |
| 58 |
struct aws_linked_list established_connection_list; |
| 59 |
|
| 60 |
const char *log_filename; |
| 61 |
enum aws_log_level log_level; |
| 62 |
}; |
| 63 |
|
| 64 |
static void s_usage(int exit_code) { |
| 65 |
|
| 66 |
fprintf(stderr, "usage: elastipubsub [options] --endpoint\n"); |
| 67 |
fprintf(stderr, " --endpoint: url to connect to \n"); |
| 68 |
fprintf(stderr, "\n Options:\n\n"); |
| 69 |
fprintf(stderr, " --cacert FILE: path to a CA certficate file.\n"); |
| 70 |
fprintf(stderr, " --cert FILE: path to a PEM encoded certificate to use with mTLS\n"); |
| 71 |
fprintf(stderr, " --key FILE: Path to a PEM encoded private key that matches cert.\n"); |
| 72 |
fprintf(stderr, " --cops INT: target control (connect, subscribe) operations per second\n"); |
| 73 |
fprintf(stderr, " --connect-timeout INT: time in milliseconds to wait for a connection.\n"); |
| 74 |
fprintf(stderr, " -i, --iterations INT: number of independent iterations to run the test for\n"); |
| 75 |
fprintf(stderr, " -k, --connections INT: number of independent connections to make.\n"); |
| 76 |
fprintf(stderr, " -l, --log FILE: dumps logs to FILE instead of stderr.\n"); |
| 77 |
fprintf(stderr, " -n, --messages INT: number of messages to publish per iteration\n"); |
| 78 |
fprintf(stderr, " -p, --pops INT: target publish operations per second\n"); |
| 79 |
fprintf(stderr, " -v, --verbose: ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.\n"); |
| 80 |
fprintf(stderr, " -h, --help\n"); |
| 81 |
fprintf(stderr, " Display this message and quit.\n"); |
| 82 |
exit(exit_code); |
| 83 |
} |
| 84 |
|
| 85 |
static struct aws_cli_option s_long_options[] = { |
| 86 |
{"cacert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'a'}, |
| 87 |
{"cert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'c'}, |
| 88 |
{"cops", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'C'}, |
| 89 |
{"key", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'e'}, |
| 90 |
{"connect-timeout", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'f'}, |
| 91 |
{"iterations", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'i'}, |
| 92 |
{"connections", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'k'}, |
| 93 |
{"log", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'l'}, |
| 94 |
{"messages", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'n'}, |
| 95 |
{"pops", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'p'}, |
| 96 |
{"verbose", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'v'}, |
| 97 |
{"help", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'h'}, |
| 98 |
{"endpoint", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'E'}, |
| 99 |
|
| 100 |
{NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0}, |
| 101 |
}; |
| 102 |
|
| 103 |
static void s_parse_options(int argc, char **argv, struct app_ctx *ctx) { |
| 104 |
bool uri_found = false; |
| 105 |
while (true) { |
| 106 |
int option_index = 0; |
| 107 |
int c = aws_cli_getopt_long(argc, argv, "a:c:C:e:f:i:k:l:n:p:v:h:E", s_long_options, &option_index); |
| 108 |
if (c == -1) { |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
switch (c) { |
| 113 |
case 0: |
| 114 |
|
| 115 |
break; |
| 116 |
case 'a': |
| 117 |
ctx->cacert = aws_cli_optarg; |
| 118 |
break; |
| 119 |
case 'c': |
| 120 |
ctx->cert = aws_cli_optarg; |
| 121 |
break; |
| 122 |
case 'C': |
| 123 |
ctx->target_cops = atoi(aws_cli_optarg); |
| 124 |
break; |
| 125 |
case 'e': |
| 126 |
ctx->key = aws_cli_optarg; |
| 127 |
break; |
| 128 |
case 'f': |
| 129 |
ctx->connect_timeout = atoi(aws_cli_optarg); |
| 130 |
break; |
| 131 |
case 'i': |
| 132 |
ctx->iterations = atoi(aws_cli_optarg); |
| 133 |
break; |
| 134 |
case 'k': |
| 135 |
ctx->connection_count = atoi(aws_cli_optarg); |
| 136 |
break; |
| 137 |
case 'l': |
| 138 |
ctx->log_filename = aws_cli_optarg; |
| 139 |
break; |
| 140 |
case 'n': |
| 141 |
ctx->message_count = atoi(aws_cli_optarg); |
| 142 |
break; |
| 143 |
case 'p': |
| 144 |
ctx->target_pops = atoi(aws_cli_optarg); |
| 145 |
break; |
| 146 |
case 'v': |
| 147 |
if (!strcmp(aws_cli_optarg, "TRACE")) { |
| 148 |
ctx->log_level = AWS_LL_TRACE; |
| 149 |
} else if (!strcmp(aws_cli_optarg, "INFO")) { |
| 150 |
ctx->log_level = AWS_LL_INFO; |
| 151 |
} else if (!strcmp(aws_cli_optarg, "DEBUG")) { |
| 152 |
ctx->log_level = AWS_LL_DEBUG; |
| 153 |
} else if (!strcmp(aws_cli_optarg, "ERROR")) { |
| 154 |
ctx->log_level = AWS_LL_ERROR; |
| 155 |
} else { |
| 156 |
fprintf(stderr, "unsupported log level %s.\n", aws_cli_optarg); |
| 157 |
s_usage(1); |
| 158 |
} |
| 159 |
break; |
| 160 |
case 'h': |
| 161 |
s_usage(0); |
| 162 |
break; |
| 163 |
case 0x02: { |
| 164 |
struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str(aws_cli_positional_arg); |
| 165 |
if (aws_uri_init_parse(&ctx->uri, ctx->allocator, &uri_cursor)) { |
| 166 |
fprintf( |
| 167 |
stderr, |
| 168 |
"Failed to parse uri %s with error %s\n", |
| 169 |
(char *)uri_cursor.ptr, |
| 170 |
aws_error_debug_str(aws_last_error())); |
| 171 |
s_usage(1); |
| 172 |
} |
| 173 |
uri_found = true; |
| 174 |
break; |
| 175 |
} |
| 176 |
default: |
| 177 |
fprintf(stderr, "Unknown option\n"); |
| 178 |
s_usage(1); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (!uri_found) { |
| 183 |
fprintf(stderr, "A URI for the request must be supplied.\n"); |
| 184 |
s_usage(1); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
struct connection_user_data { |
| 189 |
struct aws_linked_list_node node; |
| 190 |
struct app_ctx *app_ctx; |
| 191 |
struct aws_mqtt_client *client; |
| 192 |
struct aws_mqtt_client_connection *connection; |
| 193 |
uint32_t id; |
| 194 |
uint32_t subscription_id; |
| 195 |
}; |
| 196 |
|
| 197 |
static void s_final_destroy_connection_data(struct connection_user_data *user_data) { |
| 198 |
aws_mutex_lock(&user_data->app_ctx->lock); |
| 199 |
aws_linked_list_remove(&user_data->node); |
| 200 |
aws_mutex_unlock(&user_data->app_ctx->lock); |
| 201 |
aws_condition_variable_notify_one(&user_data->app_ctx->signal); |
| 202 |
|
| 203 |
if (user_data->connection != NULL) { |
| 204 |
aws_mqtt_client_connection_release(user_data->connection); |
| 205 |
user_data->connection = NULL; |
| 206 |
} |
| 207 |
|
| 208 |
if (user_data->client != NULL) { |
| 209 |
aws_mqtt_client_release(user_data->client); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
static void s_on_connection_complete( |
| 214 |
struct aws_mqtt_client_connection *connection, |
| 215 |
int error_code, |
| 216 |
enum aws_mqtt_connect_return_code return_code, |
| 217 |
bool session_present, |
| 218 |
void *userdata) { |
| 219 |
(void)connection; |
| 220 |
(void)session_present; |
| 221 |
|
| 222 |
struct connection_user_data *user_data = userdata; |
| 223 |
if (error_code != AWS_ERROR_SUCCESS || return_code != AWS_MQTT_CONNECT_ACCEPTED) { |
| 224 |
s_final_destroy_connection_data(user_data); |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
aws_mutex_lock(&user_data->app_ctx->lock); |
| 229 |
aws_linked_list_remove(&user_data->node); |
| 230 |
aws_linked_list_push_back(&user_data->app_ctx->established_connection_list, &user_data->node); |
| 231 |
aws_mutex_unlock(&user_data->app_ctx->lock); |
| 232 |
aws_condition_variable_notify_one(&user_data->app_ctx->signal); |
| 233 |
} |
| 234 |
|
| 235 |
static bool s_all_connections_complete(void *context) { |
| 236 |
struct app_ctx *app_ctx = context; |
| 237 |
|
| 238 |
return aws_linked_list_empty(&app_ctx->pending_connection_list); |
| 239 |
} |
| 240 |
|
| 241 |
#define MIN_SLEEP_TIME_MILLIS 10 |
| 242 |
|
| 243 |
static void s_throttle_operations(int target_ops, uint64_t start_time, int32_t operation_index) { |
| 244 |
if (target_ops == 0) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
uint64_t now = 0; |
| 249 |
aws_high_res_clock_get_ticks(&now); |
| 250 |
AWS_FATAL_ASSERT(now >= start_time); |
| 251 |
|
| 252 |
uint64_t elapsed_nanos = now - start_time; |
| 253 |
|
| 254 |
uint64_t nanos_per_second = aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 255 |
uint64_t target_ops_u64 = (uint64_t)target_ops; |
| 256 |
uint64_t operation_count_u64 = (uint64_t)operation_index; |
| 257 |
uint64_t desired_elapsed_nanos = 0; |
| 258 |
|
| 259 |
uint64_t product = 0; |
| 260 |
if (aws_mul_u64_checked(operation_count_u64, nanos_per_second, &product)) { |
| 261 |
double product_d = (double)operation_count_u64 * (double)nanos_per_second; |
| 262 |
desired_elapsed_nanos = (uint64_t)(product_d / (double)target_ops_u64); |
| 263 |
} else { |
| 264 |
desired_elapsed_nanos = product / target_ops_u64; |
| 265 |
} |
| 266 |
|
| 267 |
if (desired_elapsed_nanos > elapsed_nanos) { |
| 268 |
uint64_t nano_difference = desired_elapsed_nanos - elapsed_nanos; |
| 269 |
uint64_t millis = aws_timestamp_convert(nano_difference, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL); |
| 270 |
if (millis > MIN_SLEEP_TIME_MILLIS) { |
| 271 |
aws_thread_current_sleep(nano_difference); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static void s_establish_connections( |
| 277 |
struct app_ctx *app_ctx, |
| 278 |
struct connection_user_data *connections, |
| 279 |
struct aws_client_bootstrap *bootstrap, |
| 280 |
struct aws_tls_connection_options *tls_connection_options) { |
| 281 |
struct aws_socket_options socket_options = { |
| 282 |
.type = AWS_SOCKET_STREAM, |
| 283 |
.connect_timeout_ms = (uint32_t)app_ctx->connect_timeout, |
| 284 |
.keep_alive_timeout_sec = 0, |
| 285 |
.keepalive = false, |
| 286 |
.keep_alive_interval_sec = 0, |
| 287 |
}; |
| 288 |
|
| 289 |
aws_mutex_lock(&app_ctx->lock); |
| 290 |
for (int j = 0; j < app_ctx->connection_count; j++) { |
| 291 |
struct connection_user_data *connection_data = connections + j; |
| 292 |
aws_linked_list_push_back(&app_ctx->pending_connection_list, &connection_data->node); |
| 293 |
|
| 294 |
connection_data->app_ctx = app_ctx; |
| 295 |
connection_data->client = aws_mqtt_client_new(app_ctx->allocator, bootstrap); |
| 296 |
AWS_FATAL_ASSERT(connection_data->client != NULL); |
| 297 |
connection_data->connection = aws_mqtt_client_connection_new(connection_data->client); |
| 298 |
AWS_FATAL_ASSERT(connection_data->connection != NULL); |
| 299 |
connection_data->id = (uint32_t)j; |
| 300 |
} |
| 301 |
aws_mutex_unlock(&app_ctx->lock); |
| 302 |
|
| 303 |
uint64_t start_time = 0; |
| 304 |
aws_high_res_clock_get_ticks(&start_time); |
| 305 |
|
| 306 |
for (int j = 0; j < app_ctx->connection_count; j++) { |
| 307 |
struct connection_user_data *connection_data = connections + j; |
| 308 |
|
| 309 |
char client_id[32]; |
| 310 |
snprintf(client_id, AWS_ARRAY_SIZE(client_id), "stress%d", j); |
| 311 |
|
| 312 |
struct aws_mqtt_connection_options connection_options = { |
| 313 |
.host_name = app_ctx->uri.host_name, |
| 314 |
.port = app_ctx->port, |
| 315 |
.socket_options = &socket_options, |
| 316 |
.tls_options = tls_connection_options, |
| 317 |
.client_id = aws_byte_cursor_from_c_str(client_id), |
| 318 |
.keep_alive_time_secs = 0, |
| 319 |
.ping_timeout_ms = 0, |
| 320 |
.protocol_operation_timeout_ms = |
| 321 |
(uint32_t)aws_timestamp_convert(10, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL), |
| 322 |
.on_connection_complete = s_on_connection_complete, |
| 323 |
.user_data = connection_data, |
| 324 |
.clean_session = true, |
| 325 |
}; |
| 326 |
|
| 327 |
if (aws_mqtt_client_connection_connect(connection_data->connection, &connection_options)) { |
| 328 |
s_final_destroy_connection_data(connection_data); |
| 329 |
} |
| 330 |
|
| 331 |
if (j > 0 && j % 100 == 0) { |
| 332 |
printf("Connection count: %d\n", j); |
| 333 |
} |
| 334 |
|
| 335 |
s_throttle_operations(app_ctx->target_cops, start_time, j + 1); |
| 336 |
} |
| 337 |
|
| 338 |
aws_mutex_lock(&app_ctx->lock); |
| 339 |
aws_condition_variable_wait_pred(&app_ctx->signal, &app_ctx->lock, s_all_connections_complete, app_ctx); |
| 340 |
AWS_FATAL_ASSERT(!aws_linked_list_empty(&app_ctx->established_connection_list)); |
| 341 |
aws_mutex_unlock(&app_ctx->lock); |
| 342 |
} |
| 343 |
|
| 344 |
void s_on_publish( |
| 345 |
struct aws_mqtt_client_connection *connection, |
| 346 |
const struct aws_byte_cursor *topic, |
| 347 |
const struct aws_byte_cursor *payload, |
| 348 |
bool dup, |
| 349 |
enum aws_mqtt_qos qos, |
| 350 |
bool retain, |
| 351 |
void *userdata) { |
| 352 |
(void)connection; |
| 353 |
(void)topic; |
| 354 |
(void)payload; |
| 355 |
(void)dup; |
| 356 |
(void)qos; |
| 357 |
(void)retain; |
| 358 |
|
| 359 |
struct app_ctx *app_ctx = userdata; |
| 360 |
aws_mutex_lock(&app_ctx->lock); |
| 361 |
++app_ctx->received_messages; |
| 362 |
aws_mutex_unlock(&app_ctx->lock); |
| 363 |
} |
| 364 |
|
| 365 |
void s_on_disconnect_complete(struct aws_mqtt_client_connection *connection, void *userdata) { |
| 366 |
(void)connection; |
| 367 |
s_final_destroy_connection_data(userdata); |
| 368 |
} |
| 369 |
|
| 370 |
void s_on_subscribe_complete( |
| 371 |
struct aws_mqtt_client_connection *connection, |
| 372 |
uint16_t packet_id, |
| 373 |
const struct aws_byte_cursor *topic, |
| 374 |
enum aws_mqtt_qos qos, |
| 375 |
int error_code, |
| 376 |
void *userdata) { |
| 377 |
|
| 378 |
(void)connection; |
| 379 |
(void)packet_id; |
| 380 |
(void)topic; |
| 381 |
(void)qos; |
| 382 |
struct connection_user_data *connection_data = userdata; |
| 383 |
|
| 384 |
if (error_code != AWS_ERROR_SUCCESS) { |
| 385 |
if (aws_mqtt_client_connection_disconnect( |
| 386 |
connection_data->connection, s_on_disconnect_complete, connection_data)) { |
| 387 |
s_final_destroy_connection_data(connection_data); |
| 388 |
} |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
aws_mutex_lock(&connection_data->app_ctx->lock); |
| 393 |
aws_linked_list_remove(&connection_data->node); |
| 394 |
aws_linked_list_push_back(&connection_data->app_ctx->established_connection_list, &connection_data->node); |
| 395 |
aws_mutex_unlock(&connection_data->app_ctx->lock); |
| 396 |
aws_condition_variable_notify_one(&connection_data->app_ctx->signal); |
| 397 |
} |
| 398 |
|
| 399 |
void s_on_subscribe_removed(void *userdata) { |
| 400 |
(void)userdata; |
| 401 |
} |
| 402 |
|
| 403 |
static void s_establish_subscriptions(struct app_ctx *app_ctx) { |
| 404 |
|
| 405 |
struct aws_linked_list connections; |
| 406 |
aws_linked_list_init(&connections); |
| 407 |
|
| 408 |
aws_mutex_lock(&app_ctx->lock); |
| 409 |
aws_linked_list_swap_contents(&app_ctx->established_connection_list, &connections); |
| 410 |
aws_mutex_unlock(&app_ctx->lock); |
| 411 |
|
| 412 |
uint64_t start_time = 0; |
| 413 |
aws_high_res_clock_get_ticks(&start_time); |
| 414 |
uint32_t subscription_index = 1; |
| 415 |
while (!aws_linked_list_empty(&connections)) { |
| 416 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&connections); |
| 417 |
struct connection_user_data *connection_data = AWS_CONTAINER_OF(node, struct connection_user_data, node); |
| 418 |
|
| 419 |
aws_mutex_lock(&app_ctx->lock); |
| 420 |
aws_linked_list_push_back(&app_ctx->pending_connection_list, &connection_data->node); |
| 421 |
aws_mutex_unlock(&app_ctx->lock); |
| 422 |
|
| 423 |
connection_data->subscription_id = subscription_index; |
| 424 |
|
| 425 |
char buffer[32]; |
| 426 |
snprintf(buffer, AWS_ARRAY_SIZE(buffer), "topic%d", subscription_index++); |
| 427 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_c_str(buffer); |
| 428 |
|
| 429 |
uint16_t id = aws_mqtt_client_connection_subscribe( |
| 430 |
connection_data->connection, |
| 431 |
&topic_cursor, |
| 432 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 433 |
s_on_publish, |
| 434 |
app_ctx, |
| 435 |
s_on_subscribe_removed, |
| 436 |
s_on_subscribe_complete, |
| 437 |
connection_data); |
| 438 |
|
| 439 |
if (id == 0) { |
| 440 |
if (aws_mqtt_client_connection_disconnect( |
| 441 |
connection_data->connection, s_on_disconnect_complete, connection_data)) { |
| 442 |
s_final_destroy_connection_data(connection_data); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (subscription_index > 0 && subscription_index % 100 == 0) { |
| 447 |
printf("Subscribe count: %d\n", subscription_index); |
| 448 |
} |
| 449 |
|
| 450 |
s_throttle_operations(app_ctx->target_cops, start_time, subscription_index - 1); |
| 451 |
} |
| 452 |
|
| 453 |
aws_mutex_lock(&app_ctx->lock); |
| 454 |
aws_condition_variable_wait_pred(&app_ctx->signal, &app_ctx->lock, s_all_connections_complete, app_ctx); |
| 455 |
AWS_FATAL_ASSERT(!aws_linked_list_empty(&app_ctx->established_connection_list)); |
| 456 |
aws_mutex_unlock(&app_ctx->lock); |
| 457 |
} |
| 458 |
|
| 459 |
static void s_teardown_connections(struct app_ctx *app_ctx) { |
| 460 |
|
| 461 |
struct aws_linked_list connections; |
| 462 |
aws_linked_list_init(&connections); |
| 463 |
|
| 464 |
aws_mutex_lock(&app_ctx->lock); |
| 465 |
aws_linked_list_swap_contents(&app_ctx->established_connection_list, &connections); |
| 466 |
aws_mutex_unlock(&app_ctx->lock); |
| 467 |
|
| 468 |
while (!aws_linked_list_empty(&connections)) { |
| 469 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&connections); |
| 470 |
struct connection_user_data *connection_data = AWS_CONTAINER_OF(node, struct connection_user_data, node); |
| 471 |
|
| 472 |
aws_mutex_lock(&app_ctx->lock); |
| 473 |
aws_linked_list_push_back(&app_ctx->pending_connection_list, &connection_data->node); |
| 474 |
aws_mutex_unlock(&app_ctx->lock); |
| 475 |
|
| 476 |
if (aws_mqtt_client_connection_disconnect( |
| 477 |
connection_data->connection, s_on_disconnect_complete, connection_data)) { |
| 478 |
s_final_destroy_connection_data(connection_data); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
aws_mutex_lock(&app_ctx->lock); |
| 483 |
aws_condition_variable_wait_pred(&app_ctx->signal, &app_ctx->lock, s_all_connections_complete, app_ctx); |
| 484 |
aws_mutex_unlock(&app_ctx->lock); |
| 485 |
} |
| 486 |
|
| 487 |
static void s_build_web(struct app_ctx *app_ctx, struct aws_array_list *connections, struct aws_array_list *topics) { |
| 488 |
aws_array_list_init_dynamic( |
| 489 |
connections, app_ctx->allocator, app_ctx->connection_count, sizeof(struct connection_user_data *)); |
| 490 |
aws_array_list_init_dynamic(topics, app_ctx->allocator, app_ctx->connection_count, sizeof(uint32_t)); |
| 491 |
|
| 492 |
aws_mutex_lock(&app_ctx->lock); |
| 493 |
|
| 494 |
struct aws_linked_list_node *node = aws_linked_list_begin(&app_ctx->established_connection_list); |
| 495 |
while (node != aws_linked_list_end(&app_ctx->established_connection_list)) { |
| 496 |
struct connection_user_data *connection_data = AWS_CONTAINER_OF(node, struct connection_user_data, node); |
| 497 |
uint32_t topic_id = connection_data->subscription_id; |
| 498 |
|
| 499 |
aws_array_list_push_back(connections, &connection_data); |
| 500 |
aws_array_list_push_back(topics, &topic_id); |
| 501 |
|
| 502 |
node = aws_linked_list_next(node); |
| 503 |
} |
| 504 |
aws_mutex_unlock(&app_ctx->lock); |
| 505 |
} |
| 506 |
|
| 507 |
static void s_on_publish_complete( |
| 508 |
struct aws_mqtt_client_connection *connection, |
| 509 |
uint16_t packet_id, |
| 510 |
int error_code, |
| 511 |
void *userdata) { |
| 512 |
(void)connection; |
| 513 |
(void)packet_id; |
| 514 |
|
| 515 |
struct app_ctx *app_ctx = userdata; |
| 516 |
aws_mutex_lock(&app_ctx->lock); |
| 517 |
--app_ctx->pending_publish_completions; |
| 518 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 519 |
++app_ctx->publish_successes; |
| 520 |
} else { |
| 521 |
++app_ctx->publish_failures; |
| 522 |
} |
| 523 |
aws_mutex_unlock(&app_ctx->lock); |
| 524 |
aws_condition_variable_notify_one(&app_ctx->signal); |
| 525 |
} |
| 526 |
|
| 527 |
static bool s_all_publishes_complete(void *userdata) { |
| 528 |
struct app_ctx *app_ctx = userdata; |
| 529 |
|
| 530 |
return app_ctx->pending_publish_completions == 0; |
| 531 |
} |
| 532 |
|
| 533 |
static void s_publish(struct app_ctx *app_ctx) { |
| 534 |
struct aws_array_list valid_topics; |
| 535 |
AWS_ZERO_STRUCT(valid_topics); |
| 536 |
struct aws_array_list valid_connections; |
| 537 |
AWS_ZERO_STRUCT(valid_connections); |
| 538 |
|
| 539 |
s_build_web(app_ctx, &valid_connections, &valid_topics); |
| 540 |
printf("Using %d connections\n", (int)aws_array_list_length(&valid_connections)); |
| 541 |
|
| 542 |
size_t connection_count = aws_array_list_length(&valid_connections); |
| 543 |
AWS_FATAL_ASSERT(connection_count > 0); |
| 544 |
size_t topic_count = aws_array_list_length(&valid_topics); |
| 545 |
AWS_FATAL_ASSERT(topic_count > 0); |
| 546 |
|
| 547 |
aws_mutex_lock(&app_ctx->lock); |
| 548 |
app_ctx->pending_publish_completions = app_ctx->message_count; |
| 549 |
aws_mutex_unlock(&app_ctx->lock); |
| 550 |
|
| 551 |
struct aws_byte_cursor payload_cursor = aws_byte_cursor_from_c_str("MESSAGE PAYLOAD"); |
| 552 |
|
| 553 |
char topic_buffer[32]; |
| 554 |
|
| 555 |
uint64_t now = 0; |
| 556 |
aws_high_res_clock_get_ticks(&now); |
| 557 |
int failed_publishes = 0; |
| 558 |
for (int i = 0; i < app_ctx->message_count; ++i) { |
| 559 |
size_t random_connection_index = rand() % connection_count; |
| 560 |
struct connection_user_data *connection_ud = NULL; |
| 561 |
aws_array_list_get_at(&valid_connections, &connection_ud, random_connection_index); |
| 562 |
|
| 563 |
size_t random_topic_index = rand() % topic_count; |
| 564 |
uint32_t topic_id = 0; |
| 565 |
aws_array_list_get_at(&valid_topics, &topic_id, random_topic_index); |
| 566 |
|
| 567 |
snprintf(topic_buffer, AWS_ARRAY_SIZE(topic_buffer), "topic%d", topic_id); |
| 568 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_c_str(topic_buffer); |
| 569 |
|
| 570 |
uint16_t id = aws_mqtt_client_connection_publish( |
| 571 |
connection_ud->connection, |
| 572 |
&topic_cursor, |
| 573 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 574 |
false, |
| 575 |
&payload_cursor, |
| 576 |
s_on_publish_complete, |
| 577 |
app_ctx); |
| 578 |
|
| 579 |
if (id == 0) { |
| 580 |
++failed_publishes; |
| 581 |
} |
| 582 |
|
| 583 |
if (i > 0 && i % 1000 == 0) { |
| 584 |
printf("Publish count: %d\n", i); |
| 585 |
} |
| 586 |
|
| 587 |
s_throttle_operations(app_ctx->target_pops, now, i + 1); |
| 588 |
} |
| 589 |
|
| 590 |
aws_mutex_lock(&app_ctx->lock); |
| 591 |
app_ctx->pending_publish_completions -= failed_publishes; |
| 592 |
app_ctx->publish_failures += failed_publishes; |
| 593 |
aws_condition_variable_wait_pred(&app_ctx->signal, &app_ctx->lock, s_all_publishes_complete, app_ctx); |
| 594 |
aws_mutex_unlock(&app_ctx->lock); |
| 595 |
|
| 596 |
aws_array_list_clean_up(&valid_topics); |
| 597 |
aws_array_list_clean_up(&valid_connections); |
| 598 |
} |
| 599 |
|
| 600 |
static void s_reset_app_ctx(struct app_ctx *app_ctx) { |
| 601 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&app_ctx->pending_connection_list)); |
| 602 |
aws_linked_list_init(&app_ctx->pending_connection_list); |
| 603 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&app_ctx->established_connection_list)); |
| 604 |
aws_linked_list_init(&app_ctx->established_connection_list); |
| 605 |
|
| 606 |
app_ctx->pending_publish_completions = 0; |
| 607 |
app_ctx->publish_successes = 0; |
| 608 |
app_ctx->publish_failures = 0; |
| 609 |
app_ctx->received_messages = 0; |
| 610 |
} |
| 611 |
|
| 612 |
int main(int argc, char **argv) { |
| 613 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_STACKS, 8); |
| 614 |
|
| 615 |
aws_mqtt_library_init(allocator); |
| 616 |
|
| 617 |
struct app_ctx app_ctx; |
| 618 |
AWS_ZERO_STRUCT(app_ctx); |
| 619 |
app_ctx.allocator = allocator; |
| 620 |
app_ctx.signal = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT; |
| 621 |
app_ctx.connect_timeout = 3000; |
| 622 |
app_ctx.iterations = 1; |
| 623 |
app_ctx.message_count = 10; |
| 624 |
app_ctx.connection_count = 1; |
| 625 |
aws_mutex_init(&app_ctx.lock); |
| 626 |
app_ctx.port = 8883; |
| 627 |
app_ctx.target_cops = 250; |
| 628 |
app_ctx.target_pops = 10000; |
| 629 |
|
| 630 |
aws_linked_list_init(&app_ctx.pending_connection_list); |
| 631 |
aws_linked_list_init(&app_ctx.established_connection_list); |
| 632 |
|
| 633 |
s_parse_options(argc, argv, &app_ctx); |
| 634 |
if (app_ctx.uri.port) { |
| 635 |
app_ctx.port = app_ctx.uri.port; |
| 636 |
} |
| 637 |
|
| 638 |
struct aws_logger logger; |
| 639 |
AWS_ZERO_STRUCT(logger); |
| 640 |
|
| 641 |
struct aws_logger_standard_options options = { |
| 642 |
.level = app_ctx.log_level, |
| 643 |
}; |
| 644 |
|
| 645 |
if (app_ctx.log_level) { |
| 646 |
if (app_ctx.log_filename) { |
| 647 |
options.filename = app_ctx.log_filename; |
| 648 |
} else { |
| 649 |
options.file = stderr; |
| 650 |
} |
| 651 |
|
| 652 |
if (aws_logger_init_standard(&logger, allocator, &options)) { |
| 653 |
fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error())); |
| 654 |
exit(1); |
| 655 |
} |
| 656 |
|
| 657 |
aws_logger_set(&logger); |
| 658 |
} |
| 659 |
|
| 660 |
for (int i = 0; i < app_ctx.iterations; ++i) { |
| 661 |
|
| 662 |
printf("Iteration %d starting\n", i + 1); |
| 663 |
s_reset_app_ctx(&app_ctx); |
| 664 |
|
| 665 |
struct aws_tls_ctx *tls_ctx = NULL; |
| 666 |
struct aws_tls_ctx_options tls_ctx_options; |
| 667 |
AWS_ZERO_STRUCT(tls_ctx_options); |
| 668 |
struct aws_tls_connection_options tls_connection_options; |
| 669 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 670 |
|
| 671 |
if (app_ctx.cert && app_ctx.key) { |
| 672 |
if (aws_tls_ctx_options_init_client_mtls_from_path( |
| 673 |
&tls_ctx_options, allocator, app_ctx.cert, app_ctx.key)) { |
| 674 |
fprintf( |
| 675 |
stderr, |
| 676 |
"Failed to load %s and %s with error %s.", |
| 677 |
app_ctx.cert, |
| 678 |
app_ctx.key, |
| 679 |
aws_error_debug_str(aws_last_error())); |
| 680 |
exit(1); |
| 681 |
} |
| 682 |
} else { |
| 683 |
aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator); |
| 684 |
} |
| 685 |
|
| 686 |
if (app_ctx.cacert) { |
| 687 |
if (aws_tls_ctx_options_override_default_trust_store_from_path(&tls_ctx_options, NULL, app_ctx.cacert)) { |
| 688 |
fprintf( |
| 689 |
stderr, "Failed to load %s with error %s", app_ctx.cacert, aws_error_debug_str(aws_last_error())); |
| 690 |
exit(1); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
if (aws_tls_ctx_options_set_alpn_list(&tls_ctx_options, "x-amzn-mqtt-ca")) { |
| 695 |
fprintf(stderr, "Failed to set alpn list with error %s.", aws_error_debug_str(aws_last_error())); |
| 696 |
exit(1); |
| 697 |
} |
| 698 |
|
| 699 |
tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 700 |
|
| 701 |
if (!tls_ctx) { |
| 702 |
fprintf(stderr, "Failed to initialize TLS context with error %s.", aws_error_debug_str(aws_last_error())); |
| 703 |
exit(1); |
| 704 |
} |
| 705 |
|
| 706 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_ctx); |
| 707 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &app_ctx.uri.host_name)) { |
| 708 |
fprintf(stderr, "Failed to set servername with error %s.", aws_error_debug_str(aws_last_error())); |
| 709 |
exit(1); |
| 710 |
} |
| 711 |
|
| 712 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 2, NULL); |
| 713 |
|
| 714 |
struct aws_host_resolver_default_options resolver_options = { |
| 715 |
.el_group = el_group, |
| 716 |
.max_entries = 8, |
| 717 |
}; |
| 718 |
|
| 719 |
struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 720 |
|
| 721 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 722 |
.event_loop_group = el_group, |
| 723 |
.host_resolver = resolver, |
| 724 |
}; |
| 725 |
struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 726 |
|
| 727 |
struct connection_user_data *connections = |
| 728 |
aws_mem_calloc(allocator, app_ctx.connection_count, sizeof(struct connection_user_data)); |
| 729 |
AWS_FATAL_ASSERT(connections != NULL); |
| 730 |
|
| 731 |
s_establish_connections(&app_ctx, connections, bootstrap, &tls_connection_options); |
| 732 |
|
| 733 |
s_establish_subscriptions(&app_ctx); |
| 734 |
|
| 735 |
s_publish(&app_ctx); |
| 736 |
|
| 737 |
s_teardown_connections(&app_ctx); |
| 738 |
|
| 739 |
aws_mem_release(allocator, connections); |
| 740 |
|
| 741 |
aws_client_bootstrap_release(bootstrap); |
| 742 |
aws_host_resolver_release(resolver); |
| 743 |
aws_event_loop_group_release(el_group); |
| 744 |
|
| 745 |
if (tls_ctx) { |
| 746 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 747 |
aws_tls_ctx_release(tls_ctx); |
| 748 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 749 |
} |
| 750 |
|
| 751 |
aws_thread_join_all_managed(); |
| 752 |
|
| 753 |
const size_t outstanding_bytes = aws_mem_tracer_bytes(allocator); |
| 754 |
printf("Iteration %d summary:\n", i + 1); |
| 755 |
printf(" Successful Publishes: %d\n", app_ctx.publish_successes); |
| 756 |
printf(" Failed Publishes: %d\n", app_ctx.publish_failures); |
| 757 |
printf(" Outstanding bytes: %zu\n\n", outstanding_bytes); |
| 758 |
} |
| 759 |
|
| 760 |
if (app_ctx.log_level) { |
| 761 |
aws_logger_set(NULL); |
| 762 |
aws_logger_clean_up(&logger); |
| 763 |
} |
| 764 |
|
| 765 |
aws_uri_clean_up(&app_ctx.uri); |
| 766 |
|
| 767 |
aws_mqtt_library_clean_up(); |
| 768 |
|
| 769 |
const size_t leaked_bytes = aws_mem_tracer_bytes(allocator); |
| 770 |
if (leaked_bytes) { |
| 771 |
struct aws_logger memory_logger; |
| 772 |
AWS_ZERO_STRUCT(memory_logger); |
| 773 |
|
| 774 |
aws_logger_init_noalloc(&memory_logger, aws_default_allocator(), &options); |
| 775 |
aws_logger_set(&memory_logger); |
| 776 |
|
| 777 |
aws_mqtt_library_init(aws_default_allocator()); |
| 778 |
|
| 779 |
printf("Writing memory leaks to log.\n"); |
| 780 |
aws_mem_tracer_dump(allocator); |
| 781 |
|
| 782 |
aws_logger_set(NULL); |
| 783 |
aws_logger_clean_up(&memory_logger); |
| 784 |
|
| 785 |
aws_mqtt_library_clean_up(); |
| 786 |
} else { |
| 787 |
printf("Finished, with no memory leaks\n"); |
| 788 |
} |
| 789 |
|
| 790 |
aws_mem_tracer_destroy(allocator); |
| 791 |
|
| 792 |
return 0; |
| 793 |
} |