Skip to content

Commit fa0445c

Browse files
committed
Simplify servlet
1 parent 29cdadb commit fa0445c

13 files changed

+17
-130
lines changed

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/ProtocolRoundtripServlet.java

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -16,116 +16,21 @@
1616
package software.amazon.awssdk.benchmark.apicall.protocol;
1717

1818
import java.io.IOException;
19-
import java.io.OutputStream;
20-
import java.util.Map;
21-
import java.util.concurrent.ConcurrentHashMap;
2219
import javax.servlet.http.HttpServlet;
2320
import javax.servlet.http.HttpServletRequest;
2421
import javax.servlet.http.HttpServletResponse;
2522

26-
/**
27-
* Minimal servlet for protocol roundtrip benchmarks. Returns pre-loaded canned responses
28-
* with zero request inspection overhead. Routes are matched by URI prefix or X-Amz-Target header.
29-
*/
3023
class ProtocolRoundtripServlet extends HttpServlet {
24+
private final byte[] body;
3125

32-
private final Map<String, CannedResponse> targetRoutes = new ConcurrentHashMap<>();
33-
private final Map<String, CannedResponse> uriRoutes = new ConcurrentHashMap<>();
34-
private CannedResponse defaultResponse;
35-
36-
/**
37-
* Register a route matched by X-Amz-Target header value.
38-
*/
39-
ProtocolRoundtripServlet routeByTarget(String target, String contentType, byte[] body) {
40-
targetRoutes.put(target, new CannedResponse(contentType, body));
41-
return this;
42-
}
43-
44-
/**
45-
* Register a route matched by URI prefix.
46-
*/
47-
ProtocolRoundtripServlet routeByUri(String uriPrefix, String contentType, byte[] body) {
48-
uriRoutes.put(uriPrefix, new CannedResponse(contentType, body));
49-
return this;
50-
}
51-
52-
/**
53-
* Register a route matched by URI prefix with additional response headers.
54-
*/
55-
ProtocolRoundtripServlet routeByUri(String uriPrefix, String contentType, byte[] body,
56-
Map<String, String> headers) {
57-
uriRoutes.put(uriPrefix, new CannedResponse(contentType, body, headers));
58-
return this;
59-
}
60-
61-
/**
62-
* Default response when no route matches.
63-
*/
64-
ProtocolRoundtripServlet defaultRoute(String contentType, byte[] body) {
65-
this.defaultResponse = new CannedResponse(contentType, body);
66-
return this;
26+
ProtocolRoundtripServlet(byte[] body) {
27+
this.body = body;
6728
}
6829

6930
@Override
7031
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
71-
// Consume request body to simulate real HTTP exchange
72-
byte[] buf = new byte[8192];
73-
while (req.getInputStream().read(buf) != -1) {
74-
// drain
75-
}
76-
77-
CannedResponse canned = resolve(req);
78-
if (canned == null) {
79-
resp.sendError(404);
80-
return;
81-
}
82-
8332
resp.setStatus(200);
84-
resp.setContentType(canned.contentType);
85-
resp.setContentLength(canned.body.length);
86-
resp.setHeader("x-amzn-RequestId", "benchmark-request-id");
87-
if (canned.headers != null) {
88-
canned.headers.forEach(resp::setHeader);
89-
}
90-
try (OutputStream os = resp.getOutputStream()) {
91-
os.write(canned.body);
92-
}
93-
}
94-
95-
private CannedResponse resolve(HttpServletRequest req) {
96-
// Try X-Amz-Target first (JSON/CBOR protocols)
97-
String target = req.getHeader("X-Amz-Target");
98-
if (target != null) {
99-
CannedResponse r = targetRoutes.get(target);
100-
if (r != null) {
101-
return r;
102-
}
103-
}
104-
105-
// Try URI prefix match (REST protocols)
106-
String uri = req.getRequestURI();
107-
for (Map.Entry<String, CannedResponse> entry : uriRoutes.entrySet()) {
108-
if (uri.contains(entry.getKey())) {
109-
return entry.getValue();
110-
}
111-
}
112-
113-
return defaultResponse;
114-
}
115-
116-
private static class CannedResponse {
117-
final String contentType;
118-
final byte[] body;
119-
final Map<String, String> headers;
120-
121-
CannedResponse(String contentType, byte[] body) {
122-
this(contentType, body, null);
123-
}
124-
125-
CannedResponse(String contentType, byte[] body, Map<String, String> headers) {
126-
this.contentType = contentType;
127-
this.body = body;
128-
this.headers = headers;
129-
}
33+
resp.setContentLength(body.length);
34+
resp.getOutputStream().write(body);
13035
}
13136
}

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1CborRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public class V1CborRoundtripBenchmark {
6161
public void setup() throws Exception {
6262
byte[] response = createCborResponseFixture();
6363

64-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
65-
.defaultRoute("application/cbor", response);
64+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6665

6766
server = new ProtocolRoundtripServer(servlet);
6867
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1Ec2RoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public class V1Ec2RoundtripBenchmark {
5656
public void setup() throws Exception {
5757
byte[] response = ProtocolRoundtripServer.loadFixture("ec2-protocol/describe-instances-response.xml");
5858

59-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
60-
.defaultRoute("text/xml", response);
59+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6160

6261
server = new ProtocolRoundtripServer(servlet);
6362
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1JsonRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public class V1JsonRoundtripBenchmark {
5959
public void setup() throws Exception {
6060
byte[] response = ProtocolRoundtripServer.loadFixture("json-protocol/putitem-response.json");
6161

62-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
63-
.routeByTarget("DynamoDB_20120810.PutItem", "application/x-amz-json-1.0", response);
62+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6463

6564
server = new ProtocolRoundtripServer(servlet);
6665
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1QueryRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public class V1QueryRoundtripBenchmark {
5555
public void setup() throws Exception {
5656
byte[] response = ProtocolRoundtripServer.loadFixture("query-protocol/assumerole-response.xml");
5757

58-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
59-
.defaultRoute("text/xml", response);
58+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6059

6160
server = new ProtocolRoundtripServer(servlet);
6261
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1RestJsonRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public class V1RestJsonRoundtripBenchmark {
6262
public void setup() throws Exception {
6363
byte[] response = ProtocolRoundtripServer.loadFixture("rest-json-protocol/createfunction-response.json");
6464

65-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
66-
.routeByUri("/2015-03-31/functions", "application/json", response);
65+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6766

6867
server = new ProtocolRoundtripServer(servlet);
6968
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V1RestXmlRoundtripBenchmark.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import com.amazonaws.services.cloudfront.model.Origins;
3232
import com.amazonaws.services.cloudfront.model.S3OriginConfig;
3333
import com.amazonaws.services.cloudfront.model.ViewerProtocolPolicy;
34-
import java.util.Collections;
35-
import java.util.Map;
3634
import java.util.concurrent.TimeUnit;
3735
import org.openjdk.jmh.annotations.Benchmark;
3836
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -67,9 +65,7 @@ public class V1RestXmlRoundtripBenchmark {
6765
public void setup() throws Exception {
6866
byte[] response = ProtocolRoundtripServer.loadFixture("rest-xml-protocol/create-distribution-response.xml");
6967

70-
Map<String, String> headers = Collections.singletonMap("ETag", "E2QWRUHEXAMPLE");
71-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
72-
.routeByUri("/2020-05-31/distribution", "application/xml", response, headers);
68+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
7369

7470
server = new ProtocolRoundtripServer(servlet);
7571
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V2CborRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public class V2CborRoundtripBenchmark {
6161
public void setup() throws Exception {
6262
byte[] response = createCborResponseFixture();
6363

64-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
65-
.defaultRoute("application/cbor", response);
64+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6665

6766
server = new ProtocolRoundtripServer(servlet);
6867
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V2Ec2RoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public class V2Ec2RoundtripBenchmark {
5656
public void setup() throws Exception {
5757
byte[] response = ProtocolRoundtripServer.loadFixture("ec2-protocol/describe-instances-response.xml");
5858

59-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
60-
.defaultRoute("text/xml", response);
59+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6160

6261
server = new ProtocolRoundtripServer(servlet);
6362
server.start();

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/apicall/protocol/V2JsonRoundtripBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public class V2JsonRoundtripBenchmark {
5959
public void setup() throws Exception {
6060
byte[] response = ProtocolRoundtripServer.loadFixture("json-protocol/putitem-response.json");
6161

62-
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet()
63-
.routeByTarget("DynamoDB_20120810.PutItem", "application/x-amz-json-1.0", response);
62+
ProtocolRoundtripServlet servlet = new ProtocolRoundtripServlet(response);
6463

6564
server = new ProtocolRoundtripServer(servlet);
6665
server.start();

0 commit comments

Comments
 (0)