Skip to content

Commit 2c59db1

Browse files
committed
Add Java wrapper for AWS Lambda benchmarks
1 parent 601903f commit 2c59db1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import com.amazonaws.services.lambda.runtime.Context;
2+
import com.amazonaws.services.lambda.runtime.RequestHandler;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
6+
import faas.App;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.time.Instant;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
public class Handler implements RequestHandler<Map<String, Object>, String> {
15+
private static final ObjectMapper mapper = new ObjectMapper();
16+
17+
@Override
18+
public String handleRequest(Map<String, Object> event, Context context) {
19+
20+
Map<String, Object> inputData = event;
21+
22+
// Extract input if trigger is API Gateway (body is a string)
23+
if (event.containsKey("body") && event.get("body") instanceof String)
24+
try {
25+
inputData = mapper.readValue((String) event.get("body"),new TypeReference<Map<String, Object>>() {});
26+
} catch (IOException e) {
27+
throw new RuntimeException("Failed to parse JSON body", e);
28+
}
29+
30+
App function = new App();
31+
32+
Instant begin = Instant.now();
33+
long start_nano = System.nanoTime();
34+
35+
Map<String, Object> functionOutput = function.handler(inputData);
36+
37+
long end_nano = System.nanoTime();
38+
Instant end = Instant.now();
39+
40+
41+
long computeTime = end_nano - start_nano;
42+
// Detect cold start
43+
boolean isCold = false;
44+
String fileName = "/tmp/cold_run";
45+
46+
File file = new File(fileName);
47+
if (!file.exists()) {
48+
isCold = true;
49+
try {
50+
file.createNewFile();
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
// Convert to Unix timestamp in seconds.microseconds
57+
String formattedBegin = String.format("%d.%06d", begin.getEpochSecond(), begin.getNano() / 1000); // Convert nanoseconds to microseconds
58+
String formattedEnd = String.format("%d.%06d", end.getEpochSecond(), end.getNano() / 1000);
59+
60+
61+
Map<String, Object> result = new HashMap<>();
62+
result.put("begin", formattedBegin);
63+
result.put("end", formattedEnd);
64+
result.put("request_id", context.getAwsRequestId());
65+
result.put("compute_time", computeTime);
66+
result.put("is_cold", isCold);
67+
result.put("result", functionOutput);
68+
try {
69+
return mapper.writeValueAsString(result);
70+
} catch (IOException e) {
71+
throw new RuntimeException("Failed to serialize result of benchmark to JSON in Wrapper", e);
72+
}
73+
74+
}
75+
}

0 commit comments

Comments
 (0)