from __future__ import annotations
import argparse
import json
import re
import shutil
import subprocess
import sys
import tempfile
from dataclasses import dataclass
from fractions import Fraction
from pathlib import Path
HARNESS = Path("scripts") / "icount" / "scenarios.rs"
HARNESS_TARGET = "cheetah-icount"
IREFS = re.compile(r"I\s+refs:\s+([\d,]+)")
MANIFEST = """\
[package]
name = "cheetah-icount"
version = "0.0.0"
edition = "2021"
publish = false
[dependencies]
cheetah-string = {{ path = "{crate}" }}
[profile.release]
codegen-units = 1
debug = false
[workspace]
"""
class HarnessIncompatible(Exception):
@dataclass(frozen=True)
class Row:
scenario: str
base: Fraction | None
head: Fraction
regressed: bool
@property
def change(self) -> float | None:
if self.base is None or self.base == 0:
return None
return float(self.head / self.base - 1)
def exact(value: Fraction | float | int) -> Fraction:
return value if isinstance(value, Fraction) else Fraction(str(value))
def parse_irefs(stderr: str) -> int:
match = IREFS.search(stderr)
if match is None:
raise ValueError("cachegrind output has no 'I refs' line:\n" + stderr)
return int(match.group(1).replace(",", ""))
def per_iteration(low_count: int, high_count: int, low: int, high: int) -> Fraction:
if high <= low:
raise ValueError("--high must exceed --low")
if high_count < low_count:
raise ValueError(f"instruction count fell from {low_count} to {high_count}")
return Fraction(high_count - low_count, high - low)
def compare(
base: dict[str, Fraction | float] | None,
head: dict[str, Fraction | float],
tolerance: float,
min_delta: float,
) -> list[Row]:
tolerance, min_delta = exact(tolerance), exact(min_delta)
rows = []
for scenario, head_value in head.items():
head_count = exact(head_value)
base_value = None if base is None else base.get(scenario)
base_count = None if base_value is None else exact(base_value)
regressed = (
base_count is not None
and head_count - base_count > min_delta
and head_count > base_count * (1 + tolerance)
)
rows.append(Row(scenario, base_count, head_count, regressed))
return rows
def render(rows: list[Row], tolerance: float, min_delta: float, note: str) -> str:
lines = [
"## Instruction counts per iteration",
"",
"Valgrind cachegrind, instructions executed by one iteration of each "
"scenario in `scripts/icount/scenarios.rs`. A scenario regresses when "
f"head runs more than {tolerance:.0%} and more than {min_delta:g} "
"instructions above base.",
]
if note:
lines += ["", note]
lines += ["", "| Scenario | Base | Head | Change |", "| --- | ---: | ---: | ---: |"]
for row in rows:
base = "-" if row.base is None else f"{float(row.base):.1f}"
change = "-" if row.change is None else f"{row.change:+.1%}"
if row.regressed:
change = f"**{change} regression**"
lines.append(f"| {row.scenario} | {base} | {float(row.head):.1f} | {change} |")
regressions = [row.scenario for row in rows if row.regressed]
lines.append("")
if regressions:
lines.append(f"**{len(regressions)} regression(s):** " + ", ".join(regressions))
elif rows and all(row.base is None for row in rows):
lines.append("**No comparison:** base was not measured, so regressions were not checked.")
else:
lines.append("No regressions.")
return "\n".join(lines) + "\n"
def cargo_messages(cargo_json: str, reason: str) -> list[dict]:
messages = []
for line in cargo_json.splitlines():
try:
message = json.loads(line)
except json.JSONDecodeError:
continue
if isinstance(message, dict) and message.get("reason") == reason:
messages.append(message)
return messages
def rendered_diagnostics(cargo_json: str) -> list[str]:
return [
message.get("message", {}).get("rendered") or ""
for message in cargo_messages(cargo_json, "compiler-message")
]
def run_checked(command: list[str]) -> subprocess.CompletedProcess[str]:
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode != 0:
sys.stderr.write(result.stderr)
raise subprocess.CalledProcessError(result.returncode, command, result.stdout, result.stderr)
return result
def harness_errors(cargo_json: str) -> list[str]:
errors = []
for message in cargo_messages(cargo_json, "compiler-message"):
if message.get("target", {}).get("name") != HARNESS_TARGET:
continue
diagnostic = message.get("message", {})
if diagnostic.get("level") != "error":
continue
in_harness = any(
span.get("is_primary")
and span.get("file_name", "").replace("\\", "/").endswith("src/main.rs")
for span in diagnostic.get("spans", [])
)
if in_harness:
errors.append(diagnostic.get("rendered") or diagnostic.get("message", ""))
return errors
def harness_executable(cargo_json: str) -> Path | None:
for message in cargo_messages(cargo_json, "compiler-artifact"):
if message.get("target", {}).get("name") == HARNESS_TARGET and message.get("executable"):
return Path(message["executable"])
return None
def build(crate: Path, harness: Path, work: Path) -> Path:
(work / "src").mkdir(parents=True)
(work / "Cargo.toml").write_text(
MANIFEST.format(crate=crate.resolve().as_posix()), encoding="utf-8"
)
shutil.copyfile(harness, work / "src" / "main.rs")
lockfile = crate / "Cargo.lock"
if lockfile.is_file():
shutil.copyfile(lockfile, work / "Cargo.lock")
command = [
"cargo",
"build",
"--release",
"--message-format=json",
"--manifest-path",
str(work / "Cargo.toml"),
"--target-dir",
str(work / "target"),
]
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode != 0:
sys.stderr.write(result.stderr)
for diagnostic in rendered_diagnostics(result.stdout):
sys.stderr.write(diagnostic)
errors = harness_errors(result.stdout)
if errors:
raise HarnessIncompatible(f"the harness does not compile against {crate}")
raise subprocess.CalledProcessError(result.returncode, command, result.stdout, result.stderr)
executable = harness_executable(result.stdout)
if executable is None:
raise RuntimeError(f"cargo reported no {HARNESS_TARGET} executable")
return executable
def count(binary: Path, scenario: str, iterations: int) -> int:
result = run_checked(
[
"valgrind",
"--tool=cachegrind",
"--cache-sim=no",
"--cachegrind-out-file=/dev/null",
str(binary),
scenario,
str(iterations),
]
)
return parse_irefs(result.stderr)
def measure(binary: Path, scenarios: list[str], low: int, high: int) -> dict[str, Fraction]:
return {
scenario: per_iteration(
count(binary, scenario, low), count(binary, scenario, high), low, high
)
for scenario in scenarios
}
def revision(checkout: Path) -> str:
result = subprocess.run(
["git", "-C", str(checkout), "rev-parse", "--short", "HEAD"],
capture_output=True,
text=True,
)
return result.stdout.strip() or checkout.name
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--base", type=Path, required=True, help="base checkout")
parser.add_argument("--head", type=Path, required=True, help="head checkout")
parser.add_argument("--tolerance", type=float, default=0.02)
parser.add_argument("--min-delta", type=float, default=2.0)
parser.add_argument("--low", type=int, default=1_000)
parser.add_argument("--high", type=int, default=11_000)
parser.add_argument("--summary", type=Path, help="append the Markdown report here")
parser.add_argument("--report-only", action="store_true", help="never fail")
args = parser.parse_args(argv)
harness = args.head / HARNESS
with tempfile.TemporaryDirectory() as temp:
temp = Path(temp)
head_binary = build(args.head, harness, temp / "head")
scenarios = run_checked([str(head_binary), "--list"]).stdout.split()
note = f"Base `{revision(args.base)}`, head `{revision(args.head)}`."
try:
base_binary = build(args.base, harness, temp / "base")
except HarnessIncompatible:
base_binary = None
note += " The head scenarios use API that base lacks, so only head is shown."
head = measure(head_binary, scenarios, args.low, args.high)
base = None if base_binary is None else measure(base_binary, scenarios, args.low, args.high)
rows = compare(base, head, args.tolerance, args.min_delta)
report = render(rows, args.tolerance, args.min_delta, note)
print(report)
if args.summary is not None:
with args.summary.open("a", encoding="utf-8") as summary:
summary.write(report)
if any(row.regressed for row in rows) and not args.report_only:
return 1
return 0
if __name__ == "__main__":
sys.exit(main())