-
Notifications
You must be signed in to change notification settings - Fork 14
/
bench.py
executable file
·144 lines (123 loc) · 4.97 KB
/
bench.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
import copy
import datetime
import json
import multiprocessing
import pathlib
import platform
import subprocess
import sys
import tempfile
def main():
repo_root = pathlib.Path(__name__).parent
timestamp = datetime.datetime.now().strftime("%Y-%m-%d")
hostname = platform.node()
uname = platform.uname()
cpus = multiprocessing.cpu_count()
rustc = subprocess.run(["rustc", "--version"], check=True, capture_output=True, encoding="utf-8").stdout.strip()
extension = ".exe" if sys.platform in ("win32", "cygwin") else ""
runs_root = repo_root / "runs"
runs_root.mkdir(parents=True, exist_ok=True)
raw_run_path = runs_root / "{}-{}.json".format(timestamp, hostname)
if raw_run_path.exists():
old_raw_run = json.loads(raw_run_path.read_text())
else:
old_raw_run = {}
raw_run = {
"timestamp": timestamp,
"hostname": hostname,
"os": uname.system,
"os_ver": uname.release,
"arch": uname.machine,
"cpus": cpus,
"rustc": rustc,
"libs": {},
}
with tempfile.TemporaryDirectory() as tmpdir:
for example_path in sorted((repo_root / "examples").glob("*-app")):
manifest_path = example_path / "Cargo.toml"
metadata = harvest_metadata(manifest_path)
build_report_path = pathlib.Path(tmpdir) / f"{example_path.name}-build.json"
if True:
hyperfine_cmd = [
"hyperfine",
"--warmup=1",
"--min-runs=5",
f"--export-json={build_report_path}",
"--prepare=cargo clean",
# Doing debug builds because that is more likely the
# time directly impacting people
f"cargo build -j {cpus} --package {example_path.name}"
]
if False:
hyperfine_cmd.append("--show-output")
subprocess.run(
hyperfine_cmd,
cwd=repo_root,
check=True,
)
build_report = json.loads(build_report_path.read_text())
else:
build_report = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("build", None)
if True:
# Doing release builds because that is where size probably matters most
subprocess.run(["cargo", "build", "--release", "--package", example_path.name], cwd=repo_root, check=True)
app_path = repo_root / f"target/release/{example_path.name}{extension}"
file_size = app_path.stat().st_size
else:
app_path = None
file_size = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("size", None)
run_report_path = pathlib.Path(tmpdir) / f"{example_path.name}-run.json"
if True and app_path is not None:
json_path = pathlib.Path(__file__).parent / "third_party/nativejson-benchmark/data/canada.json"
assert json_path.exists()
hyperfine_cmd = [
"hyperfine",
"--warmup=1",
"--min-runs=5",
f"--export-json={run_report_path}",
f"{app_path} {json_path}"
]
if False:
hyperfine_cmd.append("--show-output")
subprocess.run(
hyperfine_cmd,
cwd=repo_root,
check=True,
)
run_report = json.loads(run_report_path.read_text())
else:
run_report = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("run", None)
raw_run["libs"][str(manifest_path)] = {
"name": example_path.name.rsplit("-", 1)[0],
"manifest_path": str(manifest_path),
"crate": metadata["name"],
"version": metadata["version"],
"build": build_report,
"run": run_report,
"size": file_size,
}
raw_run_path.write_text(json.dumps(raw_run, indent=2))
print(raw_run_path)
def harvest_metadata(manifest_path):
p = subprocess.run(["cargo", "tree"], check=True, cwd=manifest_path.parent, capture_output=True, encoding="utf-8")
lines = p.stdout.strip().splitlines()
app_line = lines.pop(0)
if lines:
self_line = lines.pop(0)
name, version = _extract_line(self_line)
unique = sorted(set(_extract_line(line) for line in lines if "(*)" not in line and "[build-dependencies]" not in line))
else:
name = None
version = None
return {
"name": name,
"version": version,
}
def _extract_line(line):
if line.endswith(" (proc-macro)"):
line = line[0:-len(" (proc-macro)")]
_, name, version = line.rsplit(" ", 2)
return name, version
if __name__ == "__main__":
main()