Skip to content

Commit 858fd86

Browse files
committed
wip
1 parent 4243909 commit 858fd86

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

.github/scripts/compare_benchmarks.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ class Comparison:
4545
delta_percent (float): Percent change from baseline to current.
4646
unit (str): Display unit for both values.
4747
metric (str): Current result metric used for comparison.
48-
regressed (bool): Whether the change exceeds the configured threshold.
48+
regressed (bool): Whether the change exceeds the configured threshold in
49+
the worse direction.
50+
improved (bool): Whether the change exceeds the configured threshold in
51+
the better direction.
4952
"""
5053

5154
name: str
@@ -55,6 +58,7 @@ class Comparison:
5558
unit: str
5659
metric: str
5760
regressed: bool
61+
improved: bool
5862

5963

6064
def _read_json(path: Path) -> Any:
@@ -246,8 +250,10 @@ def compare_benchmarks(
246250

247251
if higher_is_better:
248252
regressed = delta_percent <= -threshold_percent
253+
improved = delta_percent >= threshold_percent
249254
else:
250255
regressed = delta_percent >= threshold_percent
256+
improved = delta_percent <= -threshold_percent
251257

252258
comparisons.append(
253259
Comparison(
@@ -258,6 +264,7 @@ def compare_benchmarks(
258264
unit=current_benchmark.unit or baseline_benchmark.unit,
259265
metric=current_benchmark.metric,
260266
regressed=regressed,
267+
improved=improved,
261268
)
262269
)
263270

@@ -282,6 +289,16 @@ def _format_value(value: float, unit: str) -> str:
282289
return f"{value:.6g}{suffix}"
283290

284291

292+
def _format_status(comparison: Comparison) -> str:
293+
"""Format a comparison status for Markdown output."""
294+
295+
if comparison.regressed:
296+
return ":red_circle: regressed"
297+
if comparison.improved:
298+
return ":green_circle: improved"
299+
return "ok"
300+
301+
285302
def write_summary(
286303
path: Path,
287304
comparisons: list[Comparison],
@@ -305,6 +322,7 @@ def write_summary(
305322
"""
306323

307324
regressions = [comparison for comparison in comparisons if comparison.regressed]
325+
improvements = [comparison for comparison in comparisons if comparison.improved]
308326
direction = "higher is better" if higher_is_better else "lower is better"
309327
sorted_comparisons = sorted(comparisons, key=lambda comparison: comparison.name)
310328

@@ -313,6 +331,7 @@ def write_summary(
313331
"## Benchmark comparison",
314332
"",
315333
f"Threshold: {threshold_percent:g}% ({direction}).",
334+
f"Result: {len(regressions)} regression(s), {len(improvements)} improvement(s) beyond threshold.",
316335
]
317336
lines.append("")
318337

@@ -336,6 +355,28 @@ def write_summary(
336355
else:
337356
lines.append("No benchmark regression exceeded the configured threshold.")
338357

358+
lines.append("")
359+
360+
if improvements:
361+
lines.extend(
362+
[
363+
f"{len(improvements)} benchmark(s) improved beyond the configured threshold.",
364+
"",
365+
"| Benchmark | Baseline | Current | Change |",
366+
"| --- | ---: | ---: | ---: |",
367+
]
368+
)
369+
for comparison in improvements:
370+
lines.append(
371+
"| "
372+
f"{comparison.name} | "
373+
f"{_format_value(comparison.baseline, comparison.unit)} | "
374+
f"{_format_value(comparison.current, comparison.unit)} | "
375+
f"{comparison.delta_percent:+.2f}% |"
376+
)
377+
else:
378+
lines.append("No benchmark improvement exceeded the configured threshold.")
379+
339380
if sorted_comparisons:
340381
lines.extend(
341382
[
@@ -348,14 +389,13 @@ def write_summary(
348389
]
349390
)
350391
for comparison in sorted_comparisons:
351-
status = "regressed" if comparison.regressed else "ok"
352392
lines.append(
353393
"| "
354394
f"{comparison.name} | "
355395
f"{_format_value(comparison.baseline, comparison.unit)} | "
356396
f"{_format_value(comparison.current, comparison.unit)} | "
357397
f"{comparison.delta_percent:+.2f}% | "
358-
f"{status} |"
398+
f"{_format_status(comparison)} |"
359399
)
360400
lines.extend(["", "</details>"])
361401

0 commit comments

Comments
 (0)