• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.benchmark.apicall.httpclient.async;
17 
18 import static software.amazon.awssdk.benchmark.utils.BenchmarkConstant.CONCURRENT_CALLS;
19 import static software.amazon.awssdk.benchmark.utils.BenchmarkUtils.awaitCountdownLatchUninterruptibly;
20 import static software.amazon.awssdk.benchmark.utils.BenchmarkUtils.countDownUponCompletion;
21 
22 import java.net.URI;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.Level;
27 import org.openjdk.jmh.annotations.OperationsPerInvocation;
28 import org.openjdk.jmh.annotations.Setup;
29 import org.openjdk.jmh.annotations.TearDown;
30 import org.openjdk.jmh.infra.Blackhole;
31 import software.amazon.awssdk.benchmark.apicall.httpclient.SdkHttpClientBenchmark;
32 import software.amazon.awssdk.benchmark.utils.MockServer;
33 import software.amazon.awssdk.http.SdkHttpConfigurationOption;
34 import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
35 import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient;
36 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
37 import software.amazon.awssdk.utils.AttributeMap;
38 
39 /**
40  * Shared code between http and https benchmarks
41  */
42 public abstract class BaseCrtBenchmark implements SdkHttpClientBenchmark {
43 
44     private MockServer mockServer;
45     private SdkAsyncHttpClient sdkHttpClient;
46     private ProtocolRestJsonAsyncClient client;
47 
48     @Setup(Level.Trial)
setup()49     public void setup() throws Exception {
50         mockServer = new MockServer();
51         mockServer.start();
52 
53         AttributeMap trustAllCerts = AttributeMap.builder()
54                 .put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, Boolean.TRUE)
55                 .build();
56 
57         sdkHttpClient = AwsCrtAsyncHttpClient.builder()
58                 .buildWithDefaults(trustAllCerts);
59 
60         client = ProtocolRestJsonAsyncClient.builder()
61                 .endpointOverride(getEndpointOverride(mockServer))
62                 .httpClient(sdkHttpClient)
63                 .build();
64 
65         // Making sure the request actually succeeds
66         client.allTypes().join();
67     }
68 
69     @TearDown(Level.Trial)
tearDown()70     public void tearDown() throws Exception {
71         mockServer.stop();
72         client.close();
73         sdkHttpClient.close();
74     }
75 
76     @Override
77     @Benchmark
78     @OperationsPerInvocation(CONCURRENT_CALLS)
concurrentApiCall(Blackhole blackhole)79     public void concurrentApiCall(Blackhole blackhole) {
80         CountDownLatch countDownLatch = new CountDownLatch(CONCURRENT_CALLS);
81         for (int i = 0; i < CONCURRENT_CALLS; i++) {
82             countDownUponCompletion(blackhole, client.allTypes(), countDownLatch);
83         }
84 
85         awaitCountdownLatchUninterruptibly(countDownLatch, 10, TimeUnit.SECONDS);
86 
87     }
88 
89     @Override
90     @Benchmark
sequentialApiCall(Blackhole blackhole)91     public void sequentialApiCall(Blackhole blackhole) {
92         CountDownLatch countDownLatch = new CountDownLatch(1);
93         countDownUponCompletion(blackhole, client.allTypes(), countDownLatch);
94         awaitCountdownLatchUninterruptibly(countDownLatch, 1, TimeUnit.SECONDS);
95     }
96 
getEndpointOverride(MockServer mock)97     protected abstract URI getEndpointOverride(MockServer mock);
98 }
99