|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Compare the baseline and PR builds of a benchmark over several criterion processes. |
| 3 | +
|
| 4 | +Each argument is a directory holding `<side>/<run>/{benchmark,estimates}.json`, one run |
| 5 | +per process, for the sides `neqo-baseline` and `neqo`. Prints each comparison in |
| 6 | +criterion's own text format, which format-bench-results.py and the regression gate parse. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | +from statistics import median |
| 13 | + |
| 14 | +from scipy.stats import mannwhitneyu |
| 15 | + |
| 16 | +SIGNIFICANCE = 0.05 |
| 17 | +# Criterion's default, and the `noise_threshold` that the walltime benchmark groups set. |
| 18 | +NOISE_THRESHOLD = 0.01 |
| 19 | +GROUP_NOISE_THRESHOLDS = { |
| 20 | + "streams": 0.03, |
| 21 | + "streams-flow-controlled": 0.03, |
| 22 | + "transfer": 0.03, |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +def short(n: float, sign: str = "") -> str: |
| 27 | + """Format a number like criterion's `format::short` or `signed_short`.""" |
| 28 | + for limit, digits in ((10, 4), (100, 3), (1000, 2), (10000, 1)): |
| 29 | + if abs(n) < limit: |
| 30 | + return f"{n:{sign}.{digits}f}" |
| 31 | + return f"{n:{sign}.0f}" |
| 32 | + |
| 33 | + |
| 34 | +def time(ns: float) -> str: |
| 35 | + """Format nanoseconds like criterion's `format::time`.""" |
| 36 | + for scale, unit in ((1e9, "s"), (1e6, "ms"), (1e3, "µs"), (1, "ns")): |
| 37 | + if ns >= scale: |
| 38 | + return f"{short(ns / scale):>6} {unit}" |
| 39 | + return f"{short(ns * 1e3):>6} ps" |
| 40 | + |
| 41 | + |
| 42 | +def change(ratio: float) -> str: |
| 43 | + """Format a relative change like criterion's `format::change`.""" |
| 44 | + return f"{short(ratio * 1e2, '+'):>6}%" |
| 45 | + |
| 46 | + |
| 47 | +def bracket(values: list[float], fmt) -> str: |
| 48 | + """Format the minimum, median and maximum of `values`.""" |
| 49 | + return f"[{fmt(min(values))} {fmt(median(values))} {fmt(max(values))}]" |
| 50 | + |
| 51 | + |
| 52 | +def read(path: Path) -> dict: |
| 53 | + """Read a JSON file criterion wrote.""" |
| 54 | + return json.loads(path.read_text(encoding="utf-8")) |
| 55 | + |
| 56 | + |
| 57 | +def means(side: Path) -> list[float]: |
| 58 | + """Return each run's mean, in nanoseconds.""" |
| 59 | + return [read(f)["mean"]["point_estimate"] for f in side.glob("*/estimates.json")] |
| 60 | + |
| 61 | + |
| 62 | +def compare(path: Path) -> list[str]: |
| 63 | + """Return criterion's output lines for one benchmark.""" |
| 64 | + pr = means(path / "neqo") |
| 65 | + if not pr: |
| 66 | + return [] |
| 67 | + base = means(path / "neqo-baseline") |
| 68 | + bench = read(next(path.glob("neqo/*/benchmark.json"))) |
| 69 | + title = bench["title"] |
| 70 | + pad = " " * 24 |
| 71 | + timing = f"time: {bracket(pr, time)}" |
| 72 | + # Criterion puts ids of more than 23 characters on a line of their own. |
| 73 | + lines = [title, pad + timing] if len(title) > 23 else [f"{title:<24}{timing}"] |
| 74 | + if not base: |
| 75 | + return lines |
| 76 | + |
| 77 | + p = mannwhitneyu(base, pr, alternative="two-sided", method="exact").pvalue |
| 78 | + delta = median(pr) / median(base) - 1 |
| 79 | + if p >= SIGNIFICANCE: |
| 80 | + verdict = "No change in performance detected." |
| 81 | + elif abs(delta) <= GROUP_NOISE_THRESHOLDS.get(bench["group_id"], NOISE_THRESHOLD): |
| 82 | + verdict = "Change within noise threshold." |
| 83 | + else: |
| 84 | + verdict = f"Performance has {'regressed' if delta > 0 else 'improved'}." |
| 85 | + relative = [v / median(base) - 1 for v in pr] |
| 86 | + p_text = f"p = {p:.2f} {'<' if p < SIGNIFICANCE else '>'} {SIGNIFICANCE:.2f}" |
| 87 | + return [ |
| 88 | + *lines, |
| 89 | + f"{pad}baseline: {bracket(base, time)}", |
| 90 | + f"{' ' * 17}change:", |
| 91 | + f"{pad}time: {bracket(relative, change)} ({p_text})", |
| 92 | + pad + verdict, |
| 93 | + ] |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + """Print the comparison for each benchmark directory given.""" |
| 98 | + for arg in sys.argv[1:]: |
| 99 | + for line in compare(Path(arg)): |
| 100 | + print(line) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments