Fps meter in bun, reading PPM
Browse files at this commit02871e166eeb88c8b9d51a5d0b820ba67d27083cParent: f4ab645- chore
- fps meter in bun, reading PPM
3 changed files +58 -38
- harness/fps.py +0 -37
- harness/fps.ts +57 -0
- harness/share.sh +1 -1
harness/fps.pydeleted+0 -37
| @@ -1,37 +0,0 @@ | |||
| 1 | -import subprocess, sys, time, os | ||
| 2 | -from PIL import Image | ||
| 3 | -state = os.environ.get("HARNESS_STATE", "/tmp/claude-1000/yumush-harness") | ||
| 4 | -env = dict(os.environ) | ||
| 5 | -for line in open(f"{state}/env"): | ||
| 6 | - k, v = line.split()[1].split("=", 1) | ||
| 7 | - env[k] = v | ||
| 8 | -x0, y0, x1, y1, src_w = [int(a) for a in sys.argv[1:6]] | ||
| 9 | -seconds = float(sys.argv[6]) if len(sys.argv) > 6 else 3 | ||
| 10 | -def find_video(im): | ||
| 11 | - for y in range(y0, y1): | ||
| 12 | - if im.getpixel((x0 + (x1 - x0) // 2, y))[0] > 20: | ||
| 13 | - return y | ||
| 14 | -def read(path): | ||
| 15 | - im = Image.open(path).convert("RGB") | ||
| 16 | - top = find_video(im) | ||
| 17 | - if top is None: | ||
| 18 | - raise SystemExit("video area not found in shot") | ||
| 19 | - s = (x1 - x0) / src_w | ||
| 20 | - n = 0 | ||
| 21 | - for i in range(32): | ||
| 22 | - px = im.getpixel((int(x0 + (i * 16 + 8) * s), int(top + 4 * s))) | ||
| 23 | - if px[0] > 128: | ||
| 24 | - n |= 1 << i | ||
| 25 | - return n | ||
| 26 | -samples = [] | ||
| 27 | -end = time.time() + seconds | ||
| 28 | -while time.time() < end: | ||
| 29 | - p = f"{state}/shots/fps.png" | ||
| 30 | - t = time.time() | ||
| 31 | - if os.environ.get("WIN"): | ||
| 32 | - subprocess.run(["./win.sh", "shot", "fps"], check=True, stdout=subprocess.DEVNULL) | ||
| 33 | - else: | ||
| 34 | - subprocess.run(["grim", p], env=env, check=True) | ||
| 35 | - samples.append((t, read(p))) | ||
| 36 | -(t0, n0), (t1, n1) = samples[0], samples[-1] | ||
| 37 | -print(f"frames {n0}->{n1} in {t1-t0:.2f}s = {(n1-n0)/(t1-t0):.1f} fps, {len(samples)} shots") | ||
harness/fps.tsadded+57 -0
| @@ -0,0 +1,57 @@ | |||
| 1 | +import { readFileSync } from "node:fs"; | ||
| 2 | + | ||
| 3 | +const state = process.env.HARNESS_STATE ?? "/tmp/claude-1000/yumush-harness"; | ||
| 4 | +const env: Record<string, string> = { ...process.env } as Record<string, string>; | ||
| 5 | +for (const line of readFileSync(`${state}/env`, "utf8").split("\n")) { | ||
| 6 | + const [key, ...value] = line.replace(/^export /, "").split("="); | ||
| 7 | + if (key && value.length) env[key] = value.join("="); | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +const [x0, y0, x1, y1, sourceWidth] = process.argv.slice(2, 7).map(Number); | ||
| 11 | +const seconds = Number(process.argv[7] ?? 3); | ||
| 12 | +const scale = (x1 - x0) / sourceWidth; | ||
| 13 | + | ||
| 14 | +type Shot = { width: number; data: Buffer }; | ||
| 15 | + | ||
| 16 | +function ppm(buffer: Buffer): Shot { | ||
| 17 | + const header = buffer.subarray(0, 64).toString("latin1").match(/^P6\s+(\d+)\s+(\d+)\s+(\d+)\s/); | ||
| 18 | + if (!header) throw new Error("not a P6 ppm"); | ||
| 19 | + return { width: Number(header[1]), data: buffer.subarray(header[0].length) }; | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +function red({ width, data }: Shot, x: number, y: number): number { | ||
| 23 | + return data[(Math.floor(y) * width + Math.floor(x)) * 3]; | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +function shoot(): Shot { | ||
| 27 | + if (process.env.WIN) { | ||
| 28 | + Bun.spawnSync(["./win.sh", "shot", "fps"], { stdout: "ignore" }); | ||
| 29 | + return ppm(readFileSync(`${state}/shots/win.ppm`)); | ||
| 30 | + } | ||
| 31 | + const grim = Bun.spawnSync(["grim", "-t", "ppm", "-"], { env }); | ||
| 32 | + if (grim.exitCode !== 0) throw new Error(grim.stderr.toString()); | ||
| 33 | + return ppm(grim.stdout); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +function frame(shot: Shot): number { | ||
| 37 | + const middle = x0 + (x1 - x0) / 2; | ||
| 38 | + let top = y0; | ||
| 39 | + while (top < y1 && red(shot, middle, top) <= 20) top++; | ||
| 40 | + if (top === y1) throw new Error("video area not found in shot"); | ||
| 41 | + let number = 0; | ||
| 42 | + for (let bit = 0; bit < 32; bit++) { | ||
| 43 | + if (red(shot, x0 + (bit * 16 + 8) * scale, top + 4 * scale) > 128) number += 2 ** bit; | ||
| 44 | + } | ||
| 45 | + return number; | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +const samples: [number, number][] = []; | ||
| 49 | +const end = performance.now() + seconds * 1000; | ||
| 50 | +while (performance.now() < end) { | ||
| 51 | + const time = performance.now(); | ||
| 52 | + samples.push([time, frame(shoot())]); | ||
| 53 | +} | ||
| 54 | +const [t0, n0] = samples[0]; | ||
| 55 | +const [t1, n1] = samples[samples.length - 1]; | ||
| 56 | +const elapsed = (t1 - t0) / 1000; | ||
| 57 | +console.log(`frames ${n0}->${n1} in ${elapsed.toFixed(2)}s = ${((n1 - n0) / elapsed).toFixed(1)} fps, ${samples.length} shots`); | ||
harness/share.shmodified+1 -1
| @@ -7,4 +7,4 @@ cd "$HERE" | |||
| 7 | ./ui.sh click 144 980; sleep 3 | 7 | ./ui.sh click 144 980; sleep 3 |
| 8 | ./ui.sh click 1740 28; sleep "${SETTLE:-6}" | 8 | ./ui.sh click 1740 28; sleep "${SETTLE:-6}" |
| 9 | ./shot.sh watch | 9 | ./shot.sh watch |
| 10 | -python3 fps.py 1193 54 1911 1015 "${SRC_W:-1280}" "${SECONDS_MEASURE:-5}" | 10 | +bun fps.ts 1193 54 1911 1015 "${SRC_W:-1280}" "${SECONDS_MEASURE:-5}" |