forked from Verified-zkEVM/CompPoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_timing_report.sh
More file actions
384 lines (332 loc) · 11.1 KB
/
Copy pathbuild_timing_report.sh
File metadata and controls
384 lines (332 loc) · 11.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage:
build_timing_report.sh run <label> <results-file> -- <command> [args...]
build_timing_report.sh render <results-file> [baseline-artifact-dir]
Labels:
clean_build
warm_rebuild
test_path
EOF
}
append_result() {
local results_file="$1"
local label="$2"
local real_time="$3"
local user_time="$4"
local sys_time="$5"
local exit_code="$6"
python3 - "$results_file" "$label" "$real_time" "$user_time" "$sys_time" "$exit_code" <<'PY'
import json
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
path.parent.mkdir(parents=True, exist_ok=True)
record = {
"label": sys.argv[2],
"real": float(sys.argv[3]),
"user": float(sys.argv[4]),
"sys": float(sys.argv[5]),
"exit_code": int(sys.argv[6]),
}
with path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(record) + "\n")
PY
}
run_command() {
if [ "$#" -lt 4 ]; then
usage
exit 2
fi
local label="$1"
local results_file="$2"
shift 2
if [ "$1" != "--" ]; then
usage
exit 2
fi
shift
local timing_file
timing_file="$(mktemp)"
local log_dir="${BUILD_TIMING_LOG_DIR:-}"
local log_file=""
if [ -n "$log_dir" ]; then
mkdir -p "$log_dir"
log_file="$log_dir/${label}.log"
fi
set +e
if [ -n "$log_file" ]; then
/usr/bin/time -p -o "$timing_file" "$@" 2>&1 | tee "$log_file"
local exit_code=${PIPESTATUS[0]}
else
/usr/bin/time -p -o "$timing_file" "$@"
local exit_code=$?
fi
set -e
local real_time user_time sys_time
real_time="$(awk '$1 == "real" { print $2 }' "$timing_file")"
user_time="$(awk '$1 == "user" { print $2 }' "$timing_file")"
sys_time="$(awk '$1 == "sys" { print $2 }' "$timing_file")"
rm -f "$timing_file"
append_result "$results_file" "$label" "$real_time" "$user_time" "$sys_time" "$exit_code"
exit "$exit_code"
}
render_report() {
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
usage
exit 2
fi
local results_file="$1"
local baseline_dir="${2:-}"
python3 - "$results_file" "$baseline_dir" <<'PY'
import json
import os
import pathlib
import re
import sys
results_path = pathlib.Path(sys.argv[1])
baseline_dir = pathlib.Path(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2] else None
display = {
"clean_build": {
"name": "Clean build",
"command": "`rm -rf .lake/build && lake build`",
},
"warm_rebuild": {
"name": "Warm rebuild",
"command": "`lake build`",
},
"test_path": {
"name": "Test path",
"command": "`lake test`",
},
}
ordered_labels = ["clean_build", "warm_rebuild", "test_path"]
def load_records(path: pathlib.Path) -> dict[str, dict]:
records = {}
if not path.exists():
return records
for line in path.read_text(encoding="utf-8").splitlines():
if not line.strip():
continue
record = json.loads(line)
records[record["label"]] = record
return records
def fmt(value: float) -> str:
return f"{value:.2f}"
def fmt_delta(value: float) -> str:
return f"{value:+.2f}"
def status(record: dict) -> str:
return "ok" if record["exit_code"] == 0 else f"exit {record['exit_code']}"
def module_to_source_path(target: str) -> str | None:
if target == "CompPoly":
return "CompPoly.lean"
if target.startswith("CompPoly."):
return target.replace(".", "/") + ".lean"
if target == "CompPolyTests":
return "tests/CompPolyTests.lean"
if target.startswith("CompPolyTests."):
return "tests/" + target.replace(".", "/") + ".lean"
return None
def extract_clean_build_targets(log_path: pathlib.Path | None) -> list[dict]:
if log_path is None or not log_path.exists():
return []
pattern = re.compile(r"Built\s+(.+?)\s+\((\d+(?:\.\d+)?)s\)")
entries = []
seen = set()
for line in log_path.read_text(encoding="utf-8", errors="replace").splitlines():
match = pattern.search(line)
if not match:
continue
target = match.group(1).strip()
if target in seen:
continue
seen.add(target)
entries.append(
{
"target": target,
"seconds": float(match.group(2)),
"source": module_to_source_path(target),
}
)
preferred = [
entry for entry in entries if entry["target"].startswith(("CompPoly", "CompPolyTests"))
]
selected = preferred if preferred else entries
return sorted(selected, key=lambda entry: entry["seconds"], reverse=True)
def target_key(entry: dict) -> str:
return entry["source"] or entry["target"]
current_records = load_records(results_path)
baseline_records = load_records(baseline_dir / "results.jsonl") if baseline_dir else {}
current_log_dir = os.environ.get("BUILD_TIMING_LOG_DIR")
current_log_path = pathlib.Path(current_log_dir) / "clean_build.log" if current_log_dir else None
current_clean_build_targets = extract_clean_build_targets(current_log_path)
baseline_clean_build_targets = (
extract_clean_build_targets(baseline_dir / "clean_build.log") if baseline_dir else []
)
source_sha = os.environ.get("BUILD_TIMING_SOURCE_SHA")
source_subject = os.environ.get("BUILD_TIMING_SOURCE_SUBJECT")
source_branch = os.environ.get("BUILD_TIMING_SOURCE_BRANCH") or os.environ.get("GITHUB_REF_NAME")
source_repo = os.environ.get("GITHUB_REPOSITORY")
baseline_sha = os.environ.get("BUILD_TIMING_BASELINE_SHA")
baseline_label = os.environ.get("BUILD_TIMING_BASELINE_LABEL")
print("## Build Timing Report")
print()
if source_sha:
short_sha = source_sha[:7]
if source_repo:
commit_ref = f"[`{short_sha}`](https://github.com/{source_repo}/commit/{source_sha})"
else:
commit_ref = f"`{short_sha}`"
print(f"- Commit: {commit_ref}")
if source_subject:
print(f"- Message: {source_subject}")
if source_branch:
print(f"- Ref: `{source_branch}`")
if baseline_records:
if baseline_sha:
baseline_short_sha = baseline_sha[:7]
if source_repo:
baseline_commit_ref = (
f"[`{baseline_short_sha}`](https://github.com/{source_repo}/commit/{baseline_sha})"
)
else:
baseline_commit_ref = f"`{baseline_short_sha}`"
if baseline_label:
print(f"- Comparison baseline: {baseline_commit_ref} from {baseline_label}.")
else:
print(f"- Comparison baseline: {baseline_commit_ref}.")
elif baseline_label:
print(f"- Comparison baseline: {baseline_label}.")
print("- Measured on `ubuntu-latest` with `/usr/bin/time -p`.")
print(
"- Commands: "
+ "; ".join(
f"{display[label]['name'].lower()} {display[label]['command']}" for label in ordered_labels
)
+ "."
)
print()
if not current_records:
print("No timing data was captured.")
sys.exit(0)
if baseline_records:
print("| Measurement | Baseline (s) | Current (s) | Delta (s) | Status |")
print("| --- | ---: | ---: | ---: | --- |")
for label in ordered_labels:
baseline_record = baseline_records.get(label)
current_record = current_records.get(label)
if not baseline_record and not current_record:
continue
baseline_time = fmt(baseline_record["real"]) if baseline_record else "-"
current_time = fmt(current_record["real"]) if current_record else "-"
delta = (
fmt_delta(current_record["real"] - baseline_record["real"])
if baseline_record and current_record
else "-"
)
current_status = status(current_record) if current_record else "-"
print(
f"| {display[label]['name']} | {baseline_time} | {current_time} | {delta} | "
f"{current_status} |"
)
else:
print("| Measurement | Wall (s) | Status |")
print("| --- | ---: | --- |")
for label in ordered_labels:
if label not in current_records:
continue
record = current_records[label]
print(f"| {display[label]['name']} | {fmt(record['real'])} | {status(record)} |")
clean = current_records.get("clean_build")
warm = current_records.get("warm_rebuild")
print()
print("### Incremental Rebuild Signal")
print()
if clean and warm:
delta = clean["real"] - warm["real"]
ratio = clean["real"] / warm["real"] if warm["real"] else None
if ratio is None:
print(f"- Warm rebuild saved `{delta:.2f}s` vs clean.")
print("- Clean:warm ratio is unavailable because `warm rebuild` reported `0.00s`.")
elif delta > 0:
print(f"- Warm rebuild saved `{delta:.2f}s` vs clean (`{ratio:.2f}x` faster).")
elif delta < 0:
slowdown = warm["real"] - clean["real"]
slowdown_ratio = warm["real"] / clean["real"] if clean["real"] else None
if slowdown_ratio is None:
print(f"- Warm rebuild took `{slowdown:.2f}s` longer than clean in this run.")
else:
print(
f"- Warm rebuild took `{slowdown:.2f}s` longer than clean in this run "
f"(`{slowdown_ratio:.2f}x` slower)."
)
else:
print("- Warm rebuild matched clean build wall-clock in this run.")
else:
print("- Clean:warm comparison is unavailable because one of the build measurements is missing.")
print()
print(
"This compares a clean project build against an incremental rebuild in the same CI job; "
"it is a lightweight variability signal, not a full cross-run benchmark."
)
print()
print("### Slowest Current Clean-Build Files")
print()
if current_clean_build_targets:
shown = current_clean_build_targets[:20]
if baseline_clean_build_targets:
baseline_targets_by_key = {
target_key(entry): entry for entry in baseline_clean_build_targets
}
print(
f"Showing {len(shown)} slowest current targets, with comparison against the selected baseline when available."
)
print()
print("| Current (s) | Baseline (s) | Delta (s) | Path |")
print("| ---: | ---: | ---: | --- |")
for entry in shown:
key = target_key(entry)
baseline_entry = baseline_targets_by_key.get(key)
baseline_time = fmt(baseline_entry["seconds"]) if baseline_entry else "-"
delta = (
fmt_delta(entry["seconds"] - baseline_entry["seconds"])
if baseline_entry
else "-"
)
print(f"| {fmt(entry['seconds'])} | {baseline_time} | {delta} | `{key}` |")
else:
print(
f"Showing {len(shown)} slowest of {len(current_clean_build_targets)} repo targets parsed from the current clean build log."
)
print()
print("| Wall (s) | Path |")
print("| ---: | --- |")
for entry in shown:
print(f"| {fmt(entry['seconds'])} | `{target_key(entry)}` |")
else:
print("No per-target timings were parsed from the current clean build log.")
PY
}
main() {
if [ "$#" -lt 1 ]; then
usage
exit 2
fi
local command="$1"
shift
case "$command" in
run)
run_command "$@"
;;
render)
render_report "$@"
;;
*)
usage
exit 2
;;
esac
}
main "$@"