forked from mtusi2026/mtusi2026lab3
perf(bench): add scaling experiments and saturation diagnostics
Compare API and PostgreSQL CPU budgets and connection pool limits. Collect repeatable measurements and generate report aggregates and graphs.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// Loaded only by test/compose.diagnostics.yaml. No HTTP endpoint or credentials.
|
||||
const { Pool } = require('pg');
|
||||
const fs = require('node:fs');
|
||||
const { performance, monitorEventLoopDelay } = require('node:perf_hooks');
|
||||
|
||||
const states = new Map();
|
||||
const originalConnect = Pool.prototype.connect;
|
||||
Pool.prototype.connect = function (...args) {
|
||||
let state = states.get(this);
|
||||
if (!state) {
|
||||
state = { acquisitions: 0, errors: 0, acquire_ms: 0, interval_max_ms: 0,
|
||||
interval_max_waiting: 0, interval_max_busy: 0 };
|
||||
states.set(this, state);
|
||||
}
|
||||
const started = performance.now();
|
||||
const finish = (error) => {
|
||||
const elapsed = performance.now() - started;
|
||||
state.acquisitions += 1;
|
||||
state.errors += Number(Boolean(error));
|
||||
state.acquire_ms += elapsed;
|
||||
state.interval_max_ms = Math.max(state.interval_max_ms, elapsed);
|
||||
};
|
||||
if (typeof args[0] === 'function') {
|
||||
const callback = args[0];
|
||||
args[0] = (error, client, release) => {
|
||||
finish(error);
|
||||
callback(error, client, release);
|
||||
};
|
||||
return originalConnect.apply(this, args);
|
||||
}
|
||||
return originalConnect.apply(this, args).then(
|
||||
(client) => { finish(null); return client; },
|
||||
(error) => { finish(error); throw error; },
|
||||
);
|
||||
};
|
||||
|
||||
const output = '/tmp/scaling-pool.jsonl';
|
||||
fs.writeFileSync(output, '');
|
||||
const delay = monitorEventLoopDelay({ resolution: 20 });
|
||||
delay.enable();
|
||||
function counters(path) {
|
||||
try {
|
||||
return Object.fromEntries(fs.readFileSync(path, 'utf8').trim().split('\n')
|
||||
.map((line) => { const [key, value] = line.split(/\s+/); return [key, Number(value)]; }));
|
||||
} catch { return null; }
|
||||
}
|
||||
let lastWrite = performance.now();
|
||||
const timer = setInterval(() => {
|
||||
for (const [pool, state] of states) {
|
||||
state.interval_max_waiting = Math.max(state.interval_max_waiting, pool.waitingCount);
|
||||
state.interval_max_busy = Math.max(state.interval_max_busy, pool.totalCount - pool.idleCount);
|
||||
}
|
||||
if (performance.now() - lastWrite < 1000) return;
|
||||
lastWrite = performance.now();
|
||||
const pools = [...states].map(([pool, state]) => ({
|
||||
...state, total: pool.totalCount, idle: pool.idleCount, waiting: pool.waitingCount,
|
||||
}));
|
||||
let memory = null;
|
||||
try { memory = Number(fs.readFileSync('/sys/fs/cgroup/memory.current', 'utf8')); } catch {}
|
||||
fs.appendFileSync(output, JSON.stringify({ time_ms: Date.now(), pid: process.pid,
|
||||
instance: process.env.INSTANCE_ID, pools, cpu: counters('/sys/fs/cgroup/cpu.stat'),
|
||||
memory_bytes: memory, memory_events: counters('/sys/fs/cgroup/memory.events'),
|
||||
rss_bytes: process.memoryUsage().rss, process_cpu: process.cpuUsage(),
|
||||
event_loop_delay_ms: { mean: delay.mean / 1e6, p99: delay.percentile(99) / 1e6 },
|
||||
}) + '\n');
|
||||
delay.reset();
|
||||
for (const state of states.values()) {
|
||||
state.interval_max_ms = 0;
|
||||
state.interval_max_waiting = 0;
|
||||
state.interval_max_busy = 0;
|
||||
}
|
||||
}, 100);
|
||||
timer.unref();
|
||||
Reference in New Issue
Block a user