mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
150 lines
8.7 KiB
JavaScript
150 lines
8.7 KiB
JavaScript
'use strict';
|
|
|
|
// Source guards for the CPU monitor + governor + Admin Panel / Problems / CPU
|
|
// usage report (Meteor/ESM modules that cannot be require()d under plain Node).
|
|
//
|
|
// Run: node tests/cpuMonitorWiring.test.cjs
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
|
|
let passed = 0;
|
|
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
console.log('cpuMonitorWiring:');
|
|
|
|
check('monitor samples system CPU and records only start/end to the cpu stream', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/os\.cpus\(\)/.test(m) && /idle/.test(m), 'measures system-wide CPU from os.cpus()');
|
|
assert.ok(/HighCpuTracker/.test(m), 'uses the start/end tracker');
|
|
assert.ok(/setInterval/.test(m), 'samples on an interval');
|
|
assert.ok(/'start'/.test(m) && /'end'/.test(m), 'records start and end only');
|
|
const log = read('server/lib/cpuLog.js');
|
|
assert.ok(/stream: 'cpu'/.test(log), 'writes to the cpu event stream');
|
|
});
|
|
|
|
check('governor lets long operations slow down when CPU is high', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/export function isHighCpu/.test(m) && /export function pauseIfBusy/.test(m), 'isHighCpu + pauseIfBusy');
|
|
const c = read('server/migrations/correctFileExtensions.js');
|
|
assert.ok(/pauseIfBusy/.test(c) && /setActivity/.test(c), 'batch corrector pauses + labels activity');
|
|
});
|
|
|
|
check('the detail says what WeKan was doing during high CPU', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/currentActivity/.test(m) && /load/.test(m), 'includes activity label + load average');
|
|
});
|
|
|
|
check('logs the automatic mitigation taken and whether it lowered CPU', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/action: 'rate-limited'/.test(m), 'writes a "mitigation taken" row');
|
|
assert.ok(/automatic mitigation: slowing down/.test(m), 'says what was slowed down');
|
|
assert.ok(/mitigationSummary/.test(m), 'end row reports mitigation effect');
|
|
assert.ok(/mitigationStartPct/.test(m) && /minPctAfterMitigation/.test(m), 'measures CPU before vs after');
|
|
assert.ok(/noticeably lower/.test(m), 'states whether pausing helped');
|
|
});
|
|
|
|
check('on high CPU, WeKan reads FerretDB status before conditionally slowing it down', () => {
|
|
const g = read('server/lib/ferretdbGovernor.js');
|
|
assert.ok(/throttle: 1/.test(g), 'calls the general FerretDB throttle command');
|
|
assert.ok(/export function slowDownFerretDb/.test(g) && /export function resumeFerretDb/.test(g),
|
|
'slow-down + resume');
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/governFerretStart/.test(m) && /governFerretEnd/.test(m), 'wired into start/end');
|
|
assert.ok(/slowDownFerretDb\(0, 0\)/.test(m), 'starts with a status-only read');
|
|
assert.ok(/asked FerretDB to slow down/.test(m), 'logs what FerretDB was asked');
|
|
assert.ok(/commandsProcessed/.test(m), 'logs FerretDB activity');
|
|
assert.ok(/asked FerretDB to resume/.test(m), 'FerretDB resumes when CPU drops');
|
|
});
|
|
|
|
check('FerretDB CPU and the zero cap prevent read-path throttling', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/FERRET_SLOWDOWN_MAX_MS > 0/.test(m), 'zero cap prevents the initial delay');
|
|
assert.ok(/FERRET_SLOWDOWN_MAX_MS === 0\) return/.test(m), 'zero cap prevents adjustment');
|
|
assert.ok(/lastFerretProcessCpu >= FERRET_PROC_HIGH_PCT && !currentActivity/.test(m),
|
|
'idle WeKan does not throttle FerretDB when FerretDB is the CPU source');
|
|
});
|
|
|
|
check('the FerretDB slow-down escalates until CPU has headroom, then holds', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/governFerretAdjust/.test(m), 'adaptive adjust each sample');
|
|
assert.ok(/FERRET_SLOWDOWN_MAX_MS/.test(m) && /CPU_TARGET_PCT/.test(m), 'escalation cap + headroom target');
|
|
assert.ok(/increased FerretDB slow-down/.test(m), 'logs each increase of the delay');
|
|
assert.ok(/enough CPU is now free for other processes/.test(m), 'logs when the target is reached');
|
|
assert.ok(/did not free enough CPU/.test(m), 'logs when even max delay is not enough');
|
|
assert.ok(/\* 2/.test(m), 'delay increases (doubles) between operations');
|
|
});
|
|
|
|
check('WeKan rate-limits asks, times out, backs off, and logs the outage start→end', () => {
|
|
const m = read('server/lib/cpuMonitor.js');
|
|
assert.ok(/FERRET_ASK_MIN_INTERVAL_MS/.test(m), 'minimum gap between FerretDB asks (no flooding)');
|
|
assert.ok(/ferretDbInCooldown/.test(m), 'does not ask while backing off');
|
|
assert.ok(/became unresponsive/.test(m), 'logs when FerretDB stops answering (with start time)');
|
|
assert.ok(/ferretUnavailableSince/.test(m), 'remembers when unresponsiveness started');
|
|
assert.ok(/responded again/.test(m), 'logs recovery');
|
|
assert.ok(/unresponsive \$\{fmtAt\(since\)\} → \$\{fmtAt\(new Date\(\)\)\}/.test(m), 'recovery row reports start→end span');
|
|
assert.ok(/operationsSummary/.test(m), 'recovery row includes what FerretDB was doing');
|
|
const g = read('server/lib/ferretdbGovernor.js');
|
|
assert.ok(/withTimeout/.test(g) && /COMMAND_TIMEOUT_MS/.test(g), 'per-call timeout');
|
|
assert.ok(/COOLDOWN_MS/.test(g) && /cooldownUntil/.test(g), 'cooldown back-off after failure');
|
|
assert.ok(/export function ferretDbInCooldown/.test(g) && /export function ferretDbUnavailable/.test(g), 'state accessors');
|
|
});
|
|
|
|
check('FerretDB self-regulates its own CPU and reports its operations summary', () => {
|
|
// The FerretDB fork is a companion repo, cloned into .tools/ - one ignored
|
|
// directory for all of them, instead of one ignored subdirectory each at the
|
|
// repo root. It used to be read from FerretDB/ here, and this guard broke the
|
|
// moment it moved; the path comes from the layout now, and a checkout that has
|
|
// not cloned it yet is skipped rather than failed - it is another repository,
|
|
// and not every checkout has it.
|
|
const FERRET = '.tools/FerretDB';
|
|
if (!fs.existsSync(path.join(__dirname, '..', FERRET, 'internal/handler/selfregulate.go'))) {
|
|
console.log(` (${FERRET} is not cloned here - ./build.sh clones it on demand)`);
|
|
return;
|
|
}
|
|
const s = read(`${FERRET}/internal/handler/selfregulate.go`);
|
|
assert.ok(/nextAutoSlowdown/.test(s) && /autoSlowdownMs/.test(s), 'autonomous delay decision');
|
|
assert.ok(/procStatCPU/.test(s) && /\/proc\/stat/.test(s), 'measures host CPU itself');
|
|
assert.ok(/runSelfRegulation/.test(s), 'background loop');
|
|
const t = read(`${FERRET}/internal/handler/throttle.go`);
|
|
assert.ok(/effectiveDelay/.test(t) && /autoSlowdownMs/.test(t), 'effective delay = max(client, self-regulated)');
|
|
assert.ok(/commandSummary/.test(t) && /commandCounts/.test(t), 'per-command summary of what FerretDB is doing');
|
|
const mt = read(`${FERRET}/internal/handler/msg_throttle.go`);
|
|
assert.ok(/operationsSummary/.test(mt), 'summary returned in the throttle response');
|
|
});
|
|
|
|
check('CPU usage report is wired into Admin Panel / Problems', () => {
|
|
assert.ok(/'security', 'speed', 'tests', 'cpu'/.test(read('models/eventLog.js')), 'cpu event stream registered');
|
|
const jade = read('client/components/settings/adminProblems.jade');
|
|
assert.ok(/stream="cpu"/.test(jade), 'report template');
|
|
// The side menu is data now (docs/Features/Page/Left-Menu.md).
|
|
assert.ok(/'report-cpu'/.test(read('client/components/settings/adminProblems.js')), 'menu item');
|
|
const js = read('client/components/settings/adminProblems.js');
|
|
// The pane's state is the shared activeReport id now, not a ReactiveVar of its
|
|
// own, so what there is to check is that the menu entry and the template agree
|
|
// on the id.
|
|
const paneJade = read('client/components/settings/adminProblems.jade');
|
|
assert.ok(/isPane 'report-cpu'/.test(paneJade) && /cpuReportTitle/.test(js),
|
|
'the pane is rendered, and the menu entry has its title');
|
|
assert.ok(/"cpuReportTitle": "CPU usage"/.test(read('imports/i18n/data/en.i18n.json')), 'title string');
|
|
});
|
|
|
|
check('low-load statistics schedule one heavy maintenance operation at a time', () => {
|
|
const monitor = read('server/lib/cpuMonitor.js');
|
|
assert.match(monitor, /recentSamples/);
|
|
assert.match(monitor, /minimum[\s\S]*average[\s\S]*maximum[\s\S]*lowestAt/);
|
|
assert.match(monitor, /export async function runWhenCpuLow/);
|
|
assert.match(monitor, /maintenanceLease/);
|
|
assert.match(monitor, /consecutiveLowSamples < MAINTENANCE_LOW_SAMPLES/);
|
|
assert.match(monitor, /low-load-window-timeout/,
|
|
'heavy work must defer instead of running forever under load');
|
|
const admin = read('client/components/settings/adminProblems.js');
|
|
assert.match(admin, /this\.stream === 'cpu' \|\| this\.stream === 'performance'/,
|
|
'rolling CPU statistics are visible on both CPU usage and Problems -> Speed');
|
|
assert.match(read('client/components/settings/adminProblems.jade'), /min\/avg\/max/);
|
|
});
|
|
|
|
console.log(`\ncpuMonitorWiring: ${passed} checks passed`);
|