from __future__ import annotations
import re
import sys
from ansi import ANSI_RE COUNT_RE = re.compile(r"^\s*Instructions:\s*([0-9][0-9,]*)\|")
def counts(text: str) -> list[int]:
out = []
for line in ANSI_RE.sub("", text).splitlines():
m = COUNT_RE.match(line)
if m:
out.append(int(m.group(1).replace(",", "")))
return out
def main(argv: list[str]) -> int:
with open(argv[1], encoding="utf-8", errors="replace") as f:
cs = counts(f.read())
if not cs:
print("error: no `Instructions:` line in the output (compared nothing)", file=sys.stderr)
return 1
zero = sum(1 for c in cs if c == 0)
if zero:
print(f"error: {zero} of {len(cs)} benchmarks measured 0 instructions (Callgrind collected nothing)",
file=sys.stderr)
return 1
print(f"benchmarks measured: {len(cs)}, instructions {min(cs)}..{max(cs)}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))