Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<!-- Allow non-java.base usage in tests -->
<suppress checks="software.amazon.awssdk.buildtools.checkstyle.NonJavaBaseModuleCheck" files=".*testutils.*"/>

<!-- Allow javax.xml.stream in benchmark protocol files (needed for V1 StAX unmarshalling) -->
<suppress checks="software.amazon.awssdk.buildtools.checkstyle.NonJavaBaseModuleCheck"
files=".*benchmark[\\/]protocol[\\/]V1.*\.java$"/>

<!-- Allow private field declaration before public, to have correct initialization order -->
<suppress checks="DeclarationOrder"
files=".*SdkAdvancedClientOption\.java$"/>
Expand Down
42 changes: 41 additions & 1 deletion test/sdk-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
-->
<uberjar.name>benchmarks</uberjar.name>

<sdk-v1.version>1.11.404</sdk-v1.version>
<sdk-v1.version>1.12.797</sdk-v1.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>

Expand Down Expand Up @@ -87,18 +87,58 @@
<artifactId>aws-java-sdk-ec2</artifactId>
<version>${sdk-v1.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudfront</artifactId>
<version>${sdk-v1.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sts</artifactId>
<version>${sdk-v1.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>${sdk-v1.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatch</artifactId>
<version>${sdk-v1.version}</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ec2</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudfront</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-query-protocol</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>smithy-rpcv2-protocol</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand Down
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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use try-with-resources or close this "ServerConnector" in a "finally" clause.

See more on https://sonarcloud.io/project/issues?id=aws_aws-sdk-java-v2&issues=AZz5CZ2StGKFCtmNRfcO&open=AZz5CZ2StGKFCtmNRfcO&pullRequest=6776
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also probably be setting content-type? I'm not quite sure if it matters or not

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")))
.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();
}
}
Loading
Loading