Created
August 15, 2025 17:49
-
-
Save perpil/4a056b992838655c463a46f335b1712f to your computer and use it in GitHub Desktop.
How I measure E2E latency of lambdas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LambdaClient, InvokeCommand, LogType } from "@aws-sdk/client-lambda"; | |
| const client = new LambdaClient({ | |
| region: process.env.AWS_REGION, | |
| maxAttempts: 1, | |
| }); | |
| const invoke = async (funcName, payload = {}) => { | |
| const command = new InvokeCommand({ | |
| FunctionName: funcName, | |
| Payload: JSON.stringify(payload), | |
| LogType: LogType.None, | |
| }); | |
| let start = Date.now(); | |
| const { Payload } = await client.send(command); | |
| let latency = Date.now() - start; | |
| let { requestId, coldstart } = | |
| JSON.parse(Buffer.from(Payload).toString()).body; | |
| return { | |
| funcName, | |
| requestId, | |
| coldstart, | |
| latency, | |
| }; | |
| }; | |
| export const handler = async (event, context) => { | |
| //prime the http client | |
| try { | |
| await invoke("non-existent"); | |
| } catch (e) {} | |
| //don't always go in the same order | |
| let funcs = ["FastColdstart", "SlowColdstart", "MediumColdstart", "AwsLiteColdstart","LLRTColdstart"]; | |
| if (Math.random() < 0.5) { | |
| funcs = funcs.reverse(); | |
| } | |
| //invoke | |
| for (const f of funcs) { | |
| console.log(await invoke(f)); | |
| } | |
| }; |
Author
Author
This is the cron expression I run this lambda on from eventbridge to not encounter on the hour surges in traffic:
8,16,24,32,40,48,56 * * * ? *
Author
The functions that are invoked spit out whether they are coldstarts or not and the request id. The requestId isn't important, but the coldstart flag is.
{
"requestId": "49cc9622-f173-455e-aa5a-2ef2975e2709",
"coldstart": true
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the cloudwatch insights query I run to summarize E2E time of the coldstarts: