aws工具
by Yan Cui
崔燕
If you’re not familiar with how cold start works within the context of AWS Lambda, then read this post first.
如果您不熟悉冷启动在AWS Lambda上下文中的工作方式,请首先阅读此文章 。
When a Node.js Lambda function cold starts, a number of things happen:
当Node.js Lambda函数冷启动时,会发生许多事情:
If you enable active tracing for a Lambda function, you will be able to see how much time is spent on these steps in X-Ray. Unfortunately, the time it takes to initialize the container and the Node.js runtime are not recorded as segments. But you can work out from the difference in duration.
如果启用Lambda函数的主动跟踪,您将能够看到在X-Ray中这些步骤花费了多少时间。 不幸的是,初始化容器和Node.js运行时所花费的时间没有记录为段。 但是您可以从持续时间的差异中得出结论。
Here, Initialization
refers to the time it takes to initialize the handler module.
这里, Initialization
是指初始化处理程序模块所花费的时间。
The above trace is for the function below, which requires the AWS SDK and nothing else. As you can see, this simple require
added 147ms to the cold start.
上面的跟踪适用于以下功能,该功能需要AWS开发工具包,而无需其他任何功能。 如您所见,这个简单的require
为冷启动增加了147ms。
const AWS = require('aws-sdk')module.exports.handler = async () => {}
Consider this the cost of doing business when your function needs to interact with AWS resources. But, if you only need to interact with one service (e.g. DynamoDB), you can save some initialization time with this one-liner.
当您的功能需要与AWS资源进行交互时,请考虑这是开展业务的成本。 但是,如果您只需要与一项服务(例如DynamoDB)进行交互,则可以使用这种单一代码节省一些初始化时间。
const DynamoDB = require('aws-sdk/clients/dynamodb') const documentClient = new DynamoDB.DocumentClient()
It requires the DynamoDB client directly without initializing the whole AWS SDK. I ran an experiment to see how much cold start time you can save with this simple change.
它直接需要DynamoDB客户端,而无需初始化整个AWS开发工具包。 我进行了一项实验,以查看通过此简单更改可以节省多少冷启动时间。
Credit goes to my colleague Justin Caldicott for piquing my interest and doing a lot of the initial analysis.
我的同事贾斯汀·卡尔迪科特 ( Justin Caldicott)引起了我的兴趣,并做了很多初步分析,这归功于我。
In addition to the AWS SDK, we often require the XRay SDK too and use it to auto-instrument the AWS SDK. Unfortunately, the aws-xray-sdk
package also has some additional baggage that we don’t need. By default, it supports Express.js apps, MySQL and Postgres. If you are only interested in instrumenting the AWS SDK and http
/https
modules then you only need the aws-xray-sdk-core
.
除了AWS开发工具包外,我们通常也需要XRay开发工具包,并使用它来自动安装AWS开发工具包。 不幸的是, aws-xray-sdk
软件包也有一些我们不需要的额外行李。 默认情况下,它支持Express.js应用程序,MySQL和Postgres。 如果您仅对检测AWS开发工具包SDK和http
/ https
模块感兴趣,则只需要aws-xray-sdk-core
。
I tested a number of configurations:
我测试了许多配置:
requiring the XRay SDK Core and instrumenting only the DynamoDB client
Each of these functions is traced by X-Ray. Sample rate set to 100%, so we don’t miss anything. We are only interested in the duration of the Initialization segment as it corresponds to the time for initializing these dependencies.
X-Ray跟踪这些功能中的每一个。 采样率设置为100%,因此我们不会错过任何东西。 我们只对Initialization段的持续时间感兴趣,因为它对应于初始化这些依赖项的时间。
The no AWS SDK
case is our control group. We can see how much time each additional dependency adds to our Initialization
duration.
no AWS SDK
案例是我们的控制组。 我们可以看到每个其他依赖项增加了我们Initialization
时间的时间。
To collect a statistically significant sample set of data, I decided to automate the process using Step Functions.
为了收集具有统计意义的样本数据集,我决定使用“步函数”使过程自动化。
The state machine takes an input { functionName, count }
.
状态机接受输入{ functionName, count }
。
The SetStartTime
step adds the current UTC timestamp to the execution state. This is necessary as we need the start time of the experiment to fetch the relevant traces from X-Ray.
SetStartTime
步骤将当前UTC时间戳添加到执行状态。 这是必要的,因为我们需要实验的开始时间才能从X射线中提取相关迹线。
The Loop
step triggers the desired number of cold starts for the specified function. To trigger cold starts, I programmatically update an environment variable before invoking the function. That way, I ensure that every invocation is a cold start.
Loop
步骤触发指定功能所需的冷启动次数。 为了触发冷启动,我在调用函数之前以编程方式更新了环境变量。 这样,我确保每次调用都是冷启动。
The Wait30Seconds
step makes sure that all the traces are published to XRay before we attempt to analyze them.
在我们尝试对其进行分析之前, Wait30Seconds
步骤确保所有跟踪已发布到XRay。
The Analyze
step fetches all the relevant traces in XRay and outputs several statistics around the Initialization
duration.
“ Analyze
步骤将获取XRay中的所有相关迹线,并输出有关Initialization
持续时间的一些统计信息。
Each configuration is tested over 1000 cold starts. Occasionally the XRay traces are incomplete (see below). These incomplete traces are excluded in the Analyze
step.
每种配置均经过1000次冷启动测试。 有时XRay迹线不完整(请参见下文)。 这些不完整的痕迹在“ Analyze
步骤中被排除。
Each configuration is also tested with WebPack as well (using the serverless-webpack plugin). Thanks to Erez Rokah for the suggestion.
每个配置也都已通过WebPack(使用serverless-webpack插件)进行了测试。 感谢Erez Rokah的建议。
These are the Initialization
times for all the test cases.
这些是所有测试用例的Initialization
时间。
Key observations:
关键观察:
WebPack improves the Initialization
time across the board.
WebPack全面缩短了Initialization
时间。
Without any dependencies, Initialization
time averages only 1.72ms without WebPack and 0.97ms with WebPack.
没有任何依赖关系,WebPack的Initialization
时间平均仅为1.72ms,WebPack的平均Initialization
时间为0.97ms。
Originally published at theburningmonk.com on March 23, 2019.
最初于2019年3月23日发布在theburningmonk.com上。
翻译自: https://www.freecodecamp.org/news/just-how-expensive-is-the-full-aws-sdk-3713fed4fe70/
aws工具