# Author: Guoliang Chen, a Senior Digital Design Engineer """Run one command, print one JSON line: wall ms, child peak RSS, exit, summary line. Usage: python3 -I timer.py -- Runs *inside* tools/guard.sh, so the guard's 1 s poll does not enter the wall time. """ import json import resource import subprocess import sys import time cmd = sys.argv[sys.argv.index("--") + 1:] t0 = time.perf_counter() p = subprocess.run(cmd, capture_output=True, text=True) wall = (time.perf_counter() - t0) * 1000.0 ru = resource.getrusage(resource.RUSAGE_CHILDREN) out = (p.stdout + p.stderr).strip().splitlines() summary = [l for l in out if l.startswith(("found ", "Found ")) or "files would be" in l or "file would be" in l or "left unchanged" in l or "failed to format" in l or "could not be checked" in l] print(json.dumps({"wall_ms": round(wall, 2), "peak_rss_kib": ru.ru_maxrss // 1024, "user_s": round(ru.ru_utime, 3), "sys_s": round(ru.ru_stime, 3), "exit": p.returncode, "lines": len(out), "summary": summary[-2:] if summary else out[-1:]})) # Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com