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