#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """A/B two builds of sh2-bench on this shared machine: alternate them, workload by workload, on one pinned CPU, and compare their best runs. ab.py BIN_A BIN_B [--rounds N] [--steps N] [--cpu N] Each round runs A then B (B then A the next round) on each of sh2-bench's four workloads (the reference's harness_speed loop, cache off and on, on the minimal harness and on the 32X's bus). The result is B's best speed over A's best, per workload and overall (the geometric mean). The best, not the median: on a shared machine other work only ever slows a run down (gameboy-rust-fast's ab.py, the same method). The SH-2 clocks the builds report must agree, or they do not compute the same thing and the comparison is refused. An A/A run shows the noise. """ from __future__ import annotations import argparse import os import re import statistics import subprocess import sys WORKLOADS = [[], ["--cache"], ["--32x"], ["--32x", "--cache"]] NAMES = ["off", "on", "32x-off", "32x-on"] LINE = re.compile(r"best (\d+) instructions/s wall") STATES = re.compile(r"([\d.]+) SH-2 states per instruction") def bench(binary: str, flags: list[str], steps: int, cpu: int, env: dict) -> tuple[float, str]: out = subprocess.run( ["taskset", "-c", str(cpu), binary, "--steps", str(steps), "--runs", "1", *flags], capture_output=True, text=True, check=True, env=env, ).stdout # fmt: skip return float(LINE.search(out).group(1)), STATES.search(out).group(1) 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=6) parser.add_argument("--steps", type=int, default=10_000_000) parser.add_argument("--cpu", type=int, default=30) parser.add_argument("--env-b", action="append", default=[], help="NAME=VALUE for B only") args = parser.parse_args() env_a = dict(os.environ) env_b = dict(os.environ) for item in args.env_b: name, value = item.split("=", 1) env_b[name] = value ratios = [] for name, flags in zip(NAMES, WORKLOADS): a_runs, b_runs = [], [] for r in range(args.rounds): order = [("a", args.a, env_a), ("b", args.b, env_b)] if r % 2: order.reverse() for side, binary, env in order: (a_runs if side == "a" else b_runs).append(bench(binary, flags, args.steps, args.cpu, env)) if len({s for _, s in a_runs + b_runs}) != 1: print(f"{name}: the builds' states per instruction differ: not equivalent") return 1 a, b = max(s for s, _ in a_runs), max(s for s, _ in b_runs) ratios.append(b / a) print(f"{name}: A {a / 1e6:.2f} M/s, B {b / 1e6:.2f} M/s, x{b / a:.3f}", flush=True) geo = statistics.geometric_mean(ratios) print(f"overall x{geo:.3f} (geometric mean of {len(ratios)} workloads, {args.rounds} rounds each)") return 0 if __name__ == "__main__": sys.exit(main())