From 41b65cbf24c9abfc5e23c51c546bbf159f585ee7 Mon Sep 17 00:00:00 2001 From: sent Date: Mon, 19 May 2025 19:17:06 -0700 Subject: [PATCH] Performance 3 --- others/warmup.mjs | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 others/warmup.mjs diff --git a/others/warmup.mjs b/others/warmup.mjs new file mode 100644 index 00000000..b9ac5f78 --- /dev/null +++ b/others/warmup.mjs @@ -0,0 +1,59 @@ +import http from 'http'; + +const endpoints = [ + 'http://localhost:3000/', + 'http://localhost:3000/g', + 'http://localhost:3000/a' +]; + +function httpGet(url, timeout = 5000) { + return new Promise((resolve) => { + const req = http.get(url, (res) => { + res.on('data', () => {}); + res.on('end', () => { + resolve({ url, statusCode: res.statusCode }); + }); + }); + + req.on('error', (e) => { + resolve({ url, error: e.message }); + }); + + req.setTimeout(timeout, () => { + req.abort(); + resolve({ url, error: 'Timeout' }); + }); + }); +} + +async function warm(retries = 2) { + const results = await Promise.all(endpoints.map(async (url) => { + for (let i = 0; i <= retries; i++) { + const res = await httpGet(url); + if (!res.error) { + console.log(`[WARMUP] ${url} → ${res.statusCode}`); + return res; + } else { + console.warn(`[WARMUP] ${url} attempt ${i + 1} failed: ${res.error}`); + } + } + return { url, error: `Failed after ${retries + 1} attempts` }; + })); + + const failed = results.filter(r => r.error); + if (failed.length) { + console.warn(`[WARMUP] Some endpoints failed to warm:`, failed); + } else { + console.log('[WARMUP] All endpoints warmed successfully'); + } +} + +async function periodicWarmup(intervalMs = 5 * 60 * 1000) { + while (true) { + await warm(); + console.log('[WARMUP] Cycle done'); + await new Promise(res => setTimeout(res, intervalMs)); + } +} + +setTimeout(() => periodicWarmup(), 2000); \ No newline at end of file