1515
1616package software .amazon .awssdk .benchmark .apache5 ;
1717
18- import java .time .Duration ;
19- import java .util .ArrayList ;
20- import java .util .List ;
18+
2119import java .util .concurrent .ExecutorService ;
2220import java .util .concurrent .Executors ;
2321import java .util .concurrent .Future ;
4341import software .amazon .awssdk .http .apache5 .Apache5HttpClient ;
4442import software .amazon .awssdk .regions .Region ;
4543import software .amazon .awssdk .services .s3 .S3Client ;
46- import software .amazon .awssdk .utils .Logger ;
44+
45+ import java .time .Duration ;
46+ import java .util .ArrayList ;
47+ import java .util .List ;
48+ import java .util .logging .Logger ;
49+ /*
50+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
51+ *
52+ * Licensed under the Apache License, Version 2.0 (the "License").
53+ * You may not use this file except in compliance with the License.
54+ * A copy of the License is located at
55+ *
56+ * http://aws.amazon.com/apache2.0
57+ *
58+ * or in the "license" file accompanying this file. This file is distributed
59+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
60+ * express or implied. See the License for the specific language governing
61+ * permissions and limitations under the License.
62+ */
4763
4864@ BenchmarkMode (Mode .Throughput )
4965@ OutputTimeUnit (TimeUnit .SECONDS )
5268@ Warmup (iterations = 3 , time = 15 , timeUnit = TimeUnit .SECONDS )
5369@ Measurement (iterations = 5 , time = 10 , timeUnit = TimeUnit .SECONDS )
5470public class Apache5Benchmark implements CoreBenchmark {
55- private static final Logger logger = Logger .loggerFor (Apache5Benchmark .class );
71+ private static final Logger logger = Logger .getLogger (Apache5Benchmark .class . getName () );
5672
57- @ Param ("50" )
73+ @ Param ({ "50" } )
5874 private int maxConnections ;
5975
60- @ Param ("20" )
76+ @ Param ({ "10" } )
6177 private int threadCount ;
6278
79+ @ Param ({"platform" })
80+ private String executorType ;
81+
6382 private S3Client s3Client ;
64- private S3BenchmarkImpl s3Benchmark ;
65- private ExecutorService executor ;
83+ private S3BenchmarkImpl benchmark ;
84+ private ExecutorService executorService ;
6685
6786 @ Setup (Level .Trial )
6887 public void setup () {
69- logger .info (() -> "Setting up Apache5 benchmark with " + threadCount + " threads" );
88+ logger .info ("Setting up Apache5 benchmark with maxConnections= " + maxConnections );
7089
90+ // Apache 5 HTTP client
7191 SdkHttpClient httpClient = Apache5HttpClient .builder ()
92+ .maxConnections (maxConnections )
7293 .connectionTimeout (Duration .ofSeconds (10 ))
7394 .socketTimeout (Duration .ofSeconds (30 ))
7495 .connectionAcquisitionTimeout (Duration .ofSeconds (10 ))
75- .maxConnections (maxConnections )
96+ .connectionMaxIdleTime (Duration .ofSeconds (60 ))
97+ .connectionTimeToLive (Duration .ofMinutes (5 ))
98+ .useIdleConnectionReaper (true )
7699 .build ();
77100
101+ // S3 client
78102 s3Client = S3Client .builder ()
79103 .region (Region .US_WEST_2 )
80104 .credentialsProvider (DefaultCredentialsProvider .create ())
81105 .httpClient (httpClient )
82106 .build ();
83107
84- s3Benchmark = new S3BenchmarkImpl (s3Client );
85- s3Benchmark .setup ();
108+ // Initialize benchmark implementation
109+ benchmark = new S3BenchmarkImpl (s3Client );
110+ benchmark .setup ();
86111
87- executor = Executors .newFixedThreadPool (threadCount );
88- }
112+ // Always use platform threads
113+ executorService = Executors .newFixedThreadPool (threadCount , r -> {
114+ Thread t = new Thread (r );
115+ t .setName ("apache5-platform-worker-" + t .getId ());
116+ return t ;
117+ });
118+ logger .info ("Using platform thread executor" );
89119
90- @ TearDown (Level .Trial )
91- public void teardown () {
92- logger .info (() -> "Tearing down Apache5 benchmark" );
93- s3Benchmark .cleanup ();
94- s3Client .close ();
95- executor .shutdown ();
96- try {
97- if (!executor .awaitTermination (10 , TimeUnit .SECONDS )) {
98- executor .shutdownNow ();
99- }
100- } catch (InterruptedException e ) {
101- executor .shutdownNow ();
102- Thread .currentThread ().interrupt ();
103- }
120+ logger .info ("Apache5 benchmark setup complete" );
104121 }
105122
106123 @ Benchmark
107124 @ Override
108125 public void simpleGet (Blackhole blackhole ) throws Exception {
109- s3Benchmark .executeGet ("5MB " , blackhole );
126+ benchmark .executeGet ("medium " , blackhole );
110127 }
111128
112129 @ Benchmark
113130 @ Override
114- public void simplePut (Blackhole blackhole ) {
115- s3Benchmark .executePut ("5MB " , blackhole );
131+ public void simplePut (Blackhole blackhole ) throws Exception {
132+ benchmark .executePut ("medium " , blackhole );
116133 }
117134
118135 @ Benchmark
119136 @ Override
120137 public void multiThreadedGet (Blackhole blackhole ) throws Exception {
121- List <Future <?>> futures = new ArrayList <>();
138+ List <Future <?>> futures = new ArrayList <>(threadCount );
139+
122140 for (int i = 0 ; i < threadCount ; i ++) {
123- futures .add (executor .submit (() -> {
141+ futures .add (executorService .submit (() -> {
124142 try {
125- s3Benchmark .executeGet ("5MB " , blackhole );
143+ benchmark .executeGet ("medium " , blackhole );
126144 } catch (Exception e ) {
127- throw new RuntimeException (e );
145+ throw new RuntimeException ("GET operation failed" , e );
128146 }
129147 }));
130148 }
131149
150+ // Wait for all operations to complete
132151 for (Future <?> future : futures ) {
133152 future .get ();
134153 }
@@ -137,13 +156,45 @@ public void multiThreadedGet(Blackhole blackhole) throws Exception {
137156 @ Benchmark
138157 @ Override
139158 public void multiThreadedPut (Blackhole blackhole ) throws Exception {
140- List <Future <?>> futures = new ArrayList <>();
159+ List <Future <?>> futures = new ArrayList <>(threadCount );
160+
141161 for (int i = 0 ; i < threadCount ; i ++) {
142- futures .add (executor .submit (() -> s3Benchmark .executePut ("5MB" , blackhole )));
162+ futures .add (executorService .submit (() -> {
163+ try {
164+ benchmark .executePut ("medium" , blackhole );
165+ } catch (Exception e ) {
166+ throw new RuntimeException ("PUT operation failed" , e );
167+ }
168+ }));
143169 }
144170
171+ // Wait for all operations to complete
145172 for (Future <?> future : futures ) {
146173 future .get ();
147174 }
148175 }
176+
177+ @ TearDown (Level .Trial )
178+ public void tearDown () {
179+ logger .info ("Tearing down Apache5 benchmark" );
180+
181+ if (executorService != null ) {
182+ executorService .shutdown ();
183+ try {
184+ if (!executorService .awaitTermination (30 , TimeUnit .SECONDS )) {
185+ executorService .shutdownNow ();
186+ }
187+ } catch (InterruptedException e ) {
188+ executorService .shutdownNow ();
189+ }
190+ }
191+
192+ if (benchmark != null ) {
193+ benchmark .cleanup ();
194+ }
195+
196+ if (s3Client != null ) {
197+ s3Client .close ();
198+ }
199+ }
149200}
0 commit comments