-
Notifications
You must be signed in to change notification settings - Fork 11
/
bench.py
executable file
·181 lines (157 loc) · 6.86 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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)
full_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={full_build_report_path}",
"--prepare=cargo clean",
# Doing debug builds because that is more likely the
# time directly impacting people
f"cargo build -j {cpus} --manifest-path {example_path}/Cargo.toml"
]
if False:
hyperfine_cmd.append("--show-output")
subprocess.run(
hyperfine_cmd,
cwd=repo_root,
check=True,
)
full_build_report = json.loads(full_build_report_path.read_text())
else:
full_build_report = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("build_inc", None)
inc_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={inc_build_report_path}",
f"--prepare=touch {example_path}/app.rs",
# Doing debug builds because that is more likely the
# time directly impacting people
f"cargo build -j {cpus} --manifest-path {example_path}/Cargo.toml"
]
if False:
hyperfine_cmd.append("--show-output")
subprocess.run(
hyperfine_cmd,
cwd=repo_root,
check=True,
)
inc_build_report = json.loads(inc_build_report_path.read_text())
else:
inc_build_report = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("build_inc", 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)
xargs_report_path = pathlib.Path(tmpdir) / f"{example_path.name}-xargs.json"
if True and app_path is not None:
# This is intended to see how well the crate handles large number of arguments from
# - Shell glob expansion
# - `find -exec`
# - Piping to `xargs`
large_arg = " ".join(["some/path/that/find/found"] * 1000)
hyperfine_cmd = [
"hyperfine",
"--warmup=1",
"--min-runs=5",
f"--export-json={xargs_report_path}",
# Doing debug builds because that is more likely the
# time directly impacting people
f"{app_path} --number 42 {large_arg}"
]
if False:
hyperfine_cmd.append("--show-output")
subprocess.run(
hyperfine_cmd,
cwd=repo_root,
check=True,
)
xargs_report = json.loads(xargs_report_path.read_text())
else:
xargs_report = old_raw_run.get("libs", {}).get(str(manifest_path), {}).get("xargs", None)
p = subprocess.run(["cargo", "run", "--package", example_path.name, "--", "--number", "10", "path"], cwd=repo_root, capture_output=True, encoding="utf-8")
works = p.returncode == 0
p = subprocess.run(["cargo", "run", "--package", example_path.name, "--", "--number", "10", b"\xe9"], cwd=repo_root, capture_output=True, encoding="utf-8")
basic_osstr = p.returncode == 0
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_inc": inc_build_report,
"build_full": full_build_report,
"xargs": xargs_report,
"size": file_size,
"works": works,
"osstr_basic": basic_osstr,
}
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()