Files
FFF_vtt_frozen/tools/bench.nim
T
SirStoneandClaude Sonnet 5.5 e0705a8533 bench: harness with synthetic 5k-wall scenes (devbox run bench), scenegen + tests
Targets met without a spatial grid: polygon+cells 0.048 ms (<= 0.451) on the prototype scene, polygon 0.083 ms
(< 1 ms) on 5k sparse. 5k dense stress layout reported (1.28 ms). Logs: results/bench.log, nimble-test.log.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 12:15:31 +01:00

77 lines
4.0 KiB
Nim

## Vision microbench: devbox run bench (nim c -d:release -r tools/bench.nim; log: results/bench.log).
## No timing code lives in server/: this harness times from outside with O(1) accumulators (sum, worst), no sample arrays.
## Targets (DESIGN 5.3 / 13 M2, PROPOSED): polygon + cells <= 0.451 ms avg on the prototype scene; polygon < 1 ms avg on 5k walls.
import std/[json, monotimes, times, strformat, os, math]
import geometry, moverules
import scenegen
import ../tests/[refold, testutil]
let root = currentSourcePath().parentDir.parentDir
let j = parseFile(root / "tests" / "fixtures" / "scene.json")
var proto: seq[Wall]
for w in j["walls"]:
proto.add Wall(a: (w["a"][0].getFloat, w["a"][1].getFloat), b: (w["b"][0].getFloat, w["b"][1].getFloat),
door: w["door"].getBool, open: w["open"].getBool)
let radius = j["visionRadius"].getFloat
let cell = j["exploredCell"].getFloat
var toks: seq[Vec]
for t in j["tokens"]: toks.add (t["pos"][0].getFloat, t["pos"][1].getFloat)
var sink = 0
var misses = 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)
result = sum / n.float
echo &"{name}: n={n} avg={result:.3f}ms worst={worst:.3f}ms"
proc target(name: string, got, limit: float) =
let ok = got <= limit
if not ok: inc misses
echo &"TARGET {name}: {got:.3f} ms vs <= {limit} ms: {(if ok: \"PASS\" else: \"MISS\")}"
echo "== prototype scene (60 walls, 8 tokens, radius 400, cell 32) =="
discard bench("1 poly (jittered start positions)", 2000, proc(i: int) =
let p = toks[i mod 8]; sink += visibilityPolygon((p.x + float(i mod 50), p.y), proto, radius).len)
discard bench("6 polys (one 6-token party)", 500, proc(i: int) =
for k in 0 ..< 6:
let p = toks[k + 2]; sink += visibilityPolygon((p.x + float(i mod 50), p.y), proto, radius).len)
let pc = bench("1 poly + cellsTouched (scanline)", 2000, proc(i: int) =
let p = toks[i mod 8]; sink += cellsTouched(visibilityPolygon((p.x + float(i mod 50), p.y), proto, radius), cell).len)
discard bench("6 polys + cellsTouched (scanline)", 300, proc(i: int) =
for k in 0 ..< 6:
let p = toks[k + 2]; sink += cellsTouched(visibilityPolygon((p.x + float(i mod 50), p.y), proto, radius), cell).len)
discard bench("1 poly + cellsTouchedOld (prototype reference)", 500, proc(i: int) =
let p = toks[i mod 8]; sink += cellsTouchedOld(visibilityPolygon((p.x + float(i mod 50), p.y), proto, radius), cell).len)
target("polygon+cells, prototype scene (prototype baseline 0.451)", pc, 0.451)
proc big(label: string, world: float) =
let walls = genScene(world, 5000, 1)
var inBox, nn = 0
var r = Rng(s: 7)
for k in 0 ..< 200: # density: walls whose bbox meets the 800x800 vision box
let o: Vec = (r.uni(400, world - 400), r.uni(400, world - 400))
for w in walls:
if not (max(w.a.x, w.b.x) < o.x - radius or min(w.a.x, w.b.x) > o.x + radius or
max(w.a.y, w.b.y) < o.y - radius or min(w.a.y, w.b.y) > o.y + radius): inc inBox
inc nn
echo &"== {label}: genScene(world={world:.0f}, n=5000, seed=1), avg walls in vision bbox {inBox.float / nn.float:.1f} =="
var rng = Rng(s: 99)
let po = bench(&"1 poly, {label}", 2000, proc(i: int) =
let o: Vec = (rng.uni(400, world - 400), rng.uni(400, world - 400))
sink += visibilityPolygon(o, walls, radius).len)
discard bench(&"1 poly + cellsTouched, {label}", 2000, proc(i: int) =
let o: Vec = (rng.uni(400, world - 400), rng.uni(400, world - 400))
sink += cellsTouched(visibilityPolygon(o, walls, radius), cell).len)
discard bench(&"1 move check (pathClear, cap-length step), {label}", 20000, proc(i: int) =
let o: Vec = (rng.uni(400, world - 400), rng.uni(400, world - 400))
sink += int(pathClear(walls, o, (o.x + 300, o.y), 20)))
if label == "5k sparse": target("polygon, 5k-wall scene", po, 1.0)
big("5k sparse", 16384)
big("5k dense", 4096)
echo "(sink ", sink, ")"
quit(if misses == 0: 0 else: 1)