upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorpes7 <pes7@users.noreply.github.com>2026-11-24 01:56:31 +0400
committerClaude <noreply@anthropic.com>2026-11-25 11:33:51 +0000
commitf03dfe23ae29c56bc705865367d4fa00b7100fa4 (patch)
tree9f9ffec4893147603574d856adc10a0157454707 /scripts
parent3bcb6212a22afe78c85900dbd93d40f1c3554d70 (diff)
bench_translate: refuse flag combinations that were silently ignored, and require a baseline that exists
Reported by @SamuelReeder on #7056. --compare ignored --json, --write-baseline and --baseline; a --baseline path that does not exist was accepted and produced empty numbers; and naming one file for both baseline flags let a run that failed the limit replace the reference it failed against, so the same workload passed on the retry. All three are now parser errors, with the reason in the message. --compare also refuses to compare two trees whose measurements do not cover the same programs (a skipped program read as a ratio that looks like a win) and reports an empty corpus instead of dividing by nothing. Tests: the three refusals, an aliased baseline, a missing baseline file.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/bench_translate.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/scripts/bench_translate.py b/scripts/bench_translate.py
index 9bc0a85de..b639098b9 100644
--- a/scripts/bench_translate.py
+++ b/scripts/bench_translate.py
@@ -154,16 +154,27 @@ def compare(reference, candidate, runs, size, rounds, max_slowdown=None):
154 return json.loads(result.stdout)["measured"] 154 return json.loads(result.stdout)["measured"]
155 155
156 best = {"reference": {}, "candidate": {}} 156 best = {"reference": {}, "candidate": {}}
157 measured_any = False
157 for round_index in range(rounds + 1): # the first round warms the caches and is not counted 158 for round_index in range(rounds + 1): # the first round warms the caches and is not counted
158 order = [("reference", reference), ("candidate", candidate)] 159 order = [("reference", reference), ("candidate", candidate)]
159 if round_index % 2: 160 if round_index % 2:
160 order.reverse() # whoever runs first pays the cold file cache, so the order alternates 161 order.reverse() # whoever runs first pays the cold file cache, so the order alternates
161 for side, path in order: 162 for side, path in order:
162 measured = measure(path) 163 measured = measure(path)
164 measured_any = measured_any or bool(measured)
163 if round_index == 0: 165 if round_index == 0:
164 continue 166 continue
165 for key, value in measured.items(): 167 for key, value in measured.items():
166 best[side][key] = min(best[side].get(key, value), value) 168 best[side][key] = min(best[side].get(key, value), value)
169 if not measured_any:
170 raise SystemExit("both trees measured nothing (an empty corpus?); nothing to compare")
171 if sorted(best["reference"]) != sorted(best["candidate"]):
172 only_reference = sorted(set(best["reference"]) - set(best["candidate"]))
173 only_candidate = sorted(set(best["candidate"]) - set(best["reference"]))
174 raise SystemExit("the two trees do not cover the same measurements"
175 + (f"; only in the reference: {', '.join(only_reference)}" if only_reference else "")
176 + (f"; only in the candidate: {', '.join(only_candidate)}" if only_candidate else "")
177 + "; a missing program would read as a ratio that looks like a win")
167 rows = [] 178 rows = []
168 for key in best["reference"]: 179 for key in best["reference"]:
169 ref = best["reference"][key] 180 ref = best["reference"][key]
@@ -171,8 +182,8 @@ def compare(reference, candidate, runs, size, rounds, max_slowdown=None):
171 rows.append({"name": key, "reference": ref, "candidate": cand, "ratio": cand / ref if ref else None}) 182 rows.append({"name": key, "reference": ref, "candidate": cand, "ratio": cand / ref if ref else None})
172 print(f"{'measurement':<32} {'reference':>10} {'candidate':>10} {'ratio':>7}") 183 print(f"{'measurement':<32} {'reference':>10} {'candidate':>10} {'ratio':>7}")
173 for row in rows: 184 for row in rows:
174 print(f"{row['name']:<32} {row['reference']:>10.1f} {row['candidate']:>10.1f} " 185 ratio = f"{row['ratio']:>6.2f}x" if row["ratio"] is not None else " n/a"
175 f"{row['ratio']:>6.2f}x") 186 print(f"{row['name']:<32} {row['reference']:>10.1f} {row['candidate']:>10.1f} {ratio}")
176 print(f"\nreference {os.path.abspath(reference)}\ncandidate {os.path.abspath(candidate)}") 187 print(f"\nreference {os.path.abspath(reference)}\ncandidate {os.path.abspath(candidate)}")
177 print("ratio below 1.00 is the candidate being faster; rounds %d, min per side" % rounds) 188 print("ratio below 1.00 is the candidate being faster; rounds %d, min per side" % rounds)
178 if max_slowdown is not None: 189 if max_slowdown is not None:
@@ -206,9 +217,20 @@ def main(argv=None):
206 arguments = parser.parse_args(argv) 217 arguments = parser.parse_args(argv)
207 if arguments.rounds < 1: 218 if arguments.rounds < 1:
208 parser.error("--rounds must be at least 1") 219 parser.error("--rounds must be at least 1")
220 if arguments.baseline and arguments.write_baseline and \
221 os.path.abspath(arguments.baseline) == os.path.abspath(arguments.write_baseline):
222 # One file for both: a run that fails the limit would still replace the reference it failed against, and the
223 # same workload would pass on the retry.
224 parser.error("--baseline and --write-baseline name the same file; write to a different one")
209 if arguments.compare: 225 if arguments.compare:
226 for flag, value in (("--json", arguments.json), ("--write-baseline", arguments.write_baseline),
227 ("--baseline", arguments.baseline)):
228 if value:
229 parser.error(f"{flag} does not apply to --compare, which times two trees and prints its own table")
210 return compare(*arguments.compare, arguments.runs, arguments.size, arguments.rounds, 230 return compare(*arguments.compare, arguments.runs, arguments.size, arguments.rounds,
211 arguments.max_slowdown) 231 arguments.max_slowdown)
232 if arguments.baseline and not os.path.isfile(arguments.baseline):
233 parser.error(f"--baseline {arguments.baseline} does not exist; --write-baseline records one")
212 if arguments.runs < 1: 234 if arguments.runs < 1:
213 parser.error("--runs must be at least 1") 235 parser.error("--runs must be at least 1")
214 if arguments.size < 1: 236 if arguments.size < 1: