From cd7b19ead7307e496419fdd6bba1376730eaf2e4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 14 Feb 2026 14:16:52 +0000 Subject: [PATCH] Optimize dashboard static file serving with async I/O Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- scripts/benchmark-dashboard.mjs | 92 +++++++++++++++++++++++++++++++++ src/dashboard.mjs | 35 +++++++++---- 2 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 scripts/benchmark-dashboard.mjs diff --git a/scripts/benchmark-dashboard.mjs b/scripts/benchmark-dashboard.mjs new file mode 100644 index 0000000..39c6037 --- /dev/null +++ b/scripts/benchmark-dashboard.mjs @@ -0,0 +1,92 @@ +import { spawn } from 'node:child_process'; +import http from 'node:http'; + +const PORT = 3334; +const CONCURRENCY = 100; +const TOTAL_REQUESTS = 2000; +const ENDPOINT = `http://localhost:${PORT}/torch-config.example.json`; + +async function startServer() { + const child = spawn('node', ['bin/torch-lock.mjs', 'dashboard', '--port', PORT], { + stdio: 'ignore', // 'inherit' for debugging + detached: false + }); + return child; +} + +async function waitForServer() { + for (let i = 0; i < 50; i++) { + try { + await new Promise((resolve, reject) => { + const req = http.get(`http://localhost:${PORT}/dashboard/`, (res) => { + if (res.statusCode === 200) resolve(); + else reject(new Error(`Status ${res.statusCode}`)); + }); + req.on('error', reject); + req.end(); + }); + return; + } catch { + await new Promise(r => setTimeout(r, 100)); + } + } + throw new Error('Server not ready'); +} + +async function runBenchmark() { + let completed = 0; + let failures = 0; + const start = process.hrtime.bigint(); + + const promises = []; + for (let i = 0; i < CONCURRENCY; i++) { + promises.push(worker()); + } + + async function worker() { + while (completed + failures < TOTAL_REQUESTS) { + if (completed + failures >= TOTAL_REQUESTS) break; + try { + await makeRequest(); + completed++; + } catch { + failures++; + // console.error(e); + } + } + } + + await Promise.all(promises); + const end = process.hrtime.bigint(); + const duration = Number(end - start) / 1e6; // ms + + console.log(`Requests: ${completed}`); + console.log(`Failures: ${failures}`); + console.log(`Total time: ${duration.toFixed(2)}ms`); + console.log(`RPS: ${(completed / (duration / 1000)).toFixed(2)}`); + return duration; +} + +function makeRequest() { + return new Promise((resolve, reject) => { + http.get(ENDPOINT, (res) => { + res.resume(); // Consume data + if (res.statusCode === 200) resolve(); + else reject(new Error(`Status ${res.statusCode}`)); + }).on('error', reject); + }); +} + +async function main() { + console.log('Starting server...'); + const server = await startServer(); + try { + await waitForServer(); + console.log('Server ready. Benchmarking...'); + await runBenchmark(); + } finally { + server.kill(); + } +} + +main().catch(console.error); diff --git a/src/dashboard.mjs b/src/dashboard.mjs index 7fde009..2ecd8b2 100644 --- a/src/dashboard.mjs +++ b/src/dashboard.mjs @@ -9,7 +9,7 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) { // this file is in /src/dashboard.mjs, so '..' goes to const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); - const server = http.createServer((req, res) => { + const server = http.createServer(async (req, res) => { // URL parsing const url = new URL(req.url, `http://${req.headers.host}`); let pathname = url.pathname; @@ -25,10 +25,13 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) { // Priority: User's CWD > Package default if (pathname === '/torch-config.json') { const userConfigPath = path.resolve(process.cwd(), 'torch-config.json'); - if (fs.existsSync(userConfigPath)) { + try { + await fs.promises.access(userConfigPath, fs.constants.F_OK); res.writeHead(200, { 'Content-Type': 'application/json' }); fs.createReadStream(userConfigPath).pipe(res); return; + } catch { + // Fall through } // If not found in CWD, fall through to serve from packageRoot (if it exists there) // or return empty object if missing? @@ -39,13 +42,27 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) { const safePath = path.normalize(pathname).replace(new RegExp('^(\\.\\.[\\/\\\\])+'), ''); let filePath = path.join(packageRoot, safePath); - // If directory, try index.html - if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { - filePath = path.join(filePath, 'index.html'); - } - - // Check if file exists and is a file - if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { + try { + let stats = await fs.promises.stat(filePath); + if (stats.isDirectory()) { + const indexPath = path.join(filePath, 'index.html'); + try { + const indexStats = await fs.promises.stat(indexPath); + if (indexStats.isFile()) { + filePath = indexPath; + stats = indexStats; + } else { + // Found directory but index.html is not a file + throw new Error('Not found'); + } + } catch { + // Found directory but index.html does not exist + throw new Error('Not found'); + } + } else if (!stats.isFile()) { + throw new Error('Not a file'); + } + } catch { res.writeHead(404); res.end('Not Found'); return;