我目前正在研究用Java编写的AWS Lambda函数。它需要从S3获取对象,因此,我设置了IAM角色,并在Lambda的处理程序中构建了S3客户端:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
public class Example implements RequestHandler<Void, Void> {
@Override
public Void handleRequest(Void nothing, Context ctx) {
long start = System.currentTimeMillis();
AmazonS3 amazonS3 = AmazonS3ClientBuilder.defaultClient();
ctx.getLogger().log("Creating S3 client took " + (System.currentTimeMillis() - start) + "ms");
...
return null;
}
}
但是,由于使用AmazonS3ClientBuilder
速度非常慢,因此在向该函数分配192MB时,log语句会打印出以下时序:
Creating S3 client took 13541ms
Creating S3 client took 16482ms
Creating S3 client took 13617ms
Creating S3 client took 16380ms
即使将内存增加到3008MB以获取最大处理能力(AWS为Lambdas分配与内存成正比的CPU能力),获取客户端仍需要1-2秒:
Creating S3 client took 1413ms
Creating S3 client took 1170ms
Creating S3 client took 1528ms
Creating S3 client took 1394ms
这些时间是在冷启动场景中记录的,我正在缓存AmazonS3
实例以供后续请求使用,但是在不热的Lambda上构建一个S3客户端可能要花费16秒以上,这似乎非常极端。
我是否滥用了AmazonS3ClientBuilder
,可能是由于未覆盖某些默认值而导致性能下降?如何加快客户端初始化?
适用于Java 2.0的AWS开发工具包于11月发布。基本上,这是对1.x版的重写,并且似乎已经改善了很多性能。从问题中迁移代码以使用新的SDK将为我们提供类似于以下内容:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.services.s3.S3Client;
public class Example implements RequestHandler<Void, Void> {
@Override
public Void handleRequest(Void nothing, Context ctx) {
long start = System.currentTimeMillis();
S3Client s3Client= S3Client.create();
ctx.getLogger().log("Creating S3 client took " + (System.currentTimeMillis() - start) + "ms");
...
return null;
}
}
为该功能分配192MB时:
Creating S3 client took 9380ms
Creating S3 client took 9719ms
Creating S3 client took 10098ms
Creating S3 client took 9519ms
为该功能分配3008MB时:
Creating S3 client took 884ms
Creating S3 client took 873ms
Creating S3 client took 886ms
Creating S3 client took 877ms
基于这些粗略的数字,使用SDK版本2时,在冷启动方案中创建S3客户端的时间减少了三分之一以上。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句