1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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())
|