Skip to content

Instantly share code, notes, and snippets.

@perpil
Created August 15, 2025 17:49
Show Gist options
  • Select an option

  • Save perpil/4a056b992838655c463a46f335b1712f to your computer and use it in GitHub Desktop.

Select an option

Save perpil/4a056b992838655c463a46f335b1712f to your computer and use it in GitHub Desktop.
How I measure E2E latency of lambdas
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));
}
};
@perpil
Copy link
Author

perpil commented Aug 15, 2025

This is the cloudwatch insights query I run to summarize E2E time of the coldstarts:

filter ispresent(coldstart) and  coldstart = 1
| stats count(latency) as samples, pct(latency,0) as p0, 
pct(latency,50) as p50, pct(latency,90) as p90, pct(latency,95) as p95, pct(latency,99) as p99, pct(latency,100) as p100 by funcName
| order by funcName

@perpil
Copy link
Author

perpil commented Aug 15, 2025

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 * * * ? *

@perpil
Copy link
Author

perpil commented Aug 15, 2025

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