Files
FFF_vtt_frozen/tools/fogbench.nim
T
SirStone fcd90529b6 test(m4): finish audit, measurement and archive (#76)
- client/e2e/audit.test.mjs: independent player-browser traffic audit (own
  frame catalogue and banned-key list, never leakcheck.nim), with a
  -d:leakfault/otherVision negative control.
- client/e2e/fogbench.test.mjs + tools/fogbench.nim: 50 tokens on a
  16384 px map (server move path and headless SwiftShader cadence);
  results/m4-fog-bench.md records both honestly (SW is not the phone gate).
- Archive prototype-* under docs/archive/prototypes (Q24); DESIGN 13 M4
  done, TESTING 13 M4 accept result, HANDOFF-M4 close-out.
- mutation.sh c1/c2 patch the post-#65 tile-gate line again (they had
  silently become sed no-ops).
2026-11-21 15:43:08 +01:00

104 lines
5.2 KiB
Nim

## M4 fog benchmark (#76): the 50-token, 16384 px tile map measurement the finish ticket records
## in `results/m4-fog-bench.md`. `devbox run fogbench` compiles and runs it; the log lands in
## `results/m4-fog-bench.log`.
##
## Two numbers the phone gate needs to make sense of:
## 1. server recompute on a move: one player's vision refresh (one polygon, ~5k sparse walls,
## 16384 px map) plus the egress pass for that player with 50 visible tokens;
## 2. the client's headless frame time is measured separately by `client/e2e/fogbench.test.mjs`
## (SwiftShader, so it is a floor, never the phone number it is compared with).
##
## No timing code lives in server/: this harness times the real modules from outside.
import std/[json, monotimes, times, strformat, math, options, tables, strutils, sets]
import geometry
import characters, props, tokens, views, vision, world
import scenegen
var sink = 0
proc bench(name: string, n: int, f: proc(i: int)): float =
for i in 0 ..< 50: f(i) # warm-up
var sum, worst = 0.0
for i in 0 ..< n:
let t0 = getMonoTime(); f(i)
let ms = (getMonoTime() - t0).inNanoseconds.float / 1e6
sum += ms; worst = max(worst, ms)
echo &"{name}: n={n} avg={sum / n.float:.3f}ms worst={worst:.3f}ms"
sum / n.float
const World = 16384.0
const PlayerId = 2
## 50 tokens: the player's one sighted token at the map centre, 49 NPCs inside its radius. The
## cluster is the phone scene (50 tokens rendered with the fog on), so the wall generator's walls
## inside that box are removed: the sight cost still comes from the ~5k walls elsewhere.
proc build(): ref ContextStore =
var tokens: seq[TokenRec]
tokens.add TokenRec(id: "vision-token", scene: 1, kind: "pc", descriptiveName: "Ann",
actualName: "Ann", pos: (World / 2, World / 2), owner: PlayerId,
flags: %*{"vision": true}, visionRadius: some(300.0), radius: some(20.0),
props: newProps())
for i in 0 ..< 49:
let dx = float((i mod 7) - 3) * 28.0
let dy = float((i div 7) - 3) * 28.0
tokens.add TokenRec(id: &"npc-{i}", scene: 1, kind: "npc", descriptiveName: &"T{i}",
actualName: &"T{i}", pos: (World / 2 + dx, World / 2 + dy),
owner: GmControlled, flags: %*{}, props: newProps())
let centre = World / 2
var walls: seq[WallRec]
var id = 0
for w in genScene(World, 5000, 1):
let near = max(w.a.x, w.b.x) >= centre - 400 and min(w.a.x, w.b.x) <= centre + 400 and
max(w.a.y, w.b.y) >= centre - 400 and min(w.a.y, w.b.y) <= centre + 400
if near: continue
inc id
walls.add WallRec(id: id, a: w.a, b: w.b, thickness: 0, position: wpCenter,
blocksSight: true, blocksMovement: true, sightDir: sdBoth,
playerMarker: false, door: false, open: false, secret: false,
locked: false, height: none(float), label: none(string),
flags: newJObject())
result = (ref ContextStore)(scene: SceneRec(
id: 1, name: "Scene", w: World, h: World, tileSize: 32, cell: 32,
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1, nextWallId: id + 1,
extra: newJObject(), walls: walls), tokens: tokens)
for pid in 2 .. 7:
var pv: PlayerVision
pv.refresh(result.scene, result.tokens, pid)
result.visions[pid] = pv
result.encountered[pid] = initTable[string, characters.EncounteredEntry]()
let store = build()
echo &"== M4 fog bench: 16384 px map, cell 32 ({store.scene.cols}x{store.scene.rows} cells), " &
&"{store.tokens.len} tokens, {store.scene.walls.len} walls =="
# 1. A move refreshes the mover's player: one polygon + its explored cells (main.refreshPlayer).
let refreshAvg = bench("server vision refresh, one player (1 polygon, radius 300, ~5k walls)", 500, proc(i: int) =
store.visions[PlayerId].refresh(store.scene, store.tokens, PlayerId)
sink += store.visions[PlayerId].polys.len)
# 2. The same player's egress pass with all 50 tokens visible (fresh diff base = a reconnect).
let ctx = store.borrow
var firstKnown: Known
let frames = egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, firstKnown)
var kinds: seq[string]
for frame in frames: kinds.add $frame{"t"}
echo &"player egress frames with 50 visible tokens: {frames.len} frames, {kinds.join(\",\")}"
let egressAvg = bench("player egress pass, 50 visible tokens (fresh Known)", 300, proc(i: int) =
var known: Known
sink += egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, known).len)
# 3. The steady state after the connect snapshot: a Known that already holds every frame.
var warmKnown: Known
discard egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, warmKnown)
discard bench("player egress pass, warm Known (no changes)", 2000, proc(i: int) =
sink += egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, warmKnown).len)
echo &"explored snapshot size: {store.visions[PlayerId].explored.card} cells on the 16384 px map"
let budget = 16.7
echo &"TARGET move path: refresh + 50-token egress {refreshAvg + egressAvg:.3f} ms vs <= {budget} ms " &
&"(one 60 fps frame): {(if refreshAvg + egressAvg <= budget: \"PASS\" else: \"MISS\")}"
echo "(sink ", sink, ")"
quit(if refreshAvg + egressAvg <= budget: 0 else: 1)