From 02871e166eeb88c8b9d51a5d0b820ba67d27083c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 05:59:56 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore(harness):=20fps=20meter=20?= =?UTF-8?q?in=20bun,=20reading=20PPM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fps.ts replaces fps.py: grim -t ppm to stdout (or the Windows VM's screendump win.ppm) is parsed directly, so Pillow and the PNG encode/decode round trip are gone. Same result on a live 30 fps share (29.7 fps) with 4x the samples (404 vs 104 in 5 s). --- harness/fps.py | 37 ------------------------------- harness/fps.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ harness/share.sh | 2 +- 3 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 harness/fps.py create mode 100644 harness/fps.ts diff --git a/harness/fps.py b/harness/fps.py deleted file mode 100644 index 20129bd..0000000 --- a/harness/fps.py +++ /dev/null @@ -1,37 +0,0 @@ -import subprocess, sys, time, os -from PIL import Image -state = os.environ.get("HARNESS_STATE", "/tmp/claude-1000/yumush-harness") -env = dict(os.environ) -for line in open(f"{state}/env"): - k, v = line.split()[1].split("=", 1) - env[k] = v -x0, y0, x1, y1, src_w = [int(a) for a in sys.argv[1:6]] -seconds = float(sys.argv[6]) if len(sys.argv) > 6 else 3 -def find_video(im): - for y in range(y0, y1): - if im.getpixel((x0 + (x1 - x0) // 2, y))[0] > 20: - return y -def read(path): - im = Image.open(path).convert("RGB") - top = find_video(im) - if top is None: - raise SystemExit("video area not found in shot") - s = (x1 - x0) / src_w - n = 0 - for i in range(32): - px = im.getpixel((int(x0 + (i * 16 + 8) * s), int(top + 4 * s))) - if px[0] > 128: - n |= 1 << i - return n -samples = [] -end = time.time() + seconds -while time.time() < end: - p = f"{state}/shots/fps.png" - t = time.time() - if os.environ.get("WIN"): - subprocess.run(["./win.sh", "shot", "fps"], check=True, stdout=subprocess.DEVNULL) - else: - subprocess.run(["grim", p], env=env, check=True) - samples.append((t, read(p))) -(t0, n0), (t1, n1) = samples[0], samples[-1] -print(f"frames {n0}->{n1} in {t1-t0:.2f}s = {(n1-n0)/(t1-t0):.1f} fps, {len(samples)} shots") diff --git a/harness/fps.ts b/harness/fps.ts new file mode 100644 index 0000000..fbe6809 --- /dev/null +++ b/harness/fps.ts @@ -0,0 +1,57 @@ +import { readFileSync } from "node:fs"; + +const state = process.env.HARNESS_STATE ?? "/tmp/claude-1000/yumush-harness"; +const env: Record = { ...process.env } as Record; +for (const line of readFileSync(`${state}/env`, "utf8").split("\n")) { + const [key, ...value] = line.replace(/^export /, "").split("="); + if (key && value.length) env[key] = value.join("="); +} + +const [x0, y0, x1, y1, sourceWidth] = process.argv.slice(2, 7).map(Number); +const seconds = Number(process.argv[7] ?? 3); +const scale = (x1 - x0) / sourceWidth; + +type Shot = { width: number; data: Buffer }; + +function ppm(buffer: Buffer): Shot { + const header = buffer.subarray(0, 64).toString("latin1").match(/^P6\s+(\d+)\s+(\d+)\s+(\d+)\s/); + if (!header) throw new Error("not a P6 ppm"); + return { width: Number(header[1]), data: buffer.subarray(header[0].length) }; +} + +function red({ width, data }: Shot, x: number, y: number): number { + return data[(Math.floor(y) * width + Math.floor(x)) * 3]; +} + +function shoot(): Shot { + if (process.env.WIN) { + Bun.spawnSync(["./win.sh", "shot", "fps"], { stdout: "ignore" }); + return ppm(readFileSync(`${state}/shots/win.ppm`)); + } + const grim = Bun.spawnSync(["grim", "-t", "ppm", "-"], { env }); + if (grim.exitCode !== 0) throw new Error(grim.stderr.toString()); + return ppm(grim.stdout); +} + +function frame(shot: Shot): number { + const middle = x0 + (x1 - x0) / 2; + let top = y0; + while (top < y1 && red(shot, middle, top) <= 20) top++; + if (top === y1) throw new Error("video area not found in shot"); + let number = 0; + for (let bit = 0; bit < 32; bit++) { + if (red(shot, x0 + (bit * 16 + 8) * scale, top + 4 * scale) > 128) number += 2 ** bit; + } + return number; +} + +const samples: [number, number][] = []; +const end = performance.now() + seconds * 1000; +while (performance.now() < end) { + const time = performance.now(); + samples.push([time, frame(shoot())]); +} +const [t0, n0] = samples[0]; +const [t1, n1] = samples[samples.length - 1]; +const elapsed = (t1 - t0) / 1000; +console.log(`frames ${n0}->${n1} in ${elapsed.toFixed(2)}s = ${((n1 - n0) / elapsed).toFixed(1)} fps, ${samples.length} shots`); diff --git a/harness/share.sh b/harness/share.sh index f844e0e..751cb4e 100755 --- a/harness/share.sh +++ b/harness/share.sh @@ -7,4 +7,4 @@ cd "$HERE" ./ui.sh click 144 980; sleep 3 ./ui.sh click 1740 28; sleep "${SETTLE:-6}" ./shot.sh watch -python3 fps.py 1193 54 1911 1015 "${SRC_W:-1280}" "${SECONDS_MEASURE:-5}" +bun fps.ts 1193 54 1911 1015 "${SRC_W:-1280}" "${SECONDS_MEASURE:-5}"