Files
KoalaSync/tests/e2e/bench-finder.mjs
T
KoalaDev ef1fdc89c3 test: prove the frame lifecycle fixes against the real extension
The evidence for the frame-observation fixes was a reimplementation of the
logic measured in a browser console, not the shipped code. Two extension
specs now cover the observable behaviour end to end: a player frame that
swaps its document, and the same one level deeper.

The nested case fails against the pre-fix content.js and passes now. The
top-level case already passed before the fix, so that fix removed dead
observer registrations without changing what a user could see; recorded
here so the distinction is not lost.

Also adds bench-finder.mjs, which measures the extracted shipped finder
instead of a transcription of it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-14 07:53:45 +02:00

77 lines
3.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Measures findVideo() on a page loaded with same-origin frames.
*
* Not a spec: timings are machine dependent and would only add noise to CI.
* Run it by hand when the finder changes:
*
* node tests/e2e/fixture-server.mjs 4173 &
* node tests/e2e/bench-finder.mjs
*
* The measured implementation is the shipped one, lifted out of content.js by
* the same helper the specs use. The pre-v3.1.0 formula it is compared against
* is transcribed here, since that code no longer exists in the tree.
*/
import { chromium } from '@playwright/test';
import { buildVideoFinderScript } from './helpers/content-source.mjs';
const url = process.env.KOALA_E2E_URL || 'http://localhost:4173/pages/ad-frame.html';
const FRAMES = Number(process.env.KOALA_BENCH_FRAMES || 40);
const CALLS = Number(process.env.KOALA_BENCH_CALLS || 200);
const browser = await chromium.launch({ args: ['--autoplay-policy=no-user-gesture-required'] });
const page = await browser.newPage();
await page.goto(url);
await page.waitForFunction(() => window.__fixtureReady === true);
await page.addScriptTag({ content: buildVideoFinderScript() });
const result = await page.evaluate(({ frames, calls }) => {
// Pre-v3.1.0 scoring, kept only as the baseline for this measurement.
function findVideoLegacy(root = document) {
const candidates = window.__koalaCollect(root);
if (!candidates.length) return null;
if (candidates.length === 1) return candidates[0];
let best = null;
let bestScore = -1;
for (const v of candidates) {
if (v.tagName !== 'VIDEO') continue;
const area = (v.videoWidth || v.offsetWidth || 0) * (v.videoHeight || v.offsetHeight || 0);
const score = area + (v.muted ? 0 : 100000)
+ (v.duration && isFinite(v.duration) ? v.duration : 0) * 100;
if (score > bestScore) { bestScore = score; best = v; }
}
return best;
}
window.__koalaCollect = (root) => window.collectVideoCandidates(root, 0, []);
const holder = document.createElement('div');
document.body.appendChild(holder);
for (let i = 0; i < frames; i++) {
const frame = document.createElement('iframe');
frame.style.display = 'none';
holder.appendChild(frame);
}
// globalThis keeps this readable to a Node-configured linter: the body runs
// in the page, where performance is a global.
const clock = globalThis.performance;
const bench = (fn) => {
for (let i = 0; i < 20; i++) fn();
const start = clock.now();
for (let i = 0; i < calls; i++) fn();
return (clock.now() - start) / calls;
};
const current = bench(() => window.__koalaFindVideo());
const legacy = bench(() => findVideoLegacy());
const frameCount = document.querySelectorAll('iframe, frame').length;
holder.remove();
return { frameCount, current, legacy };
}, { frames: FRAMES, calls: CALLS });
console.log(`frames on page: ${result.frameCount}`);
console.log(`shipped findVideo: ${result.current.toFixed(3)} ms/call`);
console.log(`pre-v3.1.0 formula: ${result.legacy.toFixed(3)} ms/call`);
await browser.close();