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.trustAllTlsAttributeMapBuilder; 20 21 import java.util.Collection; 22 import java.util.concurrent.ExecutorService; 23 import java.util.concurrent.Executors; 24 import java.util.concurrent.TimeUnit; 25 import org.openjdk.jmh.annotations.Benchmark; 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.Scope; 32 import org.openjdk.jmh.annotations.Setup; 33 import org.openjdk.jmh.annotations.State; 34 import org.openjdk.jmh.annotations.TearDown; 35 import org.openjdk.jmh.annotations.Warmup; 36 import org.openjdk.jmh.infra.Blackhole; 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.apicall.httpclient.SdkHttpClientBenchmark; 43 import software.amazon.awssdk.benchmark.utils.MockServer; 44 import software.amazon.awssdk.http.SdkHttpClient; 45 import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; 46 import software.amazon.awssdk.regions.Region; 47 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; 48 49 /** 50 * Benchmarking for running with different http clients. 51 */ 52 @State(Scope.Benchmark) 53 @Warmup(iterations = 3, time = 15, timeUnit = TimeUnit.SECONDS) 54 @Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) 55 @Fork(2) // To reduce difference between each run 56 @BenchmarkMode(Mode.Throughput) 57 public class UrlConnectionHttpClientBenchmark implements SdkHttpClientBenchmark { 58 59 private MockServer mockServer; 60 private SdkHttpClient sdkHttpClient; 61 private ProtocolRestJsonClient client; 62 private ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_CALLS); 63 64 @Setup(Level.Trial) setup()65 public void setup() throws Exception { 66 mockServer = new MockServer(); 67 mockServer.start(); 68 sdkHttpClient = UrlConnectionHttpClient.builder() 69 .buildWithDefaults(trustAllTlsAttributeMapBuilder().build()); 70 client = ProtocolRestJsonClient.builder() 71 .endpointOverride(mockServer.getHttpsUri()) 72 .region(Region.US_EAST_1) 73 .httpClient(sdkHttpClient) 74 .build(); 75 client.allTypes(); 76 } 77 78 @TearDown(Level.Trial) tearDown()79 public void tearDown() throws Exception { 80 executorService.shutdown(); 81 mockServer.stop(); 82 sdkHttpClient.close(); 83 client.close(); 84 } 85 86 @Benchmark 87 @Override sequentialApiCall(Blackhole blackhole)88 public void sequentialApiCall(Blackhole blackhole) { 89 blackhole.consume(client.allTypes()); 90 } 91 main(String... args)92 public static void main(String... args) throws Exception { 93 94 Options opt = new OptionsBuilder() 95 .include(UrlConnectionHttpClientBenchmark.class.getSimpleName()) 96 .addProfiler(StackProfiler.class) 97 .build(); 98 Collection<RunResult> run = new Runner(opt).run(); 99 } 100 } 101