mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-06 12:51:54 +00:00
Optimize dashboard static file serving with async I/O
Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
695adb6dc9
commit
0448ed2932
8 files changed
+103
-101
No files matched your search
@@ -0,0 +1,88 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import http from 'node:http';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
|
||||
const PORT = 3456;
|
||||
const REQUESTS = 2000;
|
||||
const CONCURRENCY = 50;
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
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 (e) {
|
||||
await sleep(100);
|
||||
}
|
||||
}
|
||||
throw new Error('Server did not start');
|
||||
}
|
||||
|
||||
function makeRequest() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Request a small file that exists
|
||||
const req = http.get(`http://localhost:${PORT}/torch-config.example.json`, (res) => {
|
||||
res.resume(); // consume body
|
||||
res.on('end', resolve);
|
||||
});
|
||||
req.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function benchmark() {
|
||||
console.log('Starting server...');
|
||||
const serverProcess = spawn('node', ['bin/torch-lock.mjs', 'dashboard', '--port', String(PORT)], {
|
||||
stdio: 'ignore' // Suppress logs for cleaner output
|
||||
});
|
||||
|
||||
try {
|
||||
await waitForServer();
|
||||
console.log('Server ready.');
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
let sent = 0;
|
||||
const worker = async () => {
|
||||
while (true) {
|
||||
const current = sent++;
|
||||
if (current >= REQUESTS) break;
|
||||
try {
|
||||
await makeRequest();
|
||||
} catch (e) {
|
||||
// console.error(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const workers = [];
|
||||
for (let i = 0; i < CONCURRENCY; i++) {
|
||||
workers.push(worker());
|
||||
}
|
||||
|
||||
await Promise.all(workers);
|
||||
|
||||
const end = performance.now();
|
||||
const duration = end - start;
|
||||
const rps = (REQUESTS / duration) * 1000;
|
||||
|
||||
console.log(`Requests: ${REQUESTS}`);
|
||||
console.log(`Duration: ${duration.toFixed(2)}ms`);
|
||||
console.log(`RPS: ${rps.toFixed(2)}`);
|
||||
|
||||
} finally {
|
||||
serverProcess.kill();
|
||||
}
|
||||
}
|
||||
|
||||
benchmark().catch(console.error);
|
||||
Reference in new issue
Block a user