• 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;
17 
18 import org.openjdk.jmh.annotations.Benchmark;
19 import org.openjdk.jmh.annotations.BenchmarkMode;
20 import org.openjdk.jmh.annotations.Level;
21 import org.openjdk.jmh.annotations.Mode;
22 import org.openjdk.jmh.annotations.Scope;
23 import org.openjdk.jmh.annotations.Setup;
24 import org.openjdk.jmh.annotations.State;
25 import org.openjdk.jmh.annotations.TearDown;
26 import org.openjdk.jmh.runner.Runner;
27 import org.openjdk.jmh.runner.options.Options;
28 import org.openjdk.jmh.runner.options.OptionsBuilder;
29 import software.amazon.awssdk.benchmark.utils.MockServer;
30 import software.amazon.awssdk.core.async.AsyncRequestBody;
31 import software.amazon.awssdk.core.async.AsyncResponseTransformer;
32 import software.amazon.awssdk.core.client.builder.SdkClientBuilder;
33 import software.amazon.awssdk.core.sync.RequestBody;
34 import software.amazon.awssdk.http.apache.ApacheHttpClient;
35 import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
36 import software.amazon.awssdk.metrics.MetricCollection;
37 import software.amazon.awssdk.metrics.MetricPublisher;
38 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
39 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClientBuilder;
40 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
41 import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder;
42 import software.amazon.awssdk.services.protocolrestjson.model.StreamingInputOperationRequest;
43 import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationRequest;
44 
45 /**
46  * Benchmarking comparing metrics-enabled versus metrics-disabled performance.
47  */
48 @State(Scope.Benchmark)
49 @BenchmarkMode(Mode.Throughput)
50 public class MetricsEnabledBenchmark {
51     private MockServer mockServer;
52     private ProtocolRestJsonClient enabledMetricsSyncClient;
53     private ProtocolRestJsonAsyncClient enabledMetricsAsyncClient;
54 
55     @Setup(Level.Trial)
setup()56     public void setup() throws Exception {
57         mockServer = new MockServer();
58         mockServer.start();
59         enabledMetricsSyncClient = enableMetrics(syncClientBuilder()).build();
60         enabledMetricsAsyncClient = enableMetrics(asyncClientBuilder()).build();
61     }
62 
enableMetrics(T syncClientBuilder)63     private <T extends SdkClientBuilder<T, ?>> T enableMetrics(T syncClientBuilder) {
64         return syncClientBuilder.overrideConfiguration(c -> c.addMetricPublisher(new EnabledPublisher()));
65     }
66 
syncClientBuilder()67     private ProtocolRestJsonClientBuilder syncClientBuilder() {
68         return ProtocolRestJsonClient.builder()
69                                      .endpointOverride(mockServer.getHttpUri())
70                                      .httpClientBuilder(ApacheHttpClient.builder());
71     }
72 
asyncClientBuilder()73     private ProtocolRestJsonAsyncClientBuilder asyncClientBuilder() {
74         return ProtocolRestJsonAsyncClient.builder()
75                                           .endpointOverride(mockServer.getHttpUri())
76                                           .httpClientBuilder(NettyNioAsyncHttpClient.builder());
77     }
78 
79     @TearDown(Level.Trial)
tearDown()80     public void tearDown() throws Exception {
81         mockServer.stop();
82         enabledMetricsSyncClient.close();
83         enabledMetricsAsyncClient.close();
84     }
85 
86     @Benchmark
metricsEnabledSync()87     public void metricsEnabledSync() {
88         enabledMetricsSyncClient.allTypes();
89     }
90 
91     @Benchmark
metricsEnabledAsync()92     public void metricsEnabledAsync() {
93         enabledMetricsAsyncClient.allTypes().join();
94     }
95 
96     @Benchmark
metricsEnabledSyncStreamingInput()97     public void metricsEnabledSyncStreamingInput() {
98         enabledMetricsSyncClient.streamingInputOperation(streamingInputRequest(), RequestBody.fromString(""));
99     }
100 
101     @Benchmark
metricsEnabledAsyncStreamingInput()102     public void metricsEnabledAsyncStreamingInput() {
103         enabledMetricsAsyncClient.streamingInputOperation(streamingInputRequest(), AsyncRequestBody.fromString("")).join();
104     }
105 
106     @Benchmark
metricsEnabledSyncStreamingOutput()107     public void metricsEnabledSyncStreamingOutput() {
108         enabledMetricsSyncClient.streamingOutputOperationAsBytes(streamingOutputRequest());
109     }
110 
111     @Benchmark
metricsEnabledAsyncStreamingOutput()112     public void metricsEnabledAsyncStreamingOutput() {
113         enabledMetricsAsyncClient.streamingOutputOperation(streamingOutputRequest(), AsyncResponseTransformer.toBytes()).join();
114     }
115 
streamingInputRequest()116     private StreamingInputOperationRequest streamingInputRequest() {
117         return StreamingInputOperationRequest.builder().build();
118     }
119 
streamingOutputRequest()120     private StreamingOutputOperationRequest streamingOutputRequest() {
121         return StreamingOutputOperationRequest.builder().build();
122     }
123 
main(String... args)124     public static void main(String... args) throws Exception {
125         Options opt = new OptionsBuilder()
126             .include(MetricsEnabledBenchmark.class.getSimpleName())
127             .build();
128         new Runner(opt).run();
129     }
130 
131     private static final class EnabledPublisher implements MetricPublisher {
132         @Override
publish(MetricCollection metricCollection)133         public void publish(MetricCollection metricCollection) {
134         }
135 
136         @Override
close()137         public void close() {
138         }
139     }
140 }
141