// T13 (#76): the 50-token, 16384 px map measurement the finish ticket records in // `results/m4-fog-bench.md`. This is NOT the phone gate: headless Chromium here renders with // SwiftShader (software), so its frame cadence is a floor for correctness work, never a substitute // for the Note 10 run (#77). The server half of the same measurement is `tools/fogbench.nim`. // // The scene is the real one, patched to 16384 px (only the importer owns w/h, so the harness writes // the scene row before the server loads it). 50 tokens sit inside the player's sight; the fog // texture is therefore 512x512 cells, the real M4 maximum. import test from 'node:test' import assert from 'node:assert/strict' import { spawnSync } from 'node:child_process' import path from 'node:path' import { createPlayer, namePlayer } from '../../tools/lib.mjs' import { openAs } from '../../tools/withserver.mjs' import { writeTileGrid } from '../../tools/tilefixture.mjs' import { launchBrowser, signIn, waitForVisible, withStack, sleep } from './stack.mjs' const MOBILE = { viewport: { width: 412, height: 869 }, deviceScaleFactor: 2.625, isMobile: true, hasTouch: true } const SIZE = 16384 const CENTRE = [SIZE / 2, SIZE / 2] /** The scene row is the importer's (DESIGN 6.1: w/h are not wire fields); write it before boot. */ function setSceneSize(stateDir, size) { const db = path.join(stateDir, 'state.db') const r = spawnSync('sqlite3', [db, `UPDATE scene SET w=${size}, h=${size} WHERE id=1`], { encoding: 'utf8' }) assert.equal(r.status, 0, `set scene size: ${r.stderr}`) } async function gmWorld(origin, gmKey) { const gm = await openAs(origin, gmKey) const hello = await gm.ws.waitText('hello') const walls = await gm.ws.waitText('walls') return { ...gm, hello, scene: hello.scene ?? walls.scene } } /** 50 tokens: one controlled sighted token and 49 NPCs packed inside its radius. */ async function fiftyTokens(gm, scene, player) { const own = await gm.ws.request({ t: 'token.create', scene: scene.id, kind: 'pc', pos: CENTRE, descriptive_name: 'Ann', actual_name: 'Ann', controller: player.id, flags: { vision: true }, vision_radius: 400, radius: 20, }) assert.equal(own.ok, true, JSON.stringify(own)) for (let i = 0; i < 49; i += 1) { const ack = await gm.ws.request({ t: 'token.create', scene: scene.id, kind: 'npc', pos: [CENTRE[0] + ((i % 7) - 3) * 28, CENTRE[1] + (Math.floor(i / 7) - 3) * 28], descriptive_name: `T${i}`, actual_name: `T${i}`, }) assert.equal(ack.ok, true, JSON.stringify(ack)) } } /** rAF cadence over `count` frames, summarised the way the fog-cost test does. */ async function cadence(page, count) { return page.evaluate(async frames => { const samples = [] await new Promise(resolve => { let last = performance.now() let seen = 0 const step = () => { const now = performance.now() samples.push(now - last) last = now seen += 1 if (seen < frames) requestAnimationFrame(step) else resolve() } requestAnimationFrame(step) }) samples.sort((a, b) => a - b) return { samples: samples.length, median: samples[Math.floor(samples.length / 2)], p95: samples[Math.floor(samples.length * 0.95)], } }, count) } const fogState = page => page.evaluate(() => { const data = document.querySelector('#stage canvas')?.dataset if (!data) return null return { cells: Number(data.fogCells ?? -1), polys: Number(data.fogPolys ?? -1), fullMs: Number(data.fogFullMs ?? -1), tokens: Number(data.tokens ?? -1), } }) test('50-token fog measurement on a 16384 px map (headless SwiftShader, not the phone gate)', { timeout: 180000 }, async () => { await withStack(async ({ origin, gmKey, stateDir }) => { const gm = await gmWorld(origin, gmKey) const scene = gm.scene assert.equal(scene.w, SIZE, 'the prepared scene is 16384 px') const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, 'Ann') await fiftyTokens(gm, scene, { id: player.id }) // Only the viewport's window needs fixture bytes; 16384 px would be 262144 files. writeTileGrid(stateDir, { scene: scene.id, artVersion: scene.art_version, size: scene.tile_size, x0: 240, x1: 280, y0: 235, y1: 280, }) const browser = await launchBrowser() try { const context = await browser.newContext({ ignoreHTTPSErrors: true, ...MOBILE }) const page = await context.newPage() const cdp = await context.newCDPSession(page) const errors = [] page.on('pageerror', error => errors.push(error.message)) page.on('console', message => { if (message.type() === 'error' && !message.text().includes('WebSocket connection') && !message.text().includes('403') && !message.text().includes('net::ERR_')) { errors.push(message.text()) } }) await page.goto(`${origin}/`) await waitForVisible(page, '#signin') await signIn(page, player.key) await waitForVisible(page, '#stage canvas') await page.waitForFunction( () => document.querySelector('#stage canvas')?.dataset.tiles === 'ready', null, { timeout: 30000 }, ) await page.waitForFunction( () => Number(document.querySelector('#stage canvas')?.dataset.fogPolys ?? 0) > 0, null, { timeout: 30000 }, ) const initial = await fogState(page) assert.equal(initial.tokens, 50, `the client draws 50 tokens: ${initial.tokens}`) assert.ok(initial.cells > 400, `a 16384 px map explored cells: ${initial.cells}`) assert.ok(initial.fullMs > 0, `the 512x512 fog snapshot was repainted: ${initial.fullMs}`) const real = await cadence(page, 120) await cdp.send('Emulation.setCPUThrottlingRate', { rate: 4 }) await sleep(200) const throttled = await cadence(page, 90) assert.deepEqual(errors, []) console.log( `fogbench (#76): 16384px map (512x512 cells), 50 tokens, 412x869 dpr2.625, ` + `SwiftShader: fog snapshot=${initial.fullMs.toFixed(3)}ms exploredCells=${initial.cells} ` + `frames real median=${real.median.toFixed(1)}ms p95=${real.p95.toFixed(1)}ms ` + `4xCPU median=${throttled.median.toFixed(1)}ms p95=${throttled.p95.toFixed(1)}ms ` + `(software rasteriser: floor only, not the phone number)`, ) await context.close() } finally { await browser.close() gm.ws.close() } }, { prepare: stateDir => setSceneSize(stateDir, SIZE) }) })