putItem(itemRequest) 在 JUnit 测试期间有效,但在 AWS Lambda 控制台测试期间无效
putItem(itemRequest) works during JUnit test but not during test on AWS Lambda console
我有以下代码用于 Lambda 中的 putItem JAVA 函数。该函数在 client.putItem(itemRequest);
行之前正常工作。该功能正在正确读取输入。在进行 JUnit4 测试(总时间 < 2 秒)时,整个代码工作正常,我可以看到数据库中的条目。但是当在 putItem 行使用 Lambda 控制台进行测试时,这会失败(5 秒后超时)。
public class HelloDB implements RequestHandler<UserClass, String> {
AmazonDynamoDBClient client = null;
public HelloDB() throws IOException {
AWSCredentials credentials = new PropertiesCredentials(HelloDB.class
.getResourceAsStream("AwsCredentials.properties"));
client = new AmazonDynamoDBClient(credentials);
System.out.println("Creds created");
}
@Override
public String handleRequest(UserClass input, Context context) {
System.out.println("U: " + input.getUserName() + " p: " + input.getPasswordHash());
System.out.println("UI: " + input.userId + " O: " + input.getOpenIdToken());
String tableName = "User";
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("userName", new AttributeValue().withS(input.getUserName()));
item.put("passwordHash", new AttributeValue().withS(input.getPasswordHash()));
item.put("userId", new AttributeValue().withN(input.getUserId().toString()));
item.put("openIdToken", new AttributeValue().withS(input.getOpenIdToken()));
PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName).withItem(item);
System.out.println("item request");
client.putItem(itemRequest);
System.out.println("client put item request");
item.clear();
return "Success";
}
这是错误消息:
"errorMessage": .... Task timed out after 5.00 seconds"
当时间增加到 15 秒而不是 5 秒时,我收到此错误:
"errorMessage": "Metaspace",
"errorType": "java.lang.OutOfMemoryError"
如果一个函数没有收到任何请求,那么亚马逊会自动将其从 Lambda 服务器中删除,这样它就不会使用任何资源。如果发生这种情况,那么下次请求进来时,该函数必须重新部署到 Lambda 服务器,然后才能处理该请求。这叫做 "cold start"
与用 python 或 nodejs 编写的函数相比,从冷启动到 JAVA 到 运行 编写的 lambda 函数需要更长的时间和更多的资源。因此,建议分配至少 10 秒的时间限制和 512MB 的内存来启动。随后的 运行 大约需要 20-30 毫秒。
这是一项关于用所有 3 种语言编写的函数的冷启动时间和随之而来的 运行 时间的研究。
JUnit4 测试比 Lambda 快 运行s 可能是因为冷启动。
我有以下代码用于 Lambda 中的 putItem JAVA 函数。该函数在 client.putItem(itemRequest);
行之前正常工作。该功能正在正确读取输入。在进行 JUnit4 测试(总时间 < 2 秒)时,整个代码工作正常,我可以看到数据库中的条目。但是当在 putItem 行使用 Lambda 控制台进行测试时,这会失败(5 秒后超时)。
public class HelloDB implements RequestHandler<UserClass, String> {
AmazonDynamoDBClient client = null;
public HelloDB() throws IOException {
AWSCredentials credentials = new PropertiesCredentials(HelloDB.class
.getResourceAsStream("AwsCredentials.properties"));
client = new AmazonDynamoDBClient(credentials);
System.out.println("Creds created");
}
@Override
public String handleRequest(UserClass input, Context context) {
System.out.println("U: " + input.getUserName() + " p: " + input.getPasswordHash());
System.out.println("UI: " + input.userId + " O: " + input.getOpenIdToken());
String tableName = "User";
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("userName", new AttributeValue().withS(input.getUserName()));
item.put("passwordHash", new AttributeValue().withS(input.getPasswordHash()));
item.put("userId", new AttributeValue().withN(input.getUserId().toString()));
item.put("openIdToken", new AttributeValue().withS(input.getOpenIdToken()));
PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName).withItem(item);
System.out.println("item request");
client.putItem(itemRequest);
System.out.println("client put item request");
item.clear();
return "Success";
}
这是错误消息:
"errorMessage": .... Task timed out after 5.00 seconds"
当时间增加到 15 秒而不是 5 秒时,我收到此错误:
"errorMessage": "Metaspace",
"errorType": "java.lang.OutOfMemoryError"
如果一个函数没有收到任何请求,那么亚马逊会自动将其从 Lambda 服务器中删除,这样它就不会使用任何资源。如果发生这种情况,那么下次请求进来时,该函数必须重新部署到 Lambda 服务器,然后才能处理该请求。这叫做 "cold start"
与用 python 或 nodejs 编写的函数相比,从冷启动到 JAVA 到 运行 编写的 lambda 函数需要更长的时间和更多的资源。因此,建议分配至少 10 秒的时间限制和 512MB 的内存来启动。随后的 运行 大约需要 20-30 毫秒。
这是一项关于用所有 3 种语言编写的函数的冷启动时间和随之而来的 运行 时间的研究。
JUnit4 测试比 Lambda 快 运行s 可能是因为冷启动。