forked from awslabs/aws-crt-s3-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRunner.cpp
More file actions
493 lines (428 loc) · 18.2 KB
/
CRunner.cpp
File metadata and controls
493 lines (428 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "BenchmarkRunner.h"
#include <aws/auth/credentials.h>
#include <aws/common/string.h>
#include <aws/common/system_resource_util.h>
#include <aws/http/connection.h>
#include <aws/http/request_response.h>
#include <aws/io/channel_bootstrap.h>
#include <aws/io/event_loop.h>
#include <aws/io/host_resolver.h>
#include <aws/io/stream.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/s3/s3_client.h>
#include <future>
#include <iomanip>
#include <list>
#include <sstream>
using namespace std;
// Read-backpressure (feature added Sept 2022) can prevent running out of
// memory due to downloading data faster than we can write it to disk.
// 256MiB is Java Transfer Mgr V2's default initial window (as of Aug 2024).
// Unfortunately, this hurts the performance of single-file workloads
// due to limiting the number of parts in-flight for a given file.
// But the effect goes away if there are lots of files in a workload,
// because the total number of parts in-flight gets high enough.
//
// The memory-limiter (feature added 1 yr later in Nov 2023) is another way
// to prevent running out of memory.
//
// This benchmark can turn off backpressure and rely solely on the memory-limiter,
// since it always processes data synchronously within the body callback.
// #define BACKPRESSURE_INITIAL_READ_WINDOW_MiB 256 /* If commented out, backpressure is disabled */
aws_byte_cursor toCursor(string_view src)
{
return aws_byte_cursor{.len = src.length(), .ptr = (uint8_t *)src.data()};
}
// Benchmark runner using aws-c-s3 directly
class CRunner : public BenchmarkRunner
{
public:
// CRT boilerplate
aws_allocator *alloc = NULL;
aws_logger logger;
aws_event_loop_group *eventLoopGroup = NULL;
aws_host_resolver *hostResolver = NULL;
aws_client_bootstrap *clientBootstrap = NULL;
aws_tls_ctx *tlsCtx = NULL;
aws_credentials_provider *credentialsProvider = NULL;
aws_s3_client *s3Client = NULL;
string telemetryFileBasePath = "";
// derived from bucket and region (e.g. mybucket.s3.us-west-2.amazonaws.com)
string endpoint;
public:
// Instantiates S3 Client, does not run the benchmark yet
CRunner(const BenchmarkConfig &config);
~CRunner() override;
// A benchmark can be run repeatedly
void run(size_t runNumber) override;
friend class Task;
};
// A runnable task
class Task
{
CRunner &runner;
size_t taskI;
TaskConfig &config;
aws_s3_meta_request *metaRequest;
promise<void> donePromise;
future<void> doneFuture;
FILE *telemetryFile;
static void onTelemetry(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request_metrics *metrics,
void *user_data);
static void onFinished(
struct aws_s3_meta_request *meta_request,
const struct aws_s3_meta_request_result *meta_request_result,
void *user_data);
public:
// Creates the task and begins its work
Task(CRunner &runner, size_t taskI, FILE *telemetryFile);
void waitUntilDone() { return doneFuture.wait(); }
};
std::unique_ptr<BenchmarkRunner> createCRunner(const BenchmarkConfig &config)
{
return make_unique<CRunner>(config);
}
// Instantiates S3 Client, does not run the benchmark yet
CRunner::CRunner(const BenchmarkConfig &config) : BenchmarkRunner(config)
{
bool isS3Express = config.bucket.ends_with("--x-s3");
if (isS3Express)
{
// extract the "usw2-az3" from "mybucket--usw2-az3--x-s3"
string substrNoSuffix = config.bucket.substr(0, config.bucket.rfind("--"));
string azID = substrNoSuffix.substr(substrNoSuffix.rfind("--") + 2);
// Endpoint looks like: mybucket--usw2-az3--x-s3.s3express-usw2-az3.us-west-2.amazonaws.com
this->endpoint = config.bucket;
this->endpoint += ".s3express-";
this->endpoint += azID;
this->endpoint += ".";
this->endpoint += config.region;
this->endpoint += ".amazonaws.com";
}
else
{
// Standard S3 endpoint looks like: mybucket.s3.us-west-2.amazonaws.com
this->endpoint = config.bucket;
this->endpoint += ".s3.";
this->endpoint += config.region;
this->endpoint += ".amazonaws.com";
}
alloc = aws_default_allocator();
aws_s3_library_init(alloc);
struct aws_logger_standard_options logOpts;
AWS_ZERO_STRUCT(logOpts);
logOpts.level = AWS_LL_ERROR;
logOpts.file = stderr;
AWS_FATAL_ASSERT(aws_logger_init_standard(&logger, alloc, &logOpts) == 0);
aws_logger_set(&logger);
eventLoopGroup = aws_event_loop_group_new_default(alloc, 0 /*max-threads*/, NULL /*shutdown-options*/);
AWS_FATAL_ASSERT(eventLoopGroup != NULL);
aws_host_resolver_default_options resolverOpts;
AWS_ZERO_STRUCT(resolverOpts);
resolverOpts.max_entries = 8;
resolverOpts.el_group = eventLoopGroup;
hostResolver = aws_host_resolver_new_default(alloc, &resolverOpts);
AWS_FATAL_ASSERT(hostResolver != NULL);
aws_client_bootstrap_options bootstrapOpts;
AWS_ZERO_STRUCT(bootstrapOpts);
bootstrapOpts.event_loop_group = eventLoopGroup;
bootstrapOpts.host_resolver = hostResolver;
clientBootstrap = aws_client_bootstrap_new(alloc, &bootstrapOpts);
AWS_FATAL_ASSERT(clientBootstrap != NULL);
aws_tls_ctx_options tlsCtxOpts;
aws_tls_ctx_options_init_default_client(&tlsCtxOpts, alloc);
tlsCtx = aws_tls_client_ctx_new(alloc, &tlsCtxOpts);
AWS_FATAL_ASSERT(tlsCtx != NULL);
aws_tls_connection_options tlsConnOpts;
aws_tls_connection_options_init_from_ctx(&tlsConnOpts, tlsCtx);
aws_credentials_provider_chain_default_options providerOpts;
AWS_ZERO_STRUCT(providerOpts);
providerOpts.bootstrap = clientBootstrap;
providerOpts.tls_ctx = tlsCtx;
credentialsProvider = aws_credentials_provider_new_chain_default(alloc, &providerOpts);
AWS_FATAL_ASSERT(credentialsProvider != NULL);
aws_signing_config_aws signingConfig;
aws_s3_init_default_signing_config(&signingConfig, toCursor(config.region), credentialsProvider);
aws_s3_client_config s3ClientConfig;
AWS_ZERO_STRUCT(s3ClientConfig);
s3ClientConfig.region = toCursor(config.region);
s3ClientConfig.client_bootstrap = clientBootstrap;
s3ClientConfig.tls_connection_options = &tlsConnOpts;
s3ClientConfig.signing_config = &signingConfig;
s3ClientConfig.part_size = PART_SIZE;
s3ClientConfig.throughput_target_gbps = config.targetThroughputGbps;
if (isS3Express)
{
signingConfig.algorithm = AWS_SIGNING_ALGORITHM_V4_S3EXPRESS;
s3ClientConfig.enable_s3express = true;
}
struct aws_byte_cursor *networkInterfaceNamesArray = NULL;
if (config.networkInterfaceNames.size())
{
networkInterfaceNamesArray = (struct aws_byte_cursor *)aws_mem_calloc(
alloc, config.networkInterfaceNames.size(), sizeof(struct aws_byte_cursor));
for (size_t i = 0; i < config.networkInterfaceNames.size(); i++)
{
networkInterfaceNamesArray[i] = aws_byte_cursor_from_c_str(config.networkInterfaceNames[i].c_str());
}
s3ClientConfig.num_network_interface_names = config.networkInterfaceNames.size();
s3ClientConfig.network_interface_names_array = networkInterfaceNamesArray;
}
#if defined(BACKPRESSURE_INITIAL_READ_WINDOW_MiB)
// If writing data to disk, enable backpressure.
// This prevents us from running out of memory due to downloading
// data faster than we can write it to disk.
if (config.filesOnDisk)
{
s3ClientConfig.enable_read_backpressure = true;
s3ClientConfig.initial_read_window = bytesFromMiB(BACKPRESSURE_INITIAL_READ_WINDOW_MiB);
}
#endif
// struct aws_http_connection_monitoring_options httpMonitoringOpts;
// AWS_ZERO_STRUCT(httpMonitoringOpts);
// httpMonitoringOpts.minimum_throughput_bytes_per_second = 1;
// httpMonitoringOpts.allowable_throughput_failure_interval_milliseconds = 750;
// s3ClientConfig.monitoring_options = &httpMonitoringOpts;
s3Client = aws_s3_client_new(alloc, &s3ClientConfig);
if (s3Client == NULL)
{
fail(string("Unable to create S3Client. Probably wrong network interface names?"));
}
telemetryFileBasePath = config.telemetryFileBasePath;
if (networkInterfaceNamesArray)
{
aws_mem_release(alloc, networkInterfaceNamesArray);
}
}
CRunner::~CRunner()
{
s3Client = aws_s3_client_release(s3Client);
credentialsProvider = aws_credentials_provider_release(credentialsProvider);
aws_tls_ctx_release(tlsCtx);
aws_tls_ctx_release(tlsCtx);
tlsCtx = NULL;
aws_client_bootstrap_release(clientBootstrap);
clientBootstrap = NULL;
aws_host_resolver_release(hostResolver);
hostResolver = NULL;
aws_event_loop_group_release(eventLoopGroup);
eventLoopGroup = NULL;
aws_s3_library_clean_up();
}
void CRunner::run(size_t runNumber)
{
FILE *telemetryFile = NULL;
if (!telemetryFileBasePath.empty())
{
stringstream filePath;
filePath << telemetryFileBasePath << "/";
// pad the numbers like 01,02 instead 1,2 for asciibetically sorting.
filePath << setfill('0') << setw(2) << runNumber;
filePath << ".csv";
telemetryFile = fopen(filePath.str().c_str(), "w");
telemetryFile = fopen(filePath.str().c_str(), "w");
}
// kick off all tasks
list<Task> runningTasks;
for (size_t i = 0; i < config.tasks.size(); ++i)
runningTasks.emplace_back(*this, i, telemetryFile);
// wait until all tasks are done
for (auto &&task : runningTasks)
task.waitUntilDone();
if (telemetryFile != NULL)
{
fclose(telemetryFile);
}
}
void addHeader(aws_http_message *request, string_view name, string_view value)
{
aws_http_header header = {toCursor(name), toCursor(value)};
aws_http_message_add_header(request, header);
}
Task::Task(CRunner &runner, size_t taskI, FILE *telemetryFile)
: runner(runner), taskI(taskI), config(runner.config.tasks[taskI]), donePromise(),
doneFuture(donePromise.get_future())
{
aws_s3_meta_request_options options;
AWS_ZERO_STRUCT(options);
options.user_data = this;
options.finish_callback = Task::onFinished;
// TODO: add "sizeHint" to config, if true then set options.object_size_hint.
// A transfer-manager downloading a directory would know the object size ahead of time.
// Size hint could have a big performance impact when downloading lots of
// small files and validating checksums.
auto request = aws_http_message_new_request(runner.alloc);
options.message = request;
addHeader(request, "Host", runner.endpoint);
aws_http_message_set_request_path(request, toCursor(string("/") + config.key));
aws_input_stream *inMemoryStreamForUpload = NULL;
if (config.action == "upload")
{
options.type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT;
aws_http_message_set_request_method(request, toCursor("PUT"));
addHeader(request, "Content-Length", to_string(config.size));
addHeader(request, "Content-Type", "application/octet-stream");
if (runner.config.filesOnDisk)
options.send_filepath = toCursor(config.key);
else
{
// set up input-stream that uploads random data from a buffer
auto randomDataCursor =
aws_byte_cursor_from_array(runner.randomDataForUpload.data(), runner.randomDataForUpload.size());
auto inMemoryStreamForUpload = aws_input_stream_new_from_cursor(runner.alloc, &randomDataCursor);
aws_http_message_set_body_stream(request, inMemoryStreamForUpload);
aws_input_stream_release(inMemoryStreamForUpload);
}
}
else if (config.action == "download")
{
options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
aws_http_message_set_request_method(request, toCursor("GET"));
addHeader(request, "Content-Length", "0");
if (runner.config.filesOnDisk)
{
options.recv_filepath = toCursor(config.key);
}
}
else
fail(string("Unknown task action: ") + config.action);
aws_s3_checksum_config checksumConfig;
AWS_ZERO_STRUCT(checksumConfig);
if (!runner.config.checksum.empty())
{
if (runner.config.checksum == "CRC32")
checksumConfig.checksum_algorithm = AWS_SCA_CRC32;
else if (runner.config.checksum == "CRC32C")
checksumConfig.checksum_algorithm = AWS_SCA_CRC32C;
else if (runner.config.checksum == "SHA1")
checksumConfig.checksum_algorithm = AWS_SCA_SHA1;
else if (runner.config.checksum == "SHA256")
checksumConfig.checksum_algorithm = AWS_SCA_SHA256;
else
fail(string("Unknown checksum: ") + runner.config.checksum);
checksumConfig.location = AWS_SCL_HEADER;
checksumConfig.validate_response_checksum = true;
options.checksum_config = &checksumConfig;
}
if (telemetryFile != NULL)
{
options.telemetry_callback = Task::onTelemetry;
this->telemetryFile = telemetryFile;
fprintf(
telemetryFile,
"request_id,start_time,end_time,total_duration_ns,"
"send_start_time,send_end_time,sending_duration_ns,"
"receive_start_time,receive_end_time,receiving_duration_ns,"
"response_status,request_path_query,host_address,"
"ip_address,connection_id,thread_id,stream_id,"
"operation_name\n");
}
metaRequest = aws_s3_client_make_meta_request(runner.s3Client, &options);
AWS_FATAL_ASSERT(metaRequest != NULL);
aws_http_message_release(request);
}
void Task::onTelemetry(
struct aws_s3_meta_request *meta_request,
struct aws_s3_request_metrics *metrics,
void *user_data)
{
int error_code = aws_s3_request_metrics_get_error_code(metrics);
if (error_code != 0)
{
return;
}
Task *task = static_cast<Task *>(user_data);
// Variables to hold the metric values
const struct aws_string *request_id = nullptr;
uint64_t start_time, end_time, total_duration;
uint64_t send_start_time, send_end_time, sending_duration;
uint64_t receive_start_time, receive_end_time, receiving_duration, part_number;
int response_status;
const struct aws_string *request_path_query = nullptr;
const struct aws_string *host_address = nullptr;
const struct aws_string *ip_address = nullptr;
size_t connection_id;
aws_thread_id_t thread_id;
uint32_t stream_id;
const struct aws_string *operation_name = nullptr;
enum aws_s3_request_type request_type;
// Retrieve metrics
aws_s3_request_metrics_get_request_id(metrics, &request_id);
aws_s3_request_metrics_get_start_timestamp_ns(metrics, &start_time);
aws_s3_request_metrics_get_end_timestamp_ns(metrics, &end_time);
aws_s3_request_metrics_get_total_duration_ns(metrics, &total_duration);
aws_s3_request_metrics_get_send_start_timestamp_ns(metrics, &send_start_time);
aws_s3_request_metrics_get_send_end_timestamp_ns(metrics, &send_end_time);
aws_s3_request_metrics_get_sending_duration_ns(metrics, &sending_duration);
aws_s3_request_metrics_get_receive_start_timestamp_ns(metrics, &receive_start_time);
aws_s3_request_metrics_get_receive_end_timestamp_ns(metrics, &receive_end_time);
aws_s3_request_metrics_get_receiving_duration_ns(metrics, &receiving_duration);
aws_s3_request_metrics_get_response_status_code(metrics, &response_status);
aws_s3_request_metrics_get_request_path_query(metrics, &request_path_query);
aws_s3_request_metrics_get_host_address(metrics, &host_address);
aws_s3_request_metrics_get_ip_address(metrics, &ip_address);
aws_s3_request_metrics_get_connection_id(metrics, &connection_id);
aws_s3_request_metrics_get_thread_id(metrics, &thread_id);
aws_s3_request_metrics_get_request_stream_id(metrics, &stream_id);
aws_s3_request_metrics_get_operation_name(metrics, &operation_name);
// Write the metrics data
std::stringstream ss;
ss << aws_string_c_str(request_id) << "," << start_time << "," << end_time << "," << total_duration << ","
<< send_start_time << "," << send_end_time << "," << sending_duration << "," << receive_start_time << ","
<< receive_end_time << "," << receiving_duration << "," << response_status << ","
<< aws_string_c_str(request_path_query) << "," << aws_string_c_str(host_address) << ","
<< aws_string_c_str(ip_address) << "," << connection_id << "," << thread_id << "," << stream_id << ","
<< aws_string_c_str(operation_name) << std::endl;
fprintf(task->telemetryFile, "%s", ss.str().c_str());
}
void Task::onFinished(
struct aws_s3_meta_request *meta_request,
const struct aws_s3_meta_request_result *meta_request_result,
void *user_data)
{
Task *task = static_cast<Task *>(user_data);
// TODO: report failed meta-requests instead of killing benchmark?
if (meta_request_result->error_code != 0)
{
printf(
"Task[%zu] failed. action:%s key:%s error_code:%s\n",
task->taskI,
task->config.action.c_str(),
task->config.key.c_str(),
aws_error_name(meta_request_result->error_code));
if (meta_request_result->response_status != 0)
printf("Status-Code: %d\n", meta_request_result->response_status);
aws_http_headers *headers = meta_request_result->error_response_headers;
if (headers != NULL)
{
for (size_t i = 0; i < aws_http_headers_count(headers); ++i)
{
aws_http_header headerI;
aws_http_headers_get_index(headers, i, &headerI);
printf(
PRInSTR ": " PRInSTR "\n", AWS_BYTE_CURSOR_PRI(headerI.name), AWS_BYTE_CURSOR_PRI(headerI.value));
}
}
aws_byte_buf *body = meta_request_result->error_response_body;
if (body != NULL && body->len > 0)
printf(PRInSTR "\n", AWS_BYTE_BUF_PRI(*body));
fail("S3MetaRequest failed");
}
// clean up task
aws_s3_meta_request_release(task->metaRequest);
task->donePromise.set_value();
}
int main(int argc, char *argv[])
{
return benchmarkRunnerMain(
argc,
argv,
[](string_view id, const BenchmarkConfig &config)
{
if (id == "crt-c")
return createCRunner(config);
fail("Unsupported S3_CLIENT. Options are: crt-c");
});
}