|
| 1 | +from __future__ import annotations |
| 2 | +import json |
| 3 | +import pathlib |
| 4 | +import random |
| 5 | +import hashlib |
| 6 | +import collections |
| 7 | + |
| 8 | +ROOT = pathlib.Path(__file__).resolve().parent |
| 9 | +config = json.loads((ROOT / "evaluation_config.json").read_text()) |
| 10 | +rows = [ |
| 11 | + json.loads(line) |
| 12 | + for line in (ROOT / "performance-rows.jsonl").read_text().splitlines() |
| 13 | +] |
| 14 | +expected = ( |
| 15 | + 2 |
| 16 | + * len(config["providers"]) |
| 17 | + * len(config["history_counts"]) |
| 18 | + * len(config["modes"]) |
| 19 | + * (config["aa_pairs"] + config["ab_pairs"]) |
| 20 | +) |
| 21 | +assert len(rows) == expected, (len(rows), expected) |
| 22 | +groups = collections.defaultdict(dict) |
| 23 | +for row in rows: |
| 24 | + key = (row["case"], row["mode"], row["phase"]) |
| 25 | + pair = groups[key].setdefault(row["pair"], {}) |
| 26 | + assert row["label"] not in pair |
| 27 | + pair[row["label"]] = row |
| 28 | +for group in groups.values(): |
| 29 | + assert all( |
| 30 | + set(pair) == {"base", "head"} |
| 31 | + and pair["base"]["stdout_sha256"] == pair["head"]["stdout_sha256"] |
| 32 | + for pair in group.values() |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def quantile(values, q): |
| 37 | + ordered = sorted(values) |
| 38 | + position = (len(ordered) - 1) * q |
| 39 | + low = int(position) |
| 40 | + return ordered[low] + (ordered[min(low + 1, len(ordered) - 1)] - ordered[low]) * ( |
| 41 | + position - low |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def stats(group, metric): |
| 46 | + pairs = [group[i] for i in sorted(group)] |
| 47 | + base = [p["base"]["ms"] for p in pairs] |
| 48 | + head = [p["head"]["ms"] for p in pairs] |
| 49 | + q = 0.5 if metric == "p50" else 0.95 |
| 50 | + seed = ( |
| 51 | + int( |
| 52 | + hashlib.sha256( |
| 53 | + str( |
| 54 | + ( |
| 55 | + pairs[0]["base"]["case"], |
| 56 | + pairs[0]["base"]["mode"], |
| 57 | + pairs[0]["base"]["phase"], |
| 58 | + metric, |
| 59 | + ) |
| 60 | + ).encode() |
| 61 | + ).hexdigest()[:12], |
| 62 | + 16, |
| 63 | + ) |
| 64 | + ^ config["performance_decision"]["bootstrap_seed"] |
| 65 | + ) |
| 66 | + rng = random.Random(seed) |
| 67 | + boot = [] |
| 68 | + for _ in range(config["performance_decision"]["bootstrap_trials"]): |
| 69 | + indices = [rng.randrange(len(pairs)) for _ in pairs] |
| 70 | + boot.append( |
| 71 | + quantile([head[i] for i in indices], q) |
| 72 | + - quantile([base[i] for i in indices], q) |
| 73 | + ) |
| 74 | + return { |
| 75 | + "base_ms": quantile(base, q), |
| 76 | + "head_ms": quantile(head, q), |
| 77 | + "delta_ms": quantile(head, q) - quantile(base, q), |
| 78 | + "lower_95_ms": quantile(boot, 0.025), |
| 79 | + "upper_95_ms": quantile(boot, 0.95), |
| 80 | + "upper_975_ms": quantile(boot, 0.975), |
| 81 | + "repeatability_95_ms": quantile([abs(x) for x in boot], 0.95), |
| 82 | + "n": len(pairs), |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +result = [] |
| 87 | +passed = True |
| 88 | +for provider in config["providers"]: |
| 89 | + for count in config["history_counts"]: |
| 90 | + case = f"{provider}-{count}" |
| 91 | + for mode in config["modes"]: |
| 92 | + cell = {"case": case, "mode": mode, "metrics": {}} |
| 93 | + for metric in ["p50", "p95"]: |
| 94 | + aa = stats(groups[(case, mode, "aa")], metric) |
| 95 | + ab = stats(groups[(case, mode, "ab")], metric) |
| 96 | + resolution = aa["repeatability_95_ms"] |
| 97 | + cap = min( |
| 98 | + config["performance_decision"]["max_resolution_ms"], |
| 99 | + config["performance_decision"]["max_resolution_fraction"] |
| 100 | + * aa["base_ms"], |
| 101 | + ) |
| 102 | + control_ok = resolution <= cap |
| 103 | + regression_detected = ab["lower_95_ms"] > 0 |
| 104 | + equivalent = ( |
| 105 | + control_ok |
| 106 | + and not regression_detected |
| 107 | + and ab["upper_95_ms"] <= resolution |
| 108 | + ) |
| 109 | + cell["metrics"][metric] = { |
| 110 | + "aa": aa, |
| 111 | + "ab": ab, |
| 112 | + "resolution_ms": resolution, |
| 113 | + "resolution_cap_ms": cap, |
| 114 | + "control_qualified": control_ok, |
| 115 | + "regression_detected": regression_detected, |
| 116 | + "no_measurable_regression": equivalent, |
| 117 | + } |
| 118 | + passed = passed and equivalent |
| 119 | + result.append(cell) |
| 120 | +report = { |
| 121 | + "baseline": config["baseline"], |
| 122 | + "candidate": config["candidate_head"], |
| 123 | + "series": config["series"], |
| 124 | + "rows": len(rows), |
| 125 | + "passed": passed, |
| 126 | + "interpretation": "equivalence only within qualified A/A measurement resolution; not a universal exact timing guarantee", |
| 127 | + "cells": result, |
| 128 | +} |
| 129 | +(ROOT / "performance-analysis.json").write_text(json.dumps(report, indent=2)) |
| 130 | +print(json.dumps(report, indent=2)) |
| 131 | +raise SystemExit(0 if passed else 1) |
0 commit comments