summaryrefslogtreecommitdiffstats
path: root/tools/suite-cost.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/suite-cost.py')
-rwxr-xr-xtools/suite-cost.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/tools/suite-cost.py b/tools/suite-cost.py
new file mode 100755
index 0000000..eaf4039
--- /dev/null
+++ b/tools/suite-cost.py
@@ -0,0 +1,120 @@
+#!/usr/bin/env python3
+"""Measure what the test suite costs: wall time, CPU, and process churn.
+
+The suite is dominated by waiting — fsyncs on the scratch filesystem and
+subprocess spawns — so wall time alone hides the two numbers that actually move
+when the harness improves. This records all three the same way every time:
+
+ wall/cpu `cargo test --all-targets` under time + RUSAGE_CHILDREN, so
+ descendant CPU (git, the shims) counts against the suite.
+ execs the same run under `strace -f -e trace=execve`, counting only the
+ execve calls that succeeded: a `Command::new("git")` walks PATH and
+ every miss is an ENOENT execve, and those probes are overhead, not
+ work. `--no-census` skips the (slow) strace pass.
+
+Usage:
+ tools/suite-cost.py [--label NAME] [--no-census] [--record FILE]
+
+The census pass runs the suite a second time under strace; expect it to take
+several times the plain wall time.
+"""
+
+import argparse
+import collections
+import os
+import re
+import resource
+import subprocess
+import sys
+import tempfile
+import time
+from datetime import datetime, timezone
+
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+RESULT_RE = re.compile(r"^test result: \w+\. (\d+) passed; (\d+) failed")
+EXEC_RE = re.compile(r'^\d+ execve\("([^"]*)"')
+
+
+def run_suite(census=False):
+ """Run the suite, returning (wall, cpu, passed, failed, exec_counter)."""
+ cmd = ["cargo", "test", "--offline", "--all-targets", "--no-fail-fast"]
+ trace = None
+ if census:
+ trace = tempfile.NamedTemporaryFile(prefix="suite-cost-exec.", suffix=".txt",
+ delete=False).name
+ cmd = ["strace", "-f", "-qq", "-e", "trace=execve", "-o", trace] + cmd
+
+ before = resource.getrusage(resource.RUSAGE_CHILDREN)
+ start = time.time()
+ proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True)
+ wall = time.time() - start
+ after = resource.getrusage(resource.RUSAGE_CHILDREN)
+ cpu = (after.ru_utime - before.ru_utime) + (after.ru_stime - before.ru_stime)
+
+ passed = failed = 0
+ for line in proc.stdout.splitlines():
+ m = RESULT_RE.match(line)
+ if m:
+ passed += int(m.group(1))
+ failed += int(m.group(2))
+
+ execs = None
+ if census:
+ execs = count_execs(trace)
+ os.unlink(trace)
+ return wall, cpu, passed, failed, execs, proc.returncode
+
+
+def count_execs(path):
+ """Successful execve calls, tallied by program basename."""
+ tally = collections.Counter()
+ probes = 0
+ with open(path, errors="replace") as handle:
+ for line in handle:
+ m = EXEC_RE.match(line)
+ if not m:
+ continue
+ if "= -1 ENOENT" in line:
+ probes += 1
+ continue
+ tally[os.path.basename(m.group(1))] += 1
+ return tally, probes
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--label", default="run")
+ ap.add_argument("--no-census", action="store_true", help="skip the strace pass")
+ ap.add_argument("--record", help="append a markdown row to this file")
+ args = ap.parse_args()
+
+ subprocess.run(["cargo", "build", "--offline", "--all-targets"], cwd=ROOT,
+ check=True, capture_output=True)
+
+ wall, cpu, passed, failed, execs, rc = run_suite(census=not args.no_census)
+ print(f"{args.label}: {wall:.1f}s wall, {cpu:.1f}s cpu, "
+ f"{passed} passed, {failed} failed (cargo rc {rc})")
+ if execs is not None:
+ tally, probes = execs
+ total = sum(tally.values())
+ top = ", ".join(f"{n} {name}" for name, n in tally.most_common(5))
+ print(f" execs: {total} successful, {probes} PATH probes — {top}")
+
+ if args.record:
+ new = not os.path.exists(args.record)
+ with open(args.record, "a") as handle:
+ if new:
+ handle.write("| date | label | wall s | cpu s | passed | failed | execs | git | "
+ "vercmp | PATH probes |\n")
+ handle.write("|---|---|---|---|---|---|---|---|---|---|\n")
+ tally, probes = execs if execs else (collections.Counter(), 0)
+ stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M")
+ handle.write(f"| {stamp} | {args.label} | {wall:.1f} | {cpu:.1f} | {passed} | "
+ f"{failed} | {sum(tally.values())} | {tally.get('git', 0)} | "
+ f"{tally.get('vercmp', 0)} | {probes} |\n")
+ print(f" recorded in {args.record}")
+ return 1 if failed or rc else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())