• 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.sync;
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 import static software.amazon.awssdk.benchmark.utils.BenchmarkUtils.trustAllTlsAttributeMapBuilder;
22 
23 import java.util.Collection;
24 import java.util.concurrent.CompletableFuture;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.TimeUnit;
29 import org.openjdk.jmh.annotations.Benchmark;
30 import org.openjdk.jmh.annotations.BenchmarkMode;
31 import org.openjdk.jmh.annotations.Fork;
32 import org.openjdk.jmh.annotations.Level;
33 import org.openjdk.jmh.annotations.Measurement;
34 import org.openjdk.jmh.annotations.Mode;
35 import org.openjdk.jmh.annotations.OperationsPerInvocation;
36 import org.openjdk.jmh.annotations.Scope;
37 import org.openjdk.jmh.annotations.Setup;
38 import org.openjdk.jmh.annotations.State;
39 import org.openjdk.jmh.annotations.TearDown;
40 import org.openjdk.jmh.annotations.Warmup;
41 import org.openjdk.jmh.infra.Blackhole;
42 import org.openjdk.jmh.profile.StackProfiler;
43 import org.openjdk.jmh.results.RunResult;
44 import org.openjdk.jmh.runner.Runner;
45 import org.openjdk.jmh.runner.options.Options;
46 import org.openjdk.jmh.runner.options.OptionsBuilder;
47 import software.amazon.awssdk.benchmark.apicall.httpclient.SdkHttpClientBenchmark;
48 import software.amazon.awssdk.benchmark.utils.MockServer;
49 import software.amazon.awssdk.http.SdkHttpClient;
50 import software.amazon.awssdk.http.apache.ApacheHttpClient;
51 import software.amazon.awssdk.regions.Region;
52 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
53 
54 /**
55  * Benchmarking for running with different http clients.
56  */
57 @State(Scope.Benchmark)
58 @Warmup(iterations = 3, time = 15, timeUnit = TimeUnit.SECONDS)
59 @Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
60 @Fork(2) // To reduce difference between each run
61 @BenchmarkMode(Mode.Throughput)
62 public class ApacheHttpClientBenchmark implements SdkHttpClientBenchmark {
63 
64     private MockServer mockServer;
65     private SdkHttpClient sdkHttpClient;
66     private ProtocolRestJsonClient client;
67     private ExecutorService executorService;
68 
69     @Setup(Level.Trial)
setup()70     public void setup() throws Exception {
71         mockServer = new MockServer();
72         mockServer.start();
73         sdkHttpClient = ApacheHttpClient.builder()
74                                         .buildWithDefaults(trustAllTlsAttributeMapBuilder().build());
75         client = ProtocolRestJsonClient.builder()
76                                        .endpointOverride(mockServer.getHttpsUri())
77                                        .httpClient(sdkHttpClient)
78                                        .region(Region.US_EAST_1)
79                                        .build();
80         executorService = Executors.newFixedThreadPool(CONCURRENT_CALLS);
81 
82         client.allTypes();
83     }
84 
85     @TearDown(Level.Trial)
tearDown()86     public void tearDown() throws Exception {
87         executorService.shutdown();
88         mockServer.stop();
89         sdkHttpClient.close();
90         client.close();
91     }
92 
93     @Benchmark
94     @Override
sequentialApiCall(Blackhole blackhole)95     public void sequentialApiCall(Blackhole blackhole) {
96         blackhole.consume(client.allTypes());
97     }
98 
99     @Benchmark
100     @Override
101     @OperationsPerInvocation(CONCURRENT_CALLS)
concurrentApiCall(Blackhole blackhole)102     public void concurrentApiCall(Blackhole blackhole) {
103         CountDownLatch countDownLatch = new CountDownLatch(CONCURRENT_CALLS);
104         for (int i = 0; i < CONCURRENT_CALLS; i++) {
105             countDownUponCompletion(blackhole,
106                                     CompletableFuture.runAsync(() -> client.allTypes(), executorService), countDownLatch);
107         }
108 
109         awaitCountdownLatchUninterruptibly(countDownLatch, 10, TimeUnit.SECONDS);
110     }
111 
main(String... args)112     public static void main(String... args) throws Exception {
113 
114         Options opt = new OptionsBuilder()
115             .include(ApacheHttpClientBenchmark.class.getSimpleName() + ".concurrentApiCall")
116             .addProfiler(StackProfiler.class)
117             .build();
118         Collection<RunResult> run = new Runner(opt).run();
119     }
120 }
121