86 lines
5.7 KiB
JavaScript
86 lines
5.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { mkdtemp, mkdir, writeFile } from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
if (!process.versions.electron) {
|
|
const { spawn } = await import('node:child_process');
|
|
const { default: electron } = await import('electron');
|
|
const root = await mkdtemp(path.join(os.tmpdir(), 'runestone-browser-performance-'));
|
|
const child = spawn(electron, [import.meta.filename, ...process.argv.slice(2)], {
|
|
windowsHide: true, stdio: 'inherit', env: { ...process.env, RS_PERFORMANCE_ROOT: root },
|
|
});
|
|
const timeout = setTimeout(() => child.kill(), 45_000);
|
|
process.exitCode = await new Promise((resolve, reject) => { child.once('exit', code => resolve(code ?? 1)); child.once('error', reject); });
|
|
clearTimeout(timeout);
|
|
} else {
|
|
const { app } = await import('electron');
|
|
const { createEngine } = await import('../src/engine.mjs');
|
|
app.enableSandbox();
|
|
app.setPath('userData', process.env.RS_PERFORMANCE_ROOT);
|
|
app.on('window-all-closed', () => {});
|
|
app.whenReady().then(async () => {
|
|
let engine, code = 0;
|
|
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
const frames = [];
|
|
let writable = true;
|
|
try {
|
|
engine = await createEngine({ directory: process.env.RS_PERFORMANCE_ROOT, canEmitFrame: () => writable, emit: value => {
|
|
if (value.event === 'frame') { frames.push({ time: performance.now(), bytes: value.data.length, tabId: value.tabId, data: value.data, mime: value.mime }); void engine?.command('frameAck', { tabId: value.tabId, frameId: value.frameId }); }
|
|
} });
|
|
const { tabId } = await engine.command('newTab');
|
|
const wc = engine.tabs.get(tabId).view.webContents;
|
|
if (wc.isLoading()) await new Promise(resolve => wc.once('did-stop-loading', resolve));
|
|
await engine.command('resize', { tabId, width: 1280, height: 800 });
|
|
await engine.command('visible', { tabId, visible: true });
|
|
await wc.executeJavaScript(`document.body.innerHTML = '<div style="height:6000px;background:linear-gradient(white,steelblue)"><canvas width="1280" height="720"></canvas></div>';
|
|
const c = document.querySelector('canvas').getContext('2d'); let tick = 0;
|
|
function paint() { c.fillStyle = '#fafafa'; c.fillRect(0,0,1280,720);
|
|
for (let i=0;i<80;i++) { c.fillStyle = 'hsl('+i*7+' 60% 50%)'; c.fillRect((i*71+tick*3)%1280,(i*39)%720,30,30); }
|
|
tick++; if (!window.stopAnimation) requestAnimationFrame(paint); } paint(); true;`);
|
|
await wait(750);
|
|
frames.length = 0;
|
|
const started = performance.now();
|
|
await wait(3000);
|
|
const elapsed = performance.now() - started;
|
|
const fps = frames.length * 1000 / elapsed;
|
|
const gaps = frames.slice(1).map((frame, index) => frame.time - frames[index].time).sort((a,b) => a-b);
|
|
const result = { configuredFps: wc.getFrameRate(), frames: frames.length, elapsedMs: Math.round(elapsed), fps: Number(fps.toFixed(1)), p95FrameGapMs: Math.round(gaps[Math.floor(gaps.length * .95)]), meanEncodedBytes: Math.round(frames.reduce((n,f) => n+f.bytes,0)/frames.length), viewport: '1280x800', platform: process.platform };
|
|
if (!process.argv.includes('--baseline')) { assert.equal(wc.getFrameRate(), 60); assert.ok(fps >= 24, `Native frame delivery must exceed 24 FPS: ${JSON.stringify(result)}`); }
|
|
await engine.command('input', { tabId, input: { type: 'mouseMove', x: 400, y: 400 } });
|
|
for (let n=0;n<20;n++) { await engine.command('input', { tabId, input: { type: 'mouseWheel', x: 400, y: 400, deltaY: -30, deltaX: 0 } }); await wait(16); }
|
|
await wait(150);
|
|
assert.ok(await wc.executeJavaScript('scrollY') > 100, 'Forwarded wheel input must scroll the native page.');
|
|
await engine.command('visible', { tabId, visible: false });
|
|
await wait(150); frames.length = 0; await wait(300);
|
|
assert.equal(frames.length, 0, 'Hidden tabs must stop sending frames.');
|
|
await engine.command('visible', { tabId, visible: true });
|
|
await wait(200); assert.ok(frames.length > 0, 'Visible tabs must resume frames.');
|
|
writable = false; frames.length = 0; await wait(200);
|
|
assert.equal(frames.length, 0, 'A busy transport must skip frames before encoding.');
|
|
writable = true; await wait(200);
|
|
assert.ok(frames.length > 0, 'Frames resume when the transport can accept them.');
|
|
const { nativeImage } = await import('electron');
|
|
frames.length = 0;
|
|
await engine.command('resize', { tabId, width: 1280, height: 800, scale: 2 });
|
|
await wait(500);
|
|
const highDpi = frames.at(-1);
|
|
const pixels = nativeImage.createFromDataURL(`data:${highDpi.mime};base64,${highDpi.data}`).getSize();
|
|
assert.ok(pixels.width >= 2560 && pixels.width <= 2562 && pixels.height >= 1600 && pixels.height <= 1602,
|
|
`High DPI must render physical pixels, allowing native window rounding: ${JSON.stringify(pixels)}`);
|
|
assert.equal(await wc.executeJavaScript('innerWidth'), 1280, 'High DPI keeps CSS page dimensions.');
|
|
result.highDpiPixels = pixels;
|
|
writable = false;
|
|
await wc.executeJavaScript('window.stopAnimation = true; document.body.style.background = "red"; true;');
|
|
await wait(400); frames.length = 0;
|
|
writable = true; await wait(300);
|
|
assert.ok(frames.some(frame => frame.mime === 'image/png'), 'The final lossless static frame must resume after transport backpressure ends.');
|
|
await mkdir('output', { recursive: true });
|
|
await writeFile(`output/performance-${process.argv.includes('--baseline') ? 'before' : 'after'}.json`, JSON.stringify(result, null, 2));
|
|
console.log(JSON.stringify(result));
|
|
console.log('PASS native scrolling, hidden frame pause, visible frame resume');
|
|
} catch (error) { console.error(error); code = 1; }
|
|
finally { await engine?.shutdown(); app.exit(code); }
|
|
});
|
|
}
|