Files
stack/packages/cli/tests/case-p10-timing-fixture.mjs

64 lines
2.5 KiB
JavaScript

// Row299 / mos-1409: observations exist only in a private test copy of the
// shipped script. Neither the timer arguments nor its control flow are changed.
const CAPTURE = 'tester_capture() (\n';
const LEGACY = '# row276-legacy-begin';
// Stable even when a mutant removes the outer timeout from this command.
const LAUNCH = ' LC_ALL=C ';
const WAIT = ' wait "$child" 2>/dev/null || rc=$?\n';
export const P10_OBSERVERS = Object.freeze({
begin: ` # row299-observer-begin-start
if [ "$maximum" = 128 ] && [ -n "\${ROW299_TIMING_FILE-}" ]; then
{ read -r row299_up row299_spare < /proc/uptime &&
printf 'begin %s\\n' "$row299_up" >> "$ROW299_TIMING_FILE"; } 2>/dev/null || :
fi
# row299-observer-begin-end
`,
end: ` # row299-observer-end-start
if [ "$maximum" = 128 ] && [ -n "\${ROW299_TIMING_FILE-}" ]; then
{ read -r row299_up row299_spare < /proc/uptime &&
printf 'end %s\\n' "$row299_up" >> "$ROW299_TIMING_FILE"; } 2>/dev/null || :
fi
# row299-observer-end-end
`,
});
function uniqueIndex(source, text) {
const first = source.indexOf(text);
if (first < 0 || source.indexOf(text, first + text.length) >= 0) {
throw new Error('P10 observer anchor must occur exactly once');
}
return first;
}
export function instrumentP10Probe(source) {
const start = uniqueIndex(source, CAPTURE);
const end = uniqueIndex(source, LEGACY);
if (end <= start) throw new Error('P10 observer capture scope is reversed');
let capture = source.slice(start, end);
uniqueIndex(capture, LAUNCH);
uniqueIndex(capture, WAIT);
capture = capture.replace(LAUNCH, P10_OBSERVERS.begin + LAUNCH);
capture = capture.replace(WAIT, WAIT + P10_OBSERVERS.end);
return source.slice(0, start) + capture + source.slice(end);
}
export function removeP10Observers(source) {
for (const block of Object.values(P10_OBSERVERS)) {
uniqueIndex(source, block);
source = source.replace(block, '');
}
return source;
}
export function readProbeSeconds(text) {
const match = /^begin ([0-9]+)\.([0-9]{2})\nend ([0-9]+)\.([0-9]{2})\n$/.exec(text);
if (!match || match[0] !== text) throw new Error('P10 observer needs exactly one complete ordered pair');
const begin = BigInt(match[1]) * 100n + BigInt(match[2]);
const end = BigInt(match[3]) * 100n + BigInt(match[4]);
if (end <= begin || end - begin > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new Error('P10 observer interval must be positive and safely representable');
}
return Number(end - begin) / 100;
}