1+ /* *
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ * SPDX-License-Identifier: Apache-2.0.
4+ */
5+
6+ #include < aws/core/utils/StringUtils.h>
7+ #include < aws/core/utils/memory/stl/AWSAllocator.h>
8+ #include < aws/core/utils/memory/stl/AWSString.h>
9+ #include < aws/core/utils/memory/stl/AWSStringStream.h>
10+ #include < aws/s3/S3Client.h>
11+ #include < aws/s3/model/BucketInfo.h>
12+ #include < aws/s3/model/BucketType.h>
13+ #include < aws/s3/model/CreateBucketConfiguration.h>
14+ #include < aws/s3/model/CreateBucketRequest.h>
15+ #include < aws/s3/model/DataRedundancy.h>
16+ #include < aws/s3/model/DeleteBucketRequest.h>
17+ #include < aws/s3/model/DeleteObjectRequest.h>
18+ #include < aws/s3/model/GetObjectRequest.h>
19+ #include < aws/s3/model/LocationType.h>
20+ #include < aws/s3/model/PutObjectRequest.h>
21+ #include < performance-tests/Utils.h>
22+ #include < performance-tests/services/s3/S3PerformanceTest.h>
23+
24+ void PerformanceTest::Services::S3::RunTest (Aws::S3::S3Client& s3, const TestCase& config, const Aws::String& availabilityZoneId,
25+ int iterations) {
26+ auto bucketName = SetupBucket (s3, config, availabilityZoneId);
27+ if (bucketName.empty ()) {
28+ return ;
29+ }
30+
31+ RunOperations (s3, bucketName, config, iterations);
32+ CleanupResources (s3, bucketName, iterations);
33+ }
34+
35+ Aws::String PerformanceTest::Services::S3::SetupBucket (Aws::S3::S3Client& s3, const TestCase& config,
36+ const Aws::String& availabilityZoneId) {
37+ Aws::String bucketName;
38+ Aws::S3::Model::CreateBucketRequest cbr;
39+ Aws::String const bucketId = PerformanceTest::Utils::GenerateUniqueId ();
40+
41+ if (config.bucketTypeLabel == " s3-express" ) {
42+ bucketName = " perf-express-" + bucketId + " --" + availabilityZoneId + " --x-s3" ;
43+ cbr.SetBucket (bucketName);
44+ Aws::S3::Model::CreateBucketConfiguration bucketConfig;
45+ bucketConfig.SetLocation (
46+ Aws::S3::Model::LocationInfo ().WithType (Aws::S3::Model::LocationType::AvailabilityZone).WithName (availabilityZoneId));
47+
48+ bucketConfig.SetBucket (Aws::S3::Model::BucketInfo ()
49+ .WithType (Aws::S3::Model::BucketType::Directory)
50+ .WithDataRedundancy (Aws::S3::Model::DataRedundancy::SingleAvailabilityZone));
51+
52+ cbr.SetCreateBucketConfiguration (bucketConfig);
53+ } else {
54+ bucketName = " perf-standard-" + bucketId;
55+ cbr.SetBucket (bucketName);
56+ }
57+
58+ auto createOutcome = s3.CreateBucket (cbr);
59+ if (!createOutcome.IsSuccess ()) {
60+ PerformanceTest::Utils::LogError (" S3" , " CreateBucket" , createOutcome.GetError ().GetMessage ());
61+ return " " ;
62+ }
63+
64+ return bucketName;
65+ }
66+
67+ void PerformanceTest::Services::S3::RunOperations (Aws::S3::S3Client& s3, const Aws::String& bucketName, const TestCase& config,
68+ int iterations) {
69+ const auto randomPayload = PerformanceTest::Utils::RandomString (config.sizeBytes );
70+
71+ // Run PutObject multiple times
72+ for (int i = 0 ; i < iterations; i++) {
73+ auto stream = Aws::MakeShared<Aws::StringStream>(" PerfStream" );
74+ *stream << randomPayload;
75+
76+ Aws::S3::Model::PutObjectRequest por;
77+ por.WithBucket (bucketName).WithKey (" test-object-" + Aws::Utils::StringUtils::to_string (i)).SetBody (stream);
78+ por.SetAdditionalCustomHeaderValue (" test-dimension-size" , config.sizeLabel );
79+ por.SetAdditionalCustomHeaderValue (" test-dimension-bucket-type" , config.bucketTypeLabel );
80+ auto putOutcome = s3.PutObject (por);
81+ if (!putOutcome.IsSuccess ()) {
82+ PerformanceTest::Utils::LogError (" S3" , " PutObject" , putOutcome.GetError ().GetMessage ());
83+ }
84+ }
85+
86+ // Run GetObject multiple times
87+ for (int i = 0 ; i < iterations; i++) {
88+ Aws::S3::Model::GetObjectRequest gor;
89+ gor.WithBucket (bucketName).WithKey (" test-object-" + Aws::Utils::StringUtils::to_string (i));
90+ gor.SetAdditionalCustomHeaderValue (" test-dimension-size" , config.sizeLabel );
91+ gor.SetAdditionalCustomHeaderValue (" test-dimension-bucket-type" , config.bucketTypeLabel );
92+ auto getOutcome = s3.GetObject (gor);
93+ if (!getOutcome.IsSuccess ()) {
94+ PerformanceTest::Utils::LogError (" S3" , " GetObject" , getOutcome.GetError ().GetMessage ());
95+ }
96+ }
97+ }
98+
99+ void PerformanceTest::Services::S3::CleanupResources (Aws::S3::S3Client& s3, const Aws::String& bucketName, int iterations) {
100+ for (int i = 0 ; i < iterations; i++) {
101+ auto deleteObjectOutcome = s3.DeleteObject (
102+ Aws::S3::Model::DeleteObjectRequest ().WithBucket (bucketName).WithKey (" test-object-" + Aws::Utils::StringUtils::to_string (i)));
103+ if (!deleteObjectOutcome.IsSuccess ()) {
104+ PerformanceTest::Utils::LogError (" S3" , " DeleteObject" , deleteObjectOutcome.GetError ().GetMessage ());
105+ }
106+ }
107+
108+ auto deleteBucketOutcome = s3.DeleteBucket (Aws::S3::Model::DeleteBucketRequest ().WithBucket (bucketName));
109+ if (!deleteBucketOutcome.IsSuccess ()) {
110+ PerformanceTest::Utils::LogError (" S3" , " DeleteBucket" , deleteBucketOutcome.GetError ().GetMessage ());
111+ }
112+ }
0 commit comments