|
| 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.protocol; |
| 17 | + |
| 18 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 19 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 20 | +import com.amazonaws.client.builder.AwsClientBuilder; |
| 21 | +import com.amazonaws.protocol.rpcv2cbor.SdkStructuredCborFactory; |
| 22 | +import com.amazonaws.protocol.rpcv2cbor.StructuredRpcV2CborGenerator; |
| 23 | +import com.amazonaws.services.cloudwatch.AmazonCloudWatch; |
| 24 | +import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; |
| 25 | +import com.amazonaws.services.cloudwatch.model.GetMetricDataRequest; |
| 26 | +import com.amazonaws.services.cloudwatch.model.Metric; |
| 27 | +import com.amazonaws.services.cloudwatch.model.MetricDataQuery; |
| 28 | +import com.amazonaws.services.cloudwatch.model.MetricStat; |
| 29 | +import java.util.Date; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Benchmark; |
| 32 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 33 | +import org.openjdk.jmh.annotations.Fork; |
| 34 | +import org.openjdk.jmh.annotations.Level; |
| 35 | +import org.openjdk.jmh.annotations.Measurement; |
| 36 | +import org.openjdk.jmh.annotations.Mode; |
| 37 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 38 | +import org.openjdk.jmh.annotations.Scope; |
| 39 | +import org.openjdk.jmh.annotations.Setup; |
| 40 | +import org.openjdk.jmh.annotations.State; |
| 41 | +import org.openjdk.jmh.annotations.TearDown; |
| 42 | +import org.openjdk.jmh.annotations.Warmup; |
| 43 | +import org.openjdk.jmh.infra.Blackhole; |
| 44 | + |
| 45 | +/** |
| 46 | + * V1 roundtrip benchmark for SmithyRpcV2 CBOR protocol using CloudWatch GetMetricData via HTTP servlet. |
| 47 | + */ |
| 48 | +@State(Scope.Benchmark) |
| 49 | +@Warmup(iterations = 5) |
| 50 | +@Measurement(iterations = 5) |
| 51 | +@Fork(2) |
| 52 | +@BenchmarkMode(Mode.Throughput) |
| 53 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 54 | +public class V1CborRoundtripBenchmark { |
| 55 | + |
| 56 | + private ProtocolRoundtripServer server; |
| 57 | + private AmazonCloudWatch client; |
| 58 | + private GetMetricDataRequest request; |
| 59 | + |
| 60 | + @Setup(Level.Trial) |
| 61 | + public void setup() throws Exception { |
| 62 | + byte[] response = createCborResponseFixture(); |
| 63 | + |
| 64 | + ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet() |
| 65 | + .defaultRoute("application/cbor", response); |
| 66 | + |
| 67 | + server = new ProtocolRoundtripServer(servlet); |
| 68 | + server.start(); |
| 69 | + |
| 70 | + client = AmazonCloudWatchClientBuilder.standard() |
| 71 | + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration( |
| 72 | + server.getHttpUri().toString(), "us-east-1")) |
| 73 | + .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("test", "test"))) |
| 74 | + .build(); |
| 75 | + |
| 76 | + Date end = Date.from(java.time.Instant.parse("2026-03-09T00:00:00Z")); |
| 77 | + Date start = Date.from(java.time.Instant.parse("2026-03-09T00:00:00Z").minusSeconds(3600)); |
| 78 | + request = new GetMetricDataRequest() |
| 79 | + .withStartTime(start) |
| 80 | + .withEndTime(end) |
| 81 | + .withMaxDatapoints(1000) |
| 82 | + .withMetricDataQueries( |
| 83 | + new MetricDataQuery() |
| 84 | + .withId("cpu") |
| 85 | + .withMetricStat(new MetricStat() |
| 86 | + .withMetric(new Metric() |
| 87 | + .withNamespace("AWS/EC2") |
| 88 | + .withMetricName("CPUUtilization")) |
| 89 | + .withPeriod(300) |
| 90 | + .withStat("Average")) |
| 91 | + .withReturnData(true)); |
| 92 | + } |
| 93 | + |
| 94 | + @TearDown(Level.Trial) |
| 95 | + public void tearDown() throws Exception { |
| 96 | + client.shutdown(); |
| 97 | + server.stop(); |
| 98 | + } |
| 99 | + |
| 100 | + @Benchmark |
| 101 | + public void getMetricData(Blackhole bh) { |
| 102 | + bh.consume(client.getMetricData(request)); |
| 103 | + } |
| 104 | + |
| 105 | + private static byte[] createCborResponseFixture() { |
| 106 | + StructuredRpcV2CborGenerator gen = |
| 107 | + SdkStructuredCborFactory.SDK_CBOR_FACTORY.createWriter("application/cbor"); |
| 108 | + gen.writeStartObject(); |
| 109 | + gen.writeFieldName("MetricDataResults"); |
| 110 | + gen.writeStartArray(); |
| 111 | + gen.writeStartObject(); |
| 112 | + gen.writeFieldName("Id"); |
| 113 | + gen.writeValue("cpu"); |
| 114 | + gen.writeFieldName("Label"); |
| 115 | + gen.writeValue("CPUUtilization"); |
| 116 | + gen.writeFieldName("StatusCode"); |
| 117 | + gen.writeValue("Complete"); |
| 118 | + gen.writeFieldName("Timestamps"); |
| 119 | + gen.writeStartArray(); |
| 120 | + long base = 1772611200L; |
| 121 | + for (int i = 0; i < 12; i++) { |
| 122 | + gen.writeValue((double) ((base + i * 300) * 1000)); |
| 123 | + } |
| 124 | + gen.writeEndArray(); |
| 125 | + gen.writeFieldName("Values"); |
| 126 | + gen.writeStartArray(); |
| 127 | + for (int i = 0; i < 12; i++) { |
| 128 | + gen.writeValue(45.2 + i * 1.1); |
| 129 | + } |
| 130 | + gen.writeEndArray(); |
| 131 | + gen.writeFieldName("Messages"); |
| 132 | + gen.writeStartArray(); |
| 133 | + gen.writeEndArray(); |
| 134 | + gen.writeEndObject(); |
| 135 | + gen.writeEndArray(); |
| 136 | + gen.writeFieldName("Messages"); |
| 137 | + gen.writeStartArray(); |
| 138 | + gen.writeEndArray(); |
| 139 | + gen.writeEndObject(); |
| 140 | + return gen.getBytes(); |
| 141 | + } |
| 142 | +} |
0 commit comments