Files
RuneStone/tests/unit/release/measureFeatures.spec.ts
T
2026-11-16 04:21:21 -07:00

168 lines
9.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { inflateSync } from 'node:zlib';
import { checkTarget, INSTALLED_EXE, markdownTable, median, parseArguments, processTree, summarize } from '../../../scripts/measure/core.mjs';
import { crc32, encodePng, gradientPixel, PNG_SIGNATURE, readPngChunks } from '../../../scripts/measure/png.mjs';
import { readPdfXref, writePdf } from '../../../scripts/measure/pdf.mjs';
import { cardFileName, pmFiles, seededUuid } from '../../../scripts/measure/fixture.mjs';
const required = ['--exe', 'build/runestone.exe', '--identifier', 'com.runestone.phase7-ram', '--stage', 'reference', '--mode', 'plugin', '--features', 'git,pdf'];
const env = { APPDATA: 'C:\\Users\\Test\\AppData\\Roaming', LOCALAPPDATA: 'C:\\Users\\Test\\AppData\\Local' };
test('the measurement tool reads its arguments and their defaults', () => {
const options = parseArguments(required);
assert.equal(options.exe, 'build/runestone.exe');
assert.equal(options.identifier, 'com.runestone.phase7-ram');
assert.deepEqual(options.features, ['git', 'pdf']);
assert.equal(options.runs, 3);
assert.equal(options.port, 9241);
assert.equal(options.freshFixture, false);
const custom = parseArguments([...required, '--runs', '5', '--port', '9300', '--fresh-fixture', '--only', 'base,git:in-use']);
assert.equal(custom.runs, 5);
assert.equal(custom.port, 9300);
assert.equal(custom.freshFixture, true);
assert.deepEqual(custom.only, ['base', 'git:in-use']);
assert.deepEqual(parseArguments([...required.slice(0, 8), '--features', 'git,git, pm']).features, ['git', 'pm']);
});
test('the measurement tool rejects missing, repeated, and unknown arguments', () => {
for (const args of [
[],
required.slice(0, -2),
[...required, '--runs'],
[...required, '--runs', 'three'],
[...required, '--runs', '0'],
[...required, '--port', '80'],
[...required, '--mode', 'native'],
[...required, '--force'],
[...required.slice(0, 6), '--mode', 'hybrid', '--features', 'git'],
[...required.slice(0, 8), '--features', 'git,editor'],
[...required.slice(0, 4), '--stage', '../escape', '--mode', 'plugin', '--features', 'git'],
[...required, '--only', 'pm:in-use'],
[...required, '--only', 'git:busy'],
]) assert.throws(() => parseArguments(args), Error, args.join(' '));
});
test('the measurement tool refuses the installed app and its identifier', () => {
assert.throws(() => checkTarget({ exe: INSTALLED_EXE, identifier: 'com.runestone.phase7-ram' }, env), /installed RuneStone/);
assert.throws(() => checkTarget({ exe: INSTALLED_EXE.toUpperCase(), identifier: 'com.runestone.phase7-ram' }, env), /installed RuneStone/);
assert.throws(() => checkTarget({ exe: 'C:\\Users\\Test\\AppData\\Local\\RuneStone\\runestone.exe', identifier: 'com.runestone.phase7-ram' }, env), /installed RuneStone/);
assert.throws(() => checkTarget({ exe: 'C:\\Users\\Test\\AppData\\Local\\RuneStone\\other.exe', identifier: 'com.runestone.phase7-ram' }, env), /installed RuneStone/);
for (const identifier of ['com.runestone.app', 'COM.RUNESTONE.APP', 'com.example.test', 'com.runestone.', 'com.runestone.../x', 'com.runestone.Test', undefined])
assert.throws(() => checkTarget({ exe: 'D:\\build\\runestone.exe', identifier }, env), /Refusing the identifier/, String(identifier));
assert.deepEqual(checkTarget({ exe: 'D:\\build\\runestone.exe', identifier: 'com.runestone.phase7-ram' }, env), {
roaming: 'C:\\Users\\Test\\AppData\\Roaming\\com.runestone.phase7-ram',
local: 'C:\\Users\\Test\\AppData\\Local\\com.runestone.phase7-ram',
});
});
test('the median ignores missing values and averages the two middle values', () => {
assert.equal(median([]), null);
assert.equal(median([null, undefined, Number.NaN]), null);
assert.equal(median([3, 1, 2]), 2);
assert.equal(median([4, 1, 3, 2]), 2.5);
assert.equal(median([5, null, 1]), 3);
const summary: Record<string, number | null> = summarize([{ privateMB: 100, startMs: 900 }, { privateMB: 120, startMs: 1100 }, { error: 'failed', privateMB: 999 }]);
assert.equal(summary.runs, 3);
assert.equal(summary.failed, 1);
assert.equal(summary.privateMB, 110);
assert.equal(summary.startMs, 1000);
});
test('the process tree walk sums only the descendants of the started process', () => {
const list = [
{ ProcessId: 4, ParentProcessId: 0, Name: 'System', PrivatePageCount: 1, WorkingSetSize: 1, Created: 1 },
{ ProcessId: 100, ParentProcessId: 4, Name: 'runestone.exe', PrivatePageCount: 10, WorkingSetSize: 20, Created: 10 },
{ ProcessId: 101, ParentProcessId: 100, Name: 'msedgewebview2.exe', PrivatePageCount: 30, WorkingSetSize: 40, Created: 11 },
{ ProcessId: 102, ParentProcessId: 101, Name: 'msedgewebview2.exe', PrivatePageCount: 50, WorkingSetSize: 60, Created: 12 },
{ ProcessId: 103, ParentProcessId: 100, Name: 'pwsh.exe', PrivatePageCount: 5, WorkingSetSize: 6, Created: 13 },
// The installed app, a sibling tree that the walk must not count.
{ ProcessId: 200, ParentProcessId: 4, Name: 'runestone.exe', PrivatePageCount: 1000, WorkingSetSize: 1000, Created: 9 },
{ ProcessId: 201, ParentProcessId: 200, Name: 'msedgewebview2.exe', PrivatePageCount: 1000, WorkingSetSize: 1000, Created: 9 },
// A process whose parent ID was reused: it started before the process with that ID.
{ ProcessId: 104, ParentProcessId: 100, Name: 'stale.exe', PrivatePageCount: 7, WorkingSetSize: 7, Created: 5 },
];
const tree = processTree(list, 100);
assert.deepEqual(tree.processes.map(item => item.ProcessId), [100, 101, 103, 102]);
assert.equal(tree.privateBytes, 95);
assert.equal(tree.workingSet, 126);
assert.equal(tree.count, 4);
assert.deepEqual(tree.names, { 'runestone.exe': 1, 'msedgewebview2.exe': 2, 'pwsh.exe': 1 });
assert.equal(processTree(list, 999).count, 0);
// A cycle in a stale process list does not loop.
assert.equal(processTree([{ ProcessId: 1, ParentProcessId: 2, Name: 'a' }, { ProcessId: 2, ParentProcessId: 1, Name: 'b' }], 1).count, 2);
});
test('the PNG writer gives a valid signature, chunk CRCs, and pixel data', () => {
assert.equal(crc32(Buffer.from('123456789')), 0xcbf43926);
const png = encodePng(4, 3, gradientPixel(4, 3, [0, 0, 0], [255, 255, 255], 0));
assert.ok(png.subarray(0, 8).equals(PNG_SIGNATURE));
const chunks = readPngChunks(png);
assert.deepEqual(chunks.map(chunk => chunk.type), ['IHDR', 'IDAT', 'IEND']);
for (const chunk of chunks) assert.equal(chunk.stored, chunk.computed, chunk.type);
assert.equal(chunks[0].data.readUInt32BE(0), 4);
assert.equal(chunks[0].data.readUInt32BE(4), 3);
assert.equal(chunks[0].data[8], 8);
assert.equal(chunks[0].data[9], 2);
const raw = inflateSync(chunks[1].data);
assert.equal(raw.length, 3 * (1 + 4 * 3));
assert.equal(raw[0], 0);
assert.deepEqual([...raw.subarray(1, 4)], [0, 0, 0]);
assert.deepEqual([...raw.subarray(10, 13)], [191, 191, 191]);
assert.throws(() => encodePng(0, 1, () => [0, 0, 0]));
});
test('the PDF writer gives a header, a trailer, and xref offsets that point to their objects', () => {
const pdf = writePdf(Array.from({ length: 50 }, (_, index) => `Page ${index + 1}`));
const text = pdf.toString('latin1');
assert.ok(text.startsWith('%PDF-1.4\n'));
assert.ok(text.endsWith('%%EOF\n'));
const xref = readPdfXref(pdf);
assert.equal(xref.count, 3 + 50 * 2 + 1);
assert.equal(xref.entries[0].line, '0000000000 65535 f ');
for (const [index, entry] of xref.entries.slice(1).entries()) {
assert.equal(entry.line.length + 1, 20, 'Each xref entry is 20 bytes.');
assert.ok(entry.used);
assert.ok(text.startsWith(`${index + 1} 0 obj\n`, entry.offset), `Object ${index + 1} is at ${entry.offset}.`);
}
assert.match(text, /\/Type \/Pages \/Kids \[[^\]]+\] \/Count 50 >>/);
assert.match(text, /\(Page 25\) Tj/);
assert.match(text, new RegExp(`/Size ${xref.count} /Root 1 0 R`));
// Stream lengths match the stream bytes.
for (const match of text.matchAll(/<< \/Length (\d+) >>\nstream\n/g)) {
const start = match.index + match[0].length;
assert.ok(text.startsWith('endstream', start + Number(match[1])));
}
assert.match(writePdf(['A (b) \\ c']).toString('latin1'), /\(A \\\(b\\\) \\\\ c\) Tj/);
});
test('the Project Management fixture uses the card file names and ignore files of the real store', () => {
const files = pmFiles();
assert.equal(files.get('.runestone/pm/cards/.gitignore'), '*\n');
assert.match(files.get('.runestone/pm/.gitignore')!, /^\*$/m);
const catalog = JSON.parse(files.get('.runestone/pm/workspace.json')!);
assert.equal(catalog.schema, 1);
assert.equal(catalog.boards.length, 8);
const cards = [...files.keys()].filter(name => name.startsWith('.runestone/pm/cards/') && name.endsWith('.md'));
assert.equal(cards.length, 400);
const id = seededUuid('card-0-0');
assert.match(id, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
assert.equal(cardFileName(id), `${Buffer.from(id).toString('hex')}.md`);
assert.match(files.get(`.runestone/pm/cards/${cardFileName(id)}`)!, new RegExp(`^---\\nrunestone_pm:\\n schema: 1\\n id: ${id}\\n kind: task\\n`));
});
test('the report table has one row for each scenario and marks failures', () => {
const report = {
stage: 'reference', build: 'x.exe', identifier: 'com.runestone.phase7-ram', mode: 'plugin', runs: 3, date: '2026-10-29',
machine: { cpu: 'CPU', ramGB: 32, windows: 'Windows 11' },
rows: [
{ feature: 'base', scenario: 'base', median: { runs: 3, failed: 0, privateMB: 200.04, workingSetMB: 300, heapMB: 20, startMs: 1500.4, firstNeedMs: null, longTaskCount: 1, longTaskMaxMs: 80 } },
{ feature: 'pdf', scenario: 'in-use', median: { runs: 3, failed: 3 }, reason: 'timeout | x' },
],
};
const table = markdownTable(report);
assert.match(table, /\| base \| base \| 200\.0 \| 300\.0 \| 20\.0 \| 1500 \| – \| 1 \/ 80 \|/);
assert.match(table, /\| pdf \| in-use \| failed \|/);
assert.doesNotMatch(table, /timeout \| x/);
});