Files
FFF_vtt_frozen/client/src/render/modules/perf.ts
T
SirStone 9c7f9de910 feat(m4): playtest friction: perf overlay, fresh import, token seed (#82)
Opt-in ?perf=1 overlay (fps, frame p50/p95, JS p50/p95, tokens, zoom; no node or ticker work when off). fff-vtt import creates and migrates a fresh/empty state dir through the shared server startup path; devbox run serve gains --image. devbox run seed creates deterministic labelled NPC tokens over the real WS (idempotent rerun, --replace), and the GM token form defaults X/Y to the viewport centre.
2026-11-21 17:20:05 +01:00

65 lines
2.5 KiB
TypeScript

// The opt-in perf overlay (#82): fps, frame p50/p95, JS/render p50/p95, token count and zoom.
// Mounted only when the URL says `?perf=1` (client/src/perf.ts). Off means no DOM node, no ticker
// listener and no per-frame work.
//
// Timing comes from the real Pixi ticker, not a second rAF loop: a listener at INTERACTION runs
// first in the frame and one at UTILITY runs after the render plugin (registered at LOW), so the
// difference is the frame's main-thread JS + render submission time. `ticker.elapsedMS` is the raw
// frame interval (not speed-scaled), which is what the phone's fps readout needs.
import { UPDATE_PRIORITY } from 'pixi.js'
import type { Ticker } from 'pixi.js'
import type { RenderModule } from '../stage.ts'
import { viewport } from '../../viewport.ts'
import { tokensOf } from '../../slices/tokens.ts'
import { formatSnapshot, PerfWindow, perfEnabled } from '../../perf.ts'
const UPDATE_EVERY_MS = 1000
export const renderModule: RenderModule = {
name: 'perf',
mount({ app, store }) {
if (!perfEnabled(window.location.search)) return
const node = document.createElement('div')
node.id = 'perf'
node.setAttribute('role', 'status')
node.setAttribute('aria-live', 'off')
node.style.cssText =
'position:fixed;top:40px;right:12px;z-index:3;padding:6px 10px;border-radius:6px;' +
'border:1px solid #777;background:rgb(0 0 0 / 80%);color:#eee;font:11px/1.5 ' +
'ui-monospace,monospace;pointer-events:none;white-space:nowrap'
node.textContent = 'perf: warming up…'
const host = document.querySelector('#panels') ?? document.body
host.append(node)
const windowStats = new PerfWindow(10_000)
let frameStart = 0
let started = false
let lastUpdate = 0
const begin = (): void => {
frameStart = performance.now()
}
const end = (ticker: Ticker): void => {
const now = performance.now()
if (started && frameStart > 0) windowStats.add(ticker.elapsedMS, now - frameStart, now)
started = true
if (lastUpdate === 0) lastUpdate = now
if (now - lastUpdate < UPDATE_EVERY_MS) return
lastUpdate = now
const snapshot = windowStats.snapshot(now)
node.textContent = formatSnapshot(snapshot, tokensOf(store.state).length, viewport.state.camera.zoom)
}
app.ticker.add(begin, undefined, UPDATE_PRIORITY.INTERACTION)
app.ticker.add(end, undefined, UPDATE_PRIORITY.UTILITY)
return () => {
app.ticker.remove(begin)
app.ticker.remove(end)
node.remove()
}
},
}