Debian Codegraph @

crt/aws-c-http/bin/h2benchmark/main.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/http/connection.h>
6 #include <aws/http/http2_stream_manager.h>
7 #include <aws/http/request_response.h>
8
9 #include <aws/common/clock.h>
10 #include <aws/common/command_line_parser.h>
11 #include <aws/common/condition_variable.h>
12 #include <aws/common/hash_table.h>
13 #include <aws/common/log_channel.h>
14 #include <aws/common/log_formatter.h>
15 #include <aws/common/log_writer.h>
16 #include <aws/common/mutex.h>
17 #include <aws/common/string.h>
18
19 #include <aws/io/channel_bootstrap.h>
20 #include <aws/io/event_loop.h>
21 #include <aws/io/logging.h>
22 #include <aws/io/socket.h>
23 #include <aws/io/stream.h>
24 #include <aws/io/tls_channel_handler.h>
25 #include <aws/io/uri.h>
26
27 #include <inttypes.h>
28
29 #ifdef _MSC_VER
30 # pragma warning(disable : 4204) /* Declared initializers */
31 # pragma warning(disable : 4221) /* Local var in declared initializer */
32 #endif
33
34 #define DEFINE_HEADER(NAME, VALUE) \
35 { \
36 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
37 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
38 }
39
40 /* TODO: Make those configurable from cmd line */
41 const struct aws_byte_cursor uri_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("http://localhost:3280/");
42 const int rate_secs = 30; /* Time interval to collect data */
43 const int streams_per_connection = 20;
44 const int max_connections = 8;
45 const int num_data_to_collect = 5; /* The number of data to collect */
46 const enum aws_log_level log_level = AWS_LOG_LEVEL_NONE;
47 const bool direct_connection = false; /* If true, will create one connection and make requests from that connection.
48 * If false, will use stream manager to acquire streams */
49
50 const double rate_threshold =
51 4000; /* From the previous tests. All platforms seem to be larger than 4000, but it could various. TODO: Maybe
52 gather the number of previous test run, and be platform specific. */
53
54 struct aws_http_benchmark_helper {
55 struct aws_task task;
56 struct aws_event_loop *eventloop;
57
58 int num_collected; /* number of data collected */
59 uint64_t rate_ns; /* Collect data per rate_ns */
60
61 struct aws_atomic_var benchmark_finished;
62
63 double *results;
64 };
65
66 struct benchmark_ctx {
67 struct aws_allocator *allocator;
68 const char *verb;
69 struct aws_uri uri;
70 struct aws_mutex mutex;
71 struct aws_condition_variable c_var;
72
73 enum aws_log_level log_level;
74 struct aws_http_benchmark_helper helper;
75 struct aws_event_loop_group *el_group;
76 struct aws_http2_stream_manager *manager;
77
78 bool is_shutdown_complete;
79 struct aws_atomic_var streams_failed;
80 struct aws_atomic_var streams_completed;
81
82 int batch_size;
83 struct aws_atomic_var batch_completed;
84
85 struct aws_http_connection *connection;
86 };
87
88 /************************* Data collector ******************************************/
89
90 static void s_collect_data_task(struct aws_task *task, void *arg, enum aws_task_status status) {
91 (void)status;
92 (void)task;
93
94 struct benchmark_ctx *app_ctx = arg;
95 struct aws_http_benchmark_helper *helper = &app_ctx->helper;
96
97 /* collect data */
98 size_t stream_completed = aws_atomic_exchange_int(&app_ctx->streams_completed, 0);
99
100 /* TODO: maybe collect the data somewhere instead of just printing it out. */
101 double rate = (double)stream_completed / rate_secs;
102 helper->results[helper->num_collected] = rate;
103 ++helper->num_collected;
104 printf("Loop %d: The stream completed per second is %f\n", helper->num_collected, rate);
105 if (helper->num_collected >= num_data_to_collect) {
106 /* done */
107 double sum = 0;
108 for (int i = 0; i < num_data_to_collect; i++) {
109 sum += helper->results[i];
110 }
111 double avg = sum / num_data_to_collect;
112 printf("In average, the stream completed per second is %f\n", avg);
113 aws_mem_release(app_ctx->allocator, helper->results);
114 if (avg < rate_threshold) {
115
116 fprintf(stderr, "The average result is lower than threshold (%f). Failed\n", rate_threshold);
117 exit(1);
118 }
119
120 aws_atomic_store_int(&helper->benchmark_finished, 1);
121 } else {
122 /* keep running */
123 uint64_t now = 0;
124 aws_high_res_clock_get_ticks(&now);
125 aws_event_loop_schedule_task_future(helper->eventloop, &helper->task, now + helper->rate_ns);
126 }
127 }
128
129 void aws_http_benchmark_helper_init(struct benchmark_ctx *app_ctx, struct aws_http_benchmark_helper *helper) {
130
131 helper->eventloop = aws_event_loop_group_get_next_loop(app_ctx->el_group);
132 helper->rate_ns = aws_timestamp_convert(rate_secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
133 aws_atomic_init_int(&helper->benchmark_finished, 0);
134 aws_task_init(&helper->task, s_collect_data_task, app_ctx, "data_collector");
135 helper->results = aws_mem_calloc(app_ctx->allocator, num_data_to_collect, sizeof(double));
136 uint64_t now = 0;
137 aws_high_res_clock_get_ticks(&now);
138
139 aws_event_loop_schedule_task_future(helper->eventloop, &helper->task, now + helper->rate_ns);
140 }
141
142 /************************* Stream callbacks ******************************************/
143
144 static void s_on_stream_acquired(struct aws_http_stream *stream, int error_code, void *user_data) {
145 (void)stream;
146 (void)user_data;
147
148 if (error_code) {
149 fprintf(stderr, "stream failed to be acquired from stream manager %s\n", aws_error_debug_str(error_code));
150 exit(1);
151 }
152 }
153
154 static void s_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
155 (void)stream;
156
157 struct benchmark_ctx *app_ctx = user_data;
158 aws_mutex_lock(&app_ctx->mutex);
159 aws_atomic_fetch_add(&app_ctx->batch_completed, 1);
160 if (error_code) {
161 fprintf(stderr, "stream failed to complete %s\n", aws_error_debug_str(error_code));
162 exit(1);
163 } else {
164 aws_atomic_fetch_add(&app_ctx->streams_completed, 1);
165 }
166
167 aws_mutex_unlock(&app_ctx->mutex);
168 aws_http_stream_release(stream);
169 aws_condition_variable_notify_one(&app_ctx->c_var);
170 }
171
172 /************************* Stream manager ops ******************************************/
173
174 static bool s_are_batch_completed(void *context) {
175 struct benchmark_ctx *app_ctx = context;
176 size_t completed = aws_atomic_load_int(&app_ctx->batch_completed);
177 return (int)completed >= app_ctx->batch_size;
178 }
179
180 static int s_wait_on_batch_complete(struct benchmark_ctx *app_ctx) {
181
182 aws_mutex_lock(&app_ctx->mutex);
183 int signal_error =
184 aws_condition_variable_wait_pred(&app_ctx->c_var, &app_ctx->mutex, s_are_batch_completed, app_ctx);
185 aws_mutex_unlock(&app_ctx->mutex);
186
187 return signal_error;
188 }
189
190 static void s_run_stream_manager_test(struct benchmark_ctx *app_ctx, struct aws_http_message *request) {
191 struct aws_http_make_request_options request_options = {
192 .self_size = sizeof(request_options),
193 .request = request,
194 .user_data = app_ctx,
195 .on_complete = s_on_stream_complete,
196 };
197
198 struct aws_http2_stream_manager_acquire_stream_options acquire_stream_option = {
199 .options = &request_options,
200 .callback = s_on_stream_acquired,
201 .user_data = app_ctx,
202 };
203
204 bool keep_loop = true;
205 while (keep_loop) {
206 /* Loop a batch of requests to be made and completed */
207 aws_atomic_store_int(&app_ctx->batch_completed, 0);
208
209 for (int i = 0; i < app_ctx->batch_size; ++i) {
210 aws_http2_stream_manager_acquire_stream(app_ctx->manager, &acquire_stream_option);
211 }
212 /* once the data finished collected during waiting, no more data will be collected, still wait for all
213 requests
214 * made to be completed. */
215 s_wait_on_batch_complete(app_ctx);
216 size_t streams_failed = aws_atomic_load_int(&app_ctx->streams_failed);
217 if (streams_failed > 0) {
218 fprintf(
219 stderr, "%zu stream failed to complete %s\n", streams_failed, aws_error_debug_str(aws_last_error()));
220 exit(1);
221 }
222
223 size_t finished = aws_atomic_load_int(&app_ctx->helper.benchmark_finished);
224 if (finished) {
225 keep_loop = false;
226 }
227 }
228 }
229
230 static void s_on_shutdown_complete(void *user_data) {
231 struct benchmark_ctx *app_ctx = user_data;
232
233 aws_mutex_lock(&app_ctx->mutex);
234 app_ctx->is_shutdown_complete = true;
235 aws_mutex_unlock(&app_ctx->mutex);
236 aws_condition_variable_notify_one(&app_ctx->c_var);
237 }
238
239 /************************* direct connection ops ******************************************/
240
241 static void s_run_direct_connection_test(struct benchmark_ctx *app_ctx, struct aws_http_message *request) {
242 struct aws_http_make_request_options request_options = {
243 .self_size = sizeof(request_options),
244 .request = request,
245 .user_data = app_ctx,
246 .on_complete = s_on_stream_complete,
247 };
248
249 bool keep_loop = true;
250 while (keep_loop) {
251 /* Loop a batch of requests to be made and completed */
252 aws_atomic_store_int(&app_ctx->batch_completed, 0);
253
254 for (int i = 0; i < app_ctx->batch_size; ++i) {
255 struct aws_http_stream *stream = aws_http_connection_make_request(app_ctx->connection, &request_options);
256 aws_http_stream_activate(stream);
257 }
258 /* once the data finished collected during waiting, no more data will be collected, still wait for all
259 requests
260 * made to be completed. */
261 s_wait_on_batch_complete(app_ctx);
262 size_t streams_failed = aws_atomic_load_int(&app_ctx->streams_failed);
263 if (streams_failed > 0) {
264 fprintf(
265 stderr, "%zu stream failed to complete %s\n", streams_failed, aws_error_debug_str(aws_last_error()));
266 exit(1);
267 }
268
269 size_t finished = aws_atomic_load_int(&app_ctx->helper.benchmark_finished);
270 if (finished) {
271 keep_loop = false;
272 }
273 }
274 }
275
276 static void s_on_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
277 (void)connection;
278 (void)error_code;
279 struct benchmark_ctx *app_ctx = user_data;
280
281 aws_mutex_lock(&app_ctx->mutex);
282 app_ctx->is_shutdown_complete = true;
283 aws_mutex_unlock(&app_ctx->mutex);
284 aws_condition_variable_notify_one(&app_ctx->c_var);
285 }
286
287 static void s_on_client_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
288 if (error_code) {
289 fprintf(stderr, "Failed to create connection with error %s\n", aws_error_debug_str(aws_last_error()));
290 exit(1);
291 }
292 struct benchmark_ctx *app_ctx = user_data;
293
294 aws_mutex_lock(&app_ctx->mutex);
295 app_ctx->connection = connection;
296 aws_mutex_unlock(&app_ctx->mutex);
297 aws_condition_variable_notify_one(&app_ctx->c_var);
298 }
299
300 static bool s_is_connected(void *context) {
301 struct benchmark_ctx *app_ctx = context;
302 return app_ctx->connection != NULL;
303 }
304
305 /************************* general ops ******************************************/
306
307 static bool s_is_shutdown_complete(void *context) {
308 struct benchmark_ctx *app_ctx = context;
309 return app_ctx->is_shutdown_complete;
310 }
311
312 static struct aws_http_message *s_create_request(struct benchmark_ctx *app_ctx) {
313 struct aws_http_message *request = aws_http2_message_new_request(app_ctx->allocator);
314
315 struct aws_http_header request_headers_src[] = {
316 DEFINE_HEADER(":method", "GET"),
317 {
318 .name = aws_byte_cursor_from_c_str(":scheme"),
319 .value = *aws_uri_scheme(&app_ctx->uri),
320 },
321 {
322 .name = aws_byte_cursor_from_c_str(":path"),
323 .value = *aws_uri_path(&app_ctx->uri),
324 },
325 {
326 .name = aws_byte_cursor_from_c_str(":authority"),
327 .value = *aws_uri_host_name(&app_ctx->uri),
328 },
329 };
330 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
331 return request;
332 }
333
334 static void s_run_benchmark(struct benchmark_ctx *app_ctx) {
335 aws_http_benchmark_helper_init(app_ctx, &app_ctx->helper);
336 struct aws_http_message *request = s_create_request(app_ctx);
337
338 if (direct_connection) {
339 s_run_direct_connection_test(app_ctx, request);
340 } else {
341 s_run_stream_manager_test(app_ctx, request);
342 }
343
344 aws_http_message_release(request);
345 }
346
347 int main(int argc, char **argv) {
348 (void)argc;
349 (void)argv;
350
351 struct aws_allocator *allocator = aws_default_allocator();
352
353 aws_http_library_init(allocator);
354
355 struct benchmark_ctx app_ctx;
356 AWS_ZERO_STRUCT(app_ctx);
357 app_ctx.allocator = allocator;
358 app_ctx.batch_size = max_connections * streams_per_connection;
359 app_ctx.log_level = log_level;
360
361 aws_mutex_init(&app_ctx.mutex);
362 aws_condition_variable_init(&app_ctx.c_var);
363
364 struct aws_logger logger;
365 AWS_ZERO_STRUCT(logger);
366
367 if (app_ctx.log_level) {
368 struct aws_logger_standard_options options = {
369 .level = app_ctx.log_level,
370 .file = stderr,
371 };
372
373 if (aws_logger_init_standard(&logger, allocator, &options)) {
374 fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error()));
375 exit(1);
376 }
377
378 aws_logger_set(&logger);
379 }
380 if (aws_uri_init_parse(&app_ctx.uri, allocator, &uri_cursor)) {
381 fprintf(stderr, "Failed to create uri %s\n", aws_error_debug_str(aws_last_error()));
382 exit(1);
383 }
384
385 aws_atomic_store_int(&app_ctx.streams_completed, 0);
386 aws_atomic_store_int(&app_ctx.streams_failed, 0);
387
388 bool use_tls = true;
389 uint32_t port = 443;
390
391 if (!app_ctx.uri.scheme.len && (app_ctx.uri.port == 80 || app_ctx.uri.port == 8080)) {
392 use_tls = false;
393 } else {
394 if (aws_byte_cursor_eq_c_str_ignore_case(&app_ctx.uri.scheme, "http")) {
395 use_tls = false;
396 }
397 }
398
399 struct aws_tls_ctx *tls_ctx = NULL;
400 struct aws_tls_ctx_options tls_ctx_options;
401 AWS_ZERO_STRUCT(tls_ctx_options);
402 struct aws_tls_connection_options tls_connection_options;
403 AWS_ZERO_STRUCT(tls_connection_options);
404 struct aws_tls_connection_options *tls_options = NULL;
405
406 if (use_tls) {
407 aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator);
408 aws_tls_ctx_options_set_verify_peer(&tls_ctx_options, false);
409
410 if (aws_tls_ctx_options_set_alpn_list(&tls_ctx_options, "h2;http/1.1")) {
411 fprintf(stderr, "Failed to load alpn list with error %s.", aws_error_debug_str(aws_last_error()));
412 exit(1);
413 }
414
415 tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options);
416
417 if (!tls_ctx) {
418 fprintf(stderr, "Failed to initialize TLS context with error %s.", aws_error_debug_str(aws_last_error()));
419 exit(1);
420 }
421
422 aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_ctx);
423 if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &app_ctx.uri.host_name)) {
424 fprintf(stderr, "Failed to set servername with error %s.", aws_error_debug_str(aws_last_error()));
425 exit(1);
426 }
427 tls_options = &tls_connection_options;
428
429 if (app_ctx.uri.port) {
430 port = app_ctx.uri.port;
431 }
432 } else {
433 port = 80;
434 if (app_ctx.uri.port) {
435 port = app_ctx.uri.port;
436 }
437 }
438
439 app_ctx.el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
440
441 struct aws_host_resolver_default_options resolver_options = {
442 .el_group = app_ctx.el_group,
443 .max_entries = 8,
444 };
445
446 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
447
448 struct aws_client_bootstrap_options bootstrap_options = {
449 .event_loop_group = app_ctx.el_group,
450 .host_resolver = resolver,
451 };
452 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
453
454 struct aws_socket_options socket_options = {
455 .type = AWS_SOCKET_STREAM,
456 .connect_timeout_ms = 3000,
457 .keep_alive_timeout_sec = 0,
458 .keepalive = false,
459 .keep_alive_interval_sec = 0,
460 };
461 if (!direct_connection) {
462 struct aws_http2_stream_manager_options sm_options = {
463 .bootstrap = bootstrap,
464 .socket_options = &socket_options,
465 .tls_connection_options = use_tls ? tls_options : NULL,
466 .host = app_ctx.uri.host_name,
467 .port = port,
468 .max_connections = max_connections,
469 .max_concurrent_streams_per_connection = streams_per_connection,
470 .http2_prior_knowledge = !use_tls,
471 .shutdown_complete_user_data = &app_ctx,
472 .shutdown_complete_callback = s_on_shutdown_complete,
473 };
474 app_ctx.manager = aws_http2_stream_manager_new(allocator, &sm_options);
475 } else {
476 struct aws_http_client_connection_options http_client_options = {
477 .self_size = sizeof(struct aws_http_client_connection_options),
478 .socket_options = &socket_options,
479 .allocator = allocator,
480 .port = port,
481 .host_name = app_ctx.uri.host_name,
482 .bootstrap = bootstrap,
483 .initial_window_size = SIZE_MAX,
484 .tls_options = tls_options,
485 .user_data = &app_ctx,
486 .on_setup = s_on_client_connection_setup,
487 .on_shutdown = s_on_connection_shutdown,
488 };
489 if (aws_http_client_connect(&http_client_options)) {
490 exit(1);
491 }
492 aws_mutex_lock(&app_ctx.mutex);
493 aws_condition_variable_wait_pred(&app_ctx.c_var, &app_ctx.mutex, s_is_connected, &app_ctx);
494 aws_mutex_unlock(&app_ctx.mutex);
495 }
496
497 /* Really do the job */
498 s_run_benchmark(&app_ctx);
499
500 if (!direct_connection) {
501 aws_http2_stream_manager_release(app_ctx.manager);
502 } else {
503 aws_http_connection_release(app_ctx.connection);
504 }
505
506 aws_mutex_lock(&app_ctx.mutex);
507 aws_condition_variable_wait_pred(&app_ctx.c_var, &app_ctx.mutex, s_is_shutdown_complete, &app_ctx);
508 aws_mutex_unlock(&app_ctx.mutex);
509
510 aws_client_bootstrap_release(bootstrap);
511 aws_host_resolver_release(resolver);
512 aws_event_loop_group_release(app_ctx.el_group);
513
514 if (tls_ctx) {
515 aws_tls_connection_options_clean_up(&tls_connection_options);
516 aws_tls_ctx_release(tls_ctx);
517 aws_tls_ctx_options_clean_up(&tls_ctx_options);
518 }
519
520 aws_http_library_clean_up();
521
522 if (app_ctx.log_level) {
523 aws_logger_clean_up(&logger);
524 }
525
526 aws_uri_clean_up(&app_ctx.uri);
527
528 return 0;
529 }