|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +NAME = "build" |
| 11 | +REPO = Path() |
| 12 | +BENCH = REPO / "scripts" / "bench" |
| 13 | +OUTFILE = REPO / "measurements.jsonl" |
| 14 | + |
| 15 | + |
| 16 | +def save_result(metric: str, value: float, unit: str | None = None) -> None: |
| 17 | + data = {"metric": metric, "value": value} |
| 18 | + if unit is not None: |
| 19 | + data["unit"] = unit |
| 20 | + with open(OUTFILE, "a+") as f: |
| 21 | + f.write(f"{json.dumps(data)}\n") |
| 22 | + |
| 23 | + |
| 24 | +def run(*command: str) -> None: |
| 25 | + result = subprocess.run(command) |
| 26 | + if result.returncode != 0: |
| 27 | + sys.exit(result.returncode) |
| 28 | + |
| 29 | + |
| 30 | +def run_stderr(*command: str) -> str: |
| 31 | + result = subprocess.run(command, capture_output=True, encoding="utf-8") |
| 32 | + if result.returncode != 0: |
| 33 | + print(result.stdout, end="", file=sys.stdout) |
| 34 | + print(result.stderr, end="", file=sys.stderr) |
| 35 | + sys.exit(result.returncode) |
| 36 | + return result.stderr |
| 37 | + |
| 38 | + |
| 39 | +def get_module(setup: Path) -> str: |
| 40 | + with open(setup) as f: |
| 41 | + return json.load(f)["name"] |
| 42 | + |
| 43 | + |
| 44 | +def count_lines(module: str, path: Path) -> None: |
| 45 | + with open(path) as f: |
| 46 | + lines = sum(1 for _ in f) |
| 47 | + save_result(f"{NAME}/module/{module}//lines", lines) |
| 48 | + |
| 49 | + |
| 50 | +def run_lean(module: str) -> None: |
| 51 | + stderr = run_stderr( |
| 52 | + f"{BENCH}/measure.py", |
| 53 | + *("-t", f"{NAME}/module/{module}"), |
| 54 | + *("-m", "instructions"), |
| 55 | + "--", |
| 56 | + *("lean", "--profile", "-Dprofiler.threshold=9999999"), |
| 57 | + *sys.argv[1:], |
| 58 | + ) |
| 59 | + |
| 60 | + for line in stderr.splitlines(): |
| 61 | + # Output of `lean --profile` |
| 62 | + # See timeit.cpp for the time format |
| 63 | + if match := re.fullmatch(r"\t(.*) ([\d.]+)(m?s)", line): |
| 64 | + name = match.group(1) |
| 65 | + seconds = float(match.group(2)) |
| 66 | + if match.group(3) == "ms": |
| 67 | + seconds = seconds / 1000 |
| 68 | + save_result(f"{NAME}/profile/{name}//wall-clock", seconds, "s") |
| 69 | + |
| 70 | + |
| 71 | +def main() -> None: |
| 72 | + if sys.argv[1:] == ["--print-prefix"]: |
| 73 | + print(Path(__file__).resolve().parent.parent) |
| 74 | + return |
| 75 | + |
| 76 | + if sys.argv[1:] == ["--githash"]: |
| 77 | + run("lean", "--githash") |
| 78 | + return |
| 79 | + |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + parser.add_argument("lean", type=Path) |
| 82 | + parser.add_argument("--setup", type=Path) |
| 83 | + args, _ = parser.parse_known_args() |
| 84 | + |
| 85 | + lean: Path = args.lean |
| 86 | + setup: Path = args.setup |
| 87 | + |
| 88 | + module = get_module(setup) |
| 89 | + count_lines(module, lean) |
| 90 | + run_lean(module) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments