Files
qrtx c1fec73ccc 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.
2026-10-29 11:49:51 +03:00

74 lines
2.8 KiB
JavaScript

// 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();