更新
以下原始测试代码在很大程度上是正确的,但在@ Node-JSbot中,应按照@ Michael-sqlbot提供的SDK link来设置各种AWS服务
// manager
const AWS = require("aws-sdk")
const https = require('https');
const agent = new https.Agent({
maxSockets: 498 // workers hit this level; expect plus 1 for the manager instance
});
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: 'us-east-2', // Initial concurrency burst limit = 500
httpOptions: { // <--- replace the default of 50 (https) by
agent: agent // <--- plugging the modified Agent into the service
}
})
// NOW begin the manager handler code
在计划一项新服务时,我正在进行一些初步的压力测试。在阅读了每个帐户的the 1,000 concurrent execution limit和initial burst rate(在us-east-2中为500)之后,我期望马上至少实现500个突发并发执行。以下是CloudWatch Lambda指标的屏幕截图。 无论我尝试哪种参数组合,我都无法完成51个并发执行。这是测试代码:
// worker
exports.handler = async (event) => {
// declare sleep promise
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// return after one second
let nStart = new Date().getTime()
await sleep(1000)
return new Date().getTime() - nStart; // report the exact ms the sleep actually took
};
// manager
exports.handler = async(event) => {
const invokeWorker = async() => {
try {
let lambda = new AWS.Lambda() // NO! DO NOT DO THIS, SEE UPDATE ABOVE
var params = {
FunctionName: "worker-function",
InvocationType: "RequestResponse",
LogType: "None"
};
return await lambda.invoke(params).promise()
}
catch (error) {
console.log(error)
}
};
try {
let nStart = new Date().getTime()
let aPromises = []
// invoke workers
for (var i = 1; i <= 3000; i++) {
aPromises.push(invokeWorker())
}
// record time to complete spawning
let nSpawnMs = new Date().getTime() - nStart
// wait for the workers to ALL return
let aResponses = await Promise.all(aPromises)
// sum all the actual sleep times
const reducer = (accumulator, response) => { return accumulator + parseInt(response.Payload) };
let nTotalWorkMs = aResponses.reduce(reducer, 0)
// show me
let nTotalET = new Date().getTime() - nStart
return {
jobsCount: aResponses.length,
spawnCompletionMs: nSpawnMs,
spawnCompletionPct: `${Math.floor(nSpawnMs / nTotalET * 10000) / 100}%`,
totalElapsedMs: nTotalET,
totalWorkMs: nTotalWorkMs,
parallelRatio: Math.floor(nTotalET / nTotalWorkMs * 1000) / 1000
}
}
catch (error) {
console.log(error)
}
};
Response:
{
"jobsCount": 3000,
"spawnCompletionMs": 1879,
"spawnCompletionPct": "2.91%",
"totalElapsedMs": 64546,
"totalWorkMs": 3004205,
"parallelRatio": 0.021
}
Request ID:
"43f31584-238e-4af9-9c5d-95ccab22ae84"
我达到了我没有提到的其他限制吗?我的测试代码是否存在缺陷?我当时试图用3000名工人来达到极限,但是没有遇到节流的情况,我想这是由于异步调用重试行为造成的。
编辑:两个Lambda均不涉及VPC;选择输入中的设置为“无VPC”。
编辑:在修复前后显示Cloudwatch
答案 0 :(得分:1)
Lambda并发性并不是决定函数可伸缩性的唯一因素。如果您的Lambda函数在VPC内运行,则将需要ENI(弹性网络接口),该ENI允许往返于容器的以太网流量(Lambda函数)。
您的限制可能是由于请求太多ENI(一次50个)而发生的。您可以通过查看Manager lambda函数的日志并在尝试调用子容器之一时查找错误消息来进行检查。如果该错误看起来像以下内容,则说明您的问题是ENI。
Lambda was not able to create an ENI in the VPC of the Lambda function because the limit for Network Interfaces has been reached.
答案 1 :(得分:1)
有很多潜在的可疑对象,特别是由于您正在从Lambda调用Lambda,但是您始终专注于看到并发值为50(看似随意的限制(和可疑的整数)),这提醒我JavaScript SDK中有一个反步枪:
在Node.js中,您可以设置每个源的最大连接数。如果设置了maxSockets,则低级HTTP客户端会将请求排队,并在它们可用时将其分配给套接字。
当然,“来源”表示方案+主机名的任何唯一组合,在这种情况下,SDK连接到us-east-2中的Lambda服务endpoint,以便SDK调用该服务Invoke
方法,https://lambda.us-east-2.amazonaws.com
。
这使您可以一次设置给定源的并发请求数的上限。降低此值可以减少收到的节流或超时错误的数量。但是,由于请求会一直排队直到套接字可用,因此它也会增加内存使用量。
...
使用默认值
https
时,SDK从maxSockets
中获取globalAgent
值。如果maxSockets
的值未定义或为Infinity,则SDK会假设maxSockets
的值为50。https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-configuring-maxsockets.html