|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Simple performance gate for the perf_smoke benchmark.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +def parse_args(argv: List[str]) -> argparse.Namespace: |
| 14 | + parser = argparse.ArgumentParser(description=__doc__) |
| 15 | + parser.add_argument( |
| 16 | + "--baseline", |
| 17 | + required=True, |
| 18 | + type=pathlib.Path, |
| 19 | + help="Path to the baseline file containing the expected microseconds per evaluation.", |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "--threshold", |
| 23 | + type=float, |
| 24 | + default=0.20, |
| 25 | + help="Maximum allowed relative regression (e.g. 0.15 for 15%%).", |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + "command", |
| 29 | + nargs=argparse.REMAINDER, |
| 30 | + help="Command to run (prefix with -- to terminate the argument parser).", |
| 31 | + ) |
| 32 | + args = parser.parse_args(argv) |
| 33 | + if not args.command: |
| 34 | + parser.error("a benchmark command must be provided after --") |
| 35 | + if args.command[0] == "--": |
| 36 | + args.command = args.command[1:] |
| 37 | + if not args.command: |
| 38 | + parser.error("a benchmark command must be provided after --") |
| 39 | + return args |
| 40 | + |
| 41 | + |
| 42 | +def read_baseline(path: pathlib.Path) -> float: |
| 43 | + try: |
| 44 | + contents = path.read_text(encoding="utf-8").strip() |
| 45 | + except OSError as exc: |
| 46 | + raise SystemExit(f"failed to read baseline '{path}': {exc}") from exc |
| 47 | + try: |
| 48 | + return float(contents) |
| 49 | + except ValueError as exc: |
| 50 | + raise SystemExit(f"baseline '{path}' does not contain a valid float") from exc |
| 51 | + |
| 52 | + |
| 53 | +def extract_metric(stdout: str) -> float: |
| 54 | + for line in stdout.splitlines(): |
| 55 | + if line.startswith("PERF_SMOKE_PER_EVAL_US="): |
| 56 | + try: |
| 57 | + return float(line.split("=", 1)[1]) |
| 58 | + except ValueError as exc: |
| 59 | + raise SystemExit("failed to parse benchmark output") from exc |
| 60 | + raise SystemExit("benchmark output did not contain PERF_SMOKE_PER_EVAL_US") |
| 61 | + |
| 62 | + |
| 63 | +def main(argv: List[str]) -> int: |
| 64 | + args = parse_args(argv) |
| 65 | + baseline = read_baseline(args.baseline) |
| 66 | + |
| 67 | + result = subprocess.run(args.command, capture_output=True, text=True) |
| 68 | + if result.stdout: |
| 69 | + print(result.stdout, end="") |
| 70 | + if result.stderr: |
| 71 | + print(result.stderr, end="", file=sys.stderr) |
| 72 | + if result.returncode != 0: |
| 73 | + return result.returncode |
| 74 | + |
| 75 | + measured = extract_metric(result.stdout) |
| 76 | + if baseline <= 0: |
| 77 | + raise SystemExit("baseline value must be positive") |
| 78 | + |
| 79 | + regression = (measured - baseline) / baseline |
| 80 | + print(f"Recorded perf_smoke: {measured:.3f} us (baseline {baseline:.3f} us)") |
| 81 | + print(f"Relative change: {regression * 100.0:.2f}% (threshold {args.threshold * 100.0:.2f}%)") |
| 82 | + |
| 83 | + if regression > args.threshold: |
| 84 | + print( |
| 85 | + f"Regression of {regression * 100.0:.2f}% exceeds allowed threshold", |
| 86 | + file=sys.stderr, |
| 87 | + ) |
| 88 | + return 1 |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + sys.exit(main(sys.argv[1:])) |
0 commit comments