• 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.DEFAULT_JDK_SSL_PROVIDER;
19 import static software.amazon.awssdk.benchmark.utils.BenchmarkConstant.OPEN_SSL_PROVIDER;
20 import static software.amazon.awssdk.benchmark.utils.BenchmarkUtils.getSslProvider;
21 import static software.amazon.awssdk.benchmark.utils.BenchmarkUtils.trustAllTlsAttributeMapBuilder;
22 
23 import io.netty.handler.ssl.SslProvider;
24 import java.util.Collection;
25 import java.util.concurrent.TimeUnit;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.Fork;
28 import org.openjdk.jmh.annotations.Level;
29 import org.openjdk.jmh.annotations.Measurement;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.Param;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
35 import org.openjdk.jmh.annotations.TearDown;
36 import org.openjdk.jmh.annotations.Warmup;
37 import org.openjdk.jmh.profile.StackProfiler;
38 import org.openjdk.jmh.results.RunResult;
39 import org.openjdk.jmh.runner.Runner;
40 import org.openjdk.jmh.runner.options.Options;
41 import org.openjdk.jmh.runner.options.OptionsBuilder;
42 import software.amazon.awssdk.benchmark.utils.MockServer;
43 import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
44 import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
45 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
46 
47 /**
48  * Using netty client to test against local mock https server.
49  */
50 @State(Scope.Benchmark)
51 @Warmup(iterations = 3, time = 15, timeUnit = TimeUnit.SECONDS)
52 @Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
53 @Fork(2) // To reduce difference between each run
54 @BenchmarkMode(Mode.Throughput)
55 public class NettyHttpClientH1Benchmark extends BaseNettyBenchmark {
56 
57     private MockServer mockServer;
58     private SdkAsyncHttpClient sdkHttpClient;
59 
60     @Param({DEFAULT_JDK_SSL_PROVIDER, OPEN_SSL_PROVIDER})
61     private String sslProviderValue;
62 
63     @Setup(Level.Trial)
setup()64     public void setup() throws Exception {
65         mockServer = new MockServer();
66         mockServer.start();
67 
68         SslProvider sslProvider = getSslProvider(sslProviderValue);
69 
70         sdkHttpClient = NettyNioAsyncHttpClient.builder()
71                                                .sslProvider(sslProvider)
72                                                .buildWithDefaults(trustAllTlsAttributeMapBuilder().build());
73         client = ProtocolRestJsonAsyncClient.builder()
74                                             .endpointOverride(mockServer.getHttpsUri())
75                                             .httpClient(sdkHttpClient)
76                                             .build();
77 
78         // Making sure the request actually succeeds
79         client.allTypes().join();
80     }
81 
82     @TearDown(Level.Trial)
tearDown()83     public void tearDown() throws Exception {
84         mockServer.stop();
85         sdkHttpClient.close();
86         client.close();
87     }
88 
main(String... args)89     public static void main(String... args) throws Exception {
90         Options opt = new OptionsBuilder()
91             .include(NettyHttpClientH1Benchmark.class.getSimpleName())
92             .addProfiler(StackProfiler.class)
93             .build();
94         Collection<RunResult> run = new Runner(opt).run();
95     }
96 }
97