Files
FFF_vtt_frozen/tools/fogbench.nim
T
SirStone a79d809394 feat(m4): cap map images at Full HD and shrink on import (#85)
The owner fixed the map cap at 1920 px on the long side and 1080 px on the
short side (either orientation). fff-vtt import now shrinks a larger source
with ImageMagick Lanczos before tiling (alpha kept, proportions preserved,
no upscaling), prints `scaled WxH -> WxH`, and records the shrunk w/h; a
source at or below the cap is untouched, so the pixel-identity tests still
hold. A scene row already above the cap is not rewritten: the server logs
`map_above_cap` at startup and serve prints the re-import advice.

Docs: ADR-0023, DESIGN 3.3/5.3/8/11.1/13/14, TESTING 5.3/13, README,
HANDOFF-M4, client README. The M4 phone gate and the fogbench measurement
now use the Full HD maximum; the 16384 px / 262144-cell bench and test rows
stay as labelled server stress references. import-scale now proves a 16384²
input is shrunk to 1080x1080 (289 tiles at 64 px) quickly and bounded.

Part of #63, #85. #77 is blocked by this issue.
2026-11-21 22:37:34 +01:00

109 lines
5.6 KiB
Nim

## M4 fog benchmark (#76): the 50-token, Full HD (1920x1080) 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`. The Full HD cap is the owner decision of 2026-11-21 (#85), so this is
## the product maximum, not the old 16384 px stress map.
##
## Two numbers the phone gate needs to make sense of:
## 1. server recompute on a move: one player's vision refresh (one polygon, 5k-wall generator
## clipped to the Full HD scene) 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
WorldW = 1920.0
WorldH = 1080.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 5k-wall generator runs on a WorldW square and is clipped to the
## Full HD height, so the sight cost still comes from thousands of 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: (WorldW / 2, WorldH / 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: (WorldW / 2 + dx, WorldH / 2 + dy),
owner: GmControlled, flags: %*{}, props: newProps())
let centre = (WorldW / 2, WorldH / 2)
var walls: seq[WallRec]
var id = 0
for w in genScene(WorldW, 5000, 1):
if w.a.y > WorldH or w.b.y > WorldH: continue # clipped away by the Full HD scene height
let near = max(w.a.x, w.b.x) >= centre[0] - 400 and min(w.a.x, w.b.x) <= centre[0] + 400 and
max(w.a.y, w.b.y) >= centre[1] - 400 and min(w.a.y, w.b.y) <= centre[1] + 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: WorldW, h: WorldH, 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: Full HD map 1920x1080, 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-wall generator clipped to the scene)", 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 Full HD 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)