#!/usr/bin/env python3 """A/B two builds of gameboy-bench on this shared machine: alternate them, game by game, on one pinned CPU, and compare their best runs. ab.py BIN_A BIN_B [--rounds N] [--slices N] [--cpu N] Each round runs A then B (then B then A the next round) on each game; the result is B's best speed over A's best, per game and overall (the geometric mean). The best, not the median: on a shared machine other work only ever slows a run down, so each side's fastest of many short runs is the closest to its own speed (a2600-rust-fast's method). The checksums must agree, or the builds do not compute the same thing and the comparison is refused. An A/A run shows the noise. Games come from $NO_INTRO_DIR (the gate's commercial cases, DMG and CGB). """ from __future__ import annotations import argparse import os import re import statistics import subprocess import sys NOINTRO = os.environ.get("NO_INTRO_DIR", "No-Intro") GB = f"{NOINTRO}/Nintendo - Game Boy" GBC = f"{NOINTRO}/Nintendo - Game Boy Color" GAMES = [ f"dmg:{GB}/Tetris (World) (Rev 1).zip", f"dmg:{GB}/Super Mario Land (World) (Rev 1).zip", f"dmg:{GB}/Legend of Zelda, The - Link's Awakening (USA, Europe) (Rev 2).zip", f"cgb:{GBC}/Pokemon - Crystal Version (USA, Europe) (Rev 1).zip", f"cgb:{GBC}/Wario Land 3 (World) (En,Ja).zip", f"cgb:{GBC}/Super Mario Bros. Deluxe (USA, Europe) (Rev 1).zip", ] LINE = re.compile(r"([\d.]+) slices/s, check ([0-9A-F]{8})") def bench(binary: str, game: str, slices: int, cpu: int) -> tuple[float, str]: out = subprocess.run( ["taskset", "-c", str(cpu), binary, game, "--slices", str(slices)], capture_output=True, text=True, check=True, ).stdout speed, check = LINE.search(out).groups() return float(speed), check def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("a") parser.add_argument("b") parser.add_argument("--rounds", type=int, default=10) parser.add_argument("--slices", type=int, default=400) parser.add_argument("--cpu", type=int, default=31) args = parser.parse_args() ratios = [] for game in GAMES: a_runs, b_runs = [], [] for r in range(args.rounds): order = [("a", args.a), ("b", args.b)] if r % 2: order.reverse() for side, binary in order: result = bench(binary, game, args.slices, args.cpu) (a_runs if side == "a" else b_runs).append(result) checks = {c for _, c in a_runs} | {c for _, c in b_runs} name = game.split(":", 1)[0] + ":" + game.rsplit("/", 1)[-1].removesuffix(".zip") if len(checks) != 1: print(f"{name}: checksums differ {sorted(checks)}: the builds are not equivalent") return 1 a = max(s for s, _ in a_runs) b = max(s for s, _ in b_runs) ratios.append(b / a) print(f"{name}: A {a:.1f} slices/s, B {b:.1f} slices/s, x{b / a:.3f}", flush=True) geo = statistics.geometric_mean(ratios) print(f"overall x{geo:.3f} (geometric mean of {len(ratios)} games, {args.rounds} rounds each)") return 0 if __name__ == "__main__": sys.exit(main())