-
Notifications
You must be signed in to change notification settings - Fork 0
/
coldstarter.js
44 lines (39 loc) · 997 Bytes
/
coldstarter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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(
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) {}
//randomize order
let funcs = ["Function", "FunctionWithEnv"];
if (Math.random() < 0.5) {
funcs = funcs.reverse();
}
//invoke
for (const f of funcs) {
console.log(await invoke(f));
}
};