Optimize dashboard static file serving with async I/O

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-14 14:16:52 +00:00
1 parent 695adb6dc9
commit cd7b19ead7
2 files changed
+118 -9

No files matched your search

+92
View File
@@ -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);
+26 -9
View File
@@ -9,7 +9,7 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) {
// this file is in <root>/src/dashboard.mjs, so '..' goes to <root> // this file is in <root>/src/dashboard.mjs, so '..' goes to <root>
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); 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 // URL parsing
const url = new URL(req.url, `http://${req.headers.host}`); const url = new URL(req.url, `http://${req.headers.host}`);
let pathname = url.pathname; let pathname = url.pathname;
@@ -25,10 +25,13 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) {
// Priority: User's CWD > Package default // Priority: User's CWD > Package default
if (pathname === '/torch-config.json') { if (pathname === '/torch-config.json') {
const userConfigPath = path.resolve(process.cwd(), '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' }); res.writeHead(200, { 'Content-Type': 'application/json' });
fs.createReadStream(userConfigPath).pipe(res); fs.createReadStream(userConfigPath).pipe(res);
return; return;
} catch {
// Fall through
} }
// If not found in CWD, fall through to serve from packageRoot (if it exists there) // If not found in CWD, fall through to serve from packageRoot (if it exists there)
// or return empty object if missing? // 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('^(\\.\\.[\\/\\\\])+'), ''); const safePath = path.normalize(pathname).replace(new RegExp('^(\\.\\.[\\/\\\\])+'), '');
let filePath = path.join(packageRoot, safePath); let filePath = path.join(packageRoot, safePath);
// If directory, try index.html try {
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { let stats = await fs.promises.stat(filePath);
filePath = path.join(filePath, 'index.html'); if (stats.isDirectory()) {
} const indexPath = path.join(filePath, 'index.html');
try {
// Check if file exists and is a file const indexStats = await fs.promises.stat(indexPath);
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { 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.writeHead(404);
res.end('Not Found'); res.end('Not Found');
return; return;