Skip to content

Instantly share code, notes, and snippets.

@sashalikesplanes
Created November 5, 2024 19:03
Show Gist options
  • Save sashalikesplanes/37807c11bb62cec91fe63b3d79c6d368 to your computer and use it in GitHub Desktop.
Save sashalikesplanes/37807c11bb62cec91fe63b3d79c6d368 to your computer and use it in GitHub Desktop.
Lambda HTTP Benchmark
// Results
const results = {
averageMs: '16.882',
minMs: '9.618',
maxMs: '181.037',
stdDevMs: '11.790',
totalRuns: 1000
};
// test-client.js
export const handler = async (event, context) => {
return { hello: "world"}
};
// test-server.js
export const handler = async (event, context) => {
const warmupRuns = 10;
const times = [];
const iters = 1000;
// Helper function for delay
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Warmup runs
console.log(`Performing ${warmupRuns} warmup runs...`);
for (let i = 0; i < warmupRuns; i++) {
await req();
await delay(50); // 50ms delay between warmup calls
}
// Actual benchmark runs
for (let i = 0; i < iters; i++) {
await delay(50); // 50ms delay before each call
const start = process.hrtime.bigint();
try {
await req();
const end = process.hrtime.bigint();
// Convert to milliseconds
times.push(Number(end - start) / 1_000_000);
} catch (error) {
console.error(`Error in iteration ${i}:`, error);
}
}
const average = times.reduce((sum, cur) => sum + cur, 0) / times.length;
const min = Math.min(...times);
const max = Math.max(...times);
// Calculate standard deviation
const variance = times.reduce((acc, time) =>
acc + Math.pow(time - average, 2), 0) / times.length;
const stdDev = Math.sqrt(variance);
return {
statusCode: 200,
body: JSON.stringify({
averageMs: average.toFixed(3),
minMs: min.toFixed(3),
maxMs: max.toFixed(3),
stdDevMs: stdDev.toFixed(3),
totalRuns: times.length
})
};
};
async function req() {
// call test-server.js via lambda URL
const res = await fetch("https://hncy75xry4mv2el5mh7say3edm0zthyl.lambda-url.eu-west-1.on.aws/");
const body = await res.json();
return body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment