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>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
## 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)
|
||||
@@ -0,0 +1,30 @@
|
||||
## Synthetic wall scenes for the wall-count scaling bench (DESIGN 5.3 / 13 M2, H3).
|
||||
## Layout: a square world of `world` px split into G x G rooms, G = ceil(sqrt(n / 2)); every room owns its EAST and SOUTH
|
||||
## edge (2 candidate walls per room, west/north belong to the neighbour), so an n = 5000 scene is exactly 50 x 50 rooms.
|
||||
## Each edge becomes ONE wall segment covering 55..95 % of the edge at a random offset (the rest is a doorway gap, so vision
|
||||
## really passes between rooms); endpoints are jittered by up to +-6 px (no axis-aligned degeneracy); every 10th wall is a
|
||||
## closed door. Generation stops after exactly n walls (row-major). Fully determined by (world, n, seed): xorshift64.
|
||||
## Density: walls per vision bbox (800 x 800, radius 400) ~= 2 * (800 / pitch)^2: pitch 327.68 (world 16384) -> ~12,
|
||||
## pitch 81.92 (world 4096, "dense") -> ~190.
|
||||
import std/math
|
||||
import geometry
|
||||
import ../tests/testutil # Rng
|
||||
|
||||
proc genScene*(world: float, n: int, seed: uint64): seq[Wall] =
|
||||
var r = Rng(s: seed)
|
||||
let g = int(ceil(sqrt(n.float / 2)))
|
||||
let pitch = world / g.float
|
||||
proc jit(r: var Rng): float = r.uni(-6, 6)
|
||||
for gy in 0 ..< g:
|
||||
for gx in 0 ..< g:
|
||||
for edge in 0 .. 1: # 0 = east edge (vertical), 1 = south edge (horizontal)
|
||||
if result.len >= n: return
|
||||
let frac = r.uni(0.55, 0.95)
|
||||
let off = r.uni(0, 1 - frac)
|
||||
let (x0, y0) = (gx.float * pitch, gy.float * pitch)
|
||||
var a, b: Vec
|
||||
if edge == 0:
|
||||
a = (x0 + pitch + r.jit, y0 + off * pitch + r.jit); b = (x0 + pitch + r.jit, y0 + (off + frac) * pitch + r.jit)
|
||||
else:
|
||||
a = (x0 + off * pitch + r.jit, y0 + pitch + r.jit); b = (x0 + (off + frac) * pitch + r.jit, y0 + pitch + r.jit)
|
||||
result.add Wall(a: a, b: b, door: result.len mod 10 == 9, open: false)
|
||||
Reference in New Issue
Block a user