-
Notifications
You must be signed in to change notification settings - Fork 995
Add Ser/De + roundtrip protocol benchmarks #6776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b94b3a5
44ff776
11c6227
a21e795
78855ef
3443ef6
9f9c598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.benchmark.apicall.protocol; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import org.eclipse.jetty.server.Connector; | ||
| import org.eclipse.jetty.server.Server; | ||
| import org.eclipse.jetty.server.ServerConnector; | ||
| import org.eclipse.jetty.servlet.ServletContextHandler; | ||
| import org.eclipse.jetty.servlet.ServletHolder; | ||
| import software.amazon.awssdk.benchmark.utils.BenchmarkUtils; | ||
| import software.amazon.awssdk.utils.IoUtils; | ||
|
|
||
| /** | ||
| * Lightweight Jetty server for protocol roundtrip benchmarks. | ||
| */ | ||
| class ProtocolRoundtripServer { | ||
|
|
||
| private final Server server; | ||
| private final int port; | ||
|
|
||
| ProtocolRoundtripServer(ProtocolRoundtripServlet servlet) throws IOException { | ||
| port = BenchmarkUtils.getUnusedPort(); | ||
| server = new Server(); | ||
| ServerConnector connector = new ServerConnector(server); | ||
|
Check failure on line 40 in test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/ProtocolRoundtripServer.java
|
||
| connector.setPort(port); | ||
| server.setConnectors(new Connector[] {connector}); | ||
|
|
||
| ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); | ||
| context.addServlet(new ServletHolder(servlet), "/*"); | ||
| server.setHandler(context); | ||
| } | ||
|
|
||
| void start() throws Exception { | ||
| server.start(); | ||
| } | ||
|
|
||
| void stop() throws Exception { | ||
| server.stop(); | ||
| } | ||
|
|
||
| URI getHttpUri() { | ||
| return URI.create("http://localhost:" + port); | ||
| } | ||
|
|
||
| static byte[] loadFixture(String path) throws IOException { | ||
| try (InputStream is = ProtocolRoundtripServer.class.getClassLoader() | ||
| .getResourceAsStream("fixtures/" + path)) { | ||
| if (is == null) { | ||
| throw new IOException("Fixture not found: fixtures/" + path); | ||
| } | ||
| return IoUtils.toByteArray(is); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.benchmark.apicall.protocol; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| class ProtocolRoundtripServlet extends HttpServlet { | ||
| private final byte[] body; | ||
| private final String contentType; | ||
|
|
||
| ProtocolRoundtripServlet(byte[] body, String contentType) { | ||
| this.body = body; | ||
| this.contentType = contentType; | ||
| } | ||
|
|
||
| @Override | ||
| protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { | ||
| resp.setStatus(200); | ||
| resp.setContentLength(body.length); | ||
| resp.setContentType(contentType); | ||
| resp.getOutputStream().write(body); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.benchmark.apicall.protocol; | ||
|
|
||
| import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
| import com.amazonaws.auth.BasicAWSCredentials; | ||
| import com.amazonaws.client.builder.AwsClientBuilder; | ||
| import com.amazonaws.protocol.rpcv2cbor.SdkStructuredCborFactory; | ||
| import com.amazonaws.protocol.rpcv2cbor.StructuredRpcV2CborGenerator; | ||
| import com.amazonaws.services.cloudwatch.AmazonCloudWatch; | ||
| import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; | ||
| import com.amazonaws.services.cloudwatch.model.GetMetricDataRequest; | ||
| import com.amazonaws.services.cloudwatch.model.Metric; | ||
| import com.amazonaws.services.cloudwatch.model.MetricDataQuery; | ||
| import com.amazonaws.services.cloudwatch.model.MetricStat; | ||
| import java.util.Date; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * V1 roundtrip benchmark for SmithyRpcV2 CBOR protocol using CloudWatch GetMetricData via HTTP servlet. | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 5) | ||
| @Measurement(iterations = 5) | ||
| @Fork(2) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| public class V1CborRoundtripBenchmark { | ||
|
|
||
| private ProtocolRoundtripServer server; | ||
| private AmazonCloudWatch client; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() throws Exception { | ||
| byte[] response = createCborResponseFixture(); | ||
|
|
||
| ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response, "application/cbor"); | ||
|
|
||
| server = new ProtocolRoundtripServer(servlet); | ||
| server.start(); | ||
|
|
||
| client = AmazonCloudWatchClientBuilder.standard() | ||
| .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration( | ||
| server.getHttpUri().toString(), "us-east-1")) | ||
| .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("test", "test"))) | ||
|
Check failure on line 71 in test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1CborRoundtripBenchmark.java
|
||
| .build(); | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() throws Exception { | ||
| client.shutdown(); | ||
| server.stop(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void getMetricData(Blackhole bh) { | ||
| Date end = Date.from(java.time.Instant.parse("2026-03-09T00:00:00Z")); | ||
| Date start = Date.from(java.time.Instant.parse("2026-03-09T00:00:00Z").minusSeconds(3600)); | ||
| GetMetricDataRequest request = new GetMetricDataRequest() | ||
| .withStartTime(start) | ||
| .withEndTime(end) | ||
| .withMaxDatapoints(1000) | ||
| .withMetricDataQueries( | ||
| new MetricDataQuery() | ||
| .withId("cpu") | ||
| .withMetricStat(new MetricStat() | ||
| .withMetric(new Metric() | ||
| .withNamespace("AWS/EC2") | ||
| .withMetricName("CPUUtilization")) | ||
| .withPeriod(300) | ||
| .withStat("Average")) | ||
| .withReturnData(true)); | ||
|
|
||
| bh.consume(client.getMetricData(request)); | ||
| } | ||
|
|
||
| private static byte[] createCborResponseFixture() { | ||
| StructuredRpcV2CborGenerator gen = | ||
| SdkStructuredCborFactory.SDK_CBOR_FACTORY.createWriter("application/cbor"); | ||
| gen.writeStartObject(); | ||
| gen.writeFieldName("MetricDataResults"); | ||
| gen.writeStartArray(); | ||
| gen.writeStartObject(); | ||
| gen.writeFieldName("Id"); | ||
| gen.writeValue("cpu"); | ||
| gen.writeFieldName("Label"); | ||
| gen.writeValue("CPUUtilization"); | ||
| gen.writeFieldName("StatusCode"); | ||
| gen.writeValue("Complete"); | ||
| gen.writeFieldName("Timestamps"); | ||
| gen.writeStartArray(); | ||
| long base = 1772611200L; | ||
| for (int i = 0; i < 12; i++) { | ||
| gen.writeValue((double) ((base + i * 300) * 1000)); | ||
| } | ||
| gen.writeEndArray(); | ||
| gen.writeFieldName("Values"); | ||
| gen.writeStartArray(); | ||
| for (int i = 0; i < 12; i++) { | ||
| gen.writeValue(45.2 + i * 1.1); | ||
| } | ||
| gen.writeEndArray(); | ||
| gen.writeFieldName("Messages"); | ||
| gen.writeStartArray(); | ||
| gen.writeEndArray(); | ||
| gen.writeEndObject(); | ||
| gen.writeEndArray(); | ||
| gen.writeFieldName("Messages"); | ||
| gen.writeStartArray(); | ||
| gen.writeEndArray(); | ||
| gen.writeEndObject(); | ||
| return gen.getBytes(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.