|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Summarize likely query-plan performance regression/improvement signals.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import re |
| 8 | +from pathlib import Path |
| 9 | +from typing import Dict, List, Optional |
| 10 | + |
| 11 | +EXECUTION_LINE = re.compile( |
| 12 | + r"runs=(?P<runs>\d+),\s*" |
| 13 | + r"totalMillis=(?P<total>\d+),\s*" |
| 14 | + r"averageMillis=(?P<avg>\d+),\s*" |
| 15 | + r"resultCount=(?P<results>\d+),\s*" |
| 16 | + r"softLimitMillis=(?P<soft_limit>\d+),\s*" |
| 17 | + r"softLimitReached=(?P<soft_reached>true|false),\s*" |
| 18 | + r"maxRunsReached=(?P<max_reached>true|false)" |
| 19 | +) |
| 20 | + |
| 21 | +DIFF_LINE = re.compile( |
| 22 | + r"^\s*(?P<level>unoptimized|optimized|executed):\s+" |
| 23 | + r".*structure=(?P<structure>[^,]+),\s*" |
| 24 | + r"joinAlgorithms=(?P<joins>[^,]+),\s*" |
| 25 | + r"actualResultSizes=(?P<actual>[^,]+),\s*" |
| 26 | + r"estimates=(?P<estimates>[^,\s]+)" |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def parse_execution_metrics(path: Path) -> Dict[str, int]: |
| 31 | + text = path.read_text(encoding="utf-8", errors="replace") |
| 32 | + matches = list(EXECUTION_LINE.finditer(text)) |
| 33 | + if not matches: |
| 34 | + raise ValueError(f"No execution verification line found in {path}") |
| 35 | + last = matches[-1] |
| 36 | + return { |
| 37 | + "runs": int(last.group("runs")), |
| 38 | + "total": int(last.group("total")), |
| 39 | + "avg": int(last.group("avg")), |
| 40 | + "results": int(last.group("results")), |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def parse_semantic_diff(path: Optional[Path]) -> List[Dict[str, str]]: |
| 45 | + if path is None: |
| 46 | + return [] |
| 47 | + text = path.read_text(encoding="utf-8", errors="replace") |
| 48 | + rows: List[Dict[str, str]] = [] |
| 49 | + for line in text.splitlines(): |
| 50 | + match = DIFF_LINE.search(line) |
| 51 | + if not match: |
| 52 | + continue |
| 53 | + rows.append( |
| 54 | + { |
| 55 | + "level": match.group("level"), |
| 56 | + "structure": match.group("structure").strip(), |
| 57 | + "joins": match.group("joins").strip(), |
| 58 | + "actual": match.group("actual").strip(), |
| 59 | + "estimates": match.group("estimates").strip(), |
| 60 | + } |
| 61 | + ) |
| 62 | + return rows |
| 63 | + |
| 64 | + |
| 65 | +def runtime_classification(delta_percent: Optional[float]) -> str: |
| 66 | + if delta_percent is None: |
| 67 | + return "unknown" |
| 68 | + if delta_percent <= -10.0: |
| 69 | + return "improvement" |
| 70 | + if delta_percent >= 10.0: |
| 71 | + return "regression" |
| 72 | + return "neutral" |
| 73 | + |
| 74 | + |
| 75 | +def find_diff(rows: List[Dict[str, str]], key: str) -> bool: |
| 76 | + return any(row[key] == "diff" for row in rows) |
| 77 | + |
| 78 | + |
| 79 | +def main() -> int: |
| 80 | + parser = argparse.ArgumentParser(description=__doc__) |
| 81 | + parser.add_argument("--baseline-log", required=True, type=Path) |
| 82 | + parser.add_argument("--candidate-log", required=True, type=Path) |
| 83 | + parser.add_argument("--comparison-log", type=Path) |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + baseline = parse_execution_metrics(args.baseline_log) |
| 87 | + candidate = parse_execution_metrics(args.candidate_log) |
| 88 | + semantic_rows = parse_semantic_diff(args.comparison_log) |
| 89 | + |
| 90 | + avg_base = baseline["avg"] |
| 91 | + avg_candidate = candidate["avg"] |
| 92 | + delta_percent: Optional[float] |
| 93 | + if avg_base == 0: |
| 94 | + delta_percent = None |
| 95 | + else: |
| 96 | + delta_percent = ((avg_candidate - avg_base) / avg_base) * 100.0 |
| 97 | + |
| 98 | + runtime_signal = runtime_classification(delta_percent) |
| 99 | + result_count_changed = baseline["results"] != candidate["results"] |
| 100 | + |
| 101 | + structure_changed = find_diff(semantic_rows, "structure") |
| 102 | + joins_changed = find_diff(semantic_rows, "joins") |
| 103 | + actual_changed = find_diff(semantic_rows, "actual") |
| 104 | + estimates_changed = find_diff(semantic_rows, "estimates") |
| 105 | + |
| 106 | + if result_count_changed: |
| 107 | + verdict = "semantic regression risk: result count changed; runtime delta not comparable" |
| 108 | + elif runtime_signal == "regression" and (structure_changed or joins_changed or actual_changed): |
| 109 | + verdict = "likely performance regression with plan-shape change" |
| 110 | + elif runtime_signal == "improvement" and (structure_changed or joins_changed): |
| 111 | + verdict = "likely performance improvement with optimizer-plan change" |
| 112 | + elif runtime_signal == "regression": |
| 113 | + verdict = "possible performance regression (no semantic diff evidence provided)" |
| 114 | + elif runtime_signal == "improvement": |
| 115 | + verdict = "possible performance improvement" |
| 116 | + elif structure_changed or joins_changed or actual_changed or estimates_changed: |
| 117 | + verdict = "plan changed but runtime signal neutral" |
| 118 | + else: |
| 119 | + verdict = "no clear regression/improvement signal" |
| 120 | + |
| 121 | + print("QueryPlanSnapshotCli regression summary") |
| 122 | + print(f"- baseline avgMillis: {avg_base}") |
| 123 | + print(f"- candidate avgMillis: {avg_candidate}") |
| 124 | + if delta_percent is None: |
| 125 | + print("- delta: n/a (baseline averageMillis=0)") |
| 126 | + else: |
| 127 | + print(f"- delta: {delta_percent:+.2f}%") |
| 128 | + print(f"- baseline resultCount: {baseline['results']}") |
| 129 | + print(f"- candidate resultCount: {candidate['results']}") |
| 130 | + print(f"- runtime signal: {runtime_signal}") |
| 131 | + |
| 132 | + if semantic_rows: |
| 133 | + print("- semantic diff:") |
| 134 | + for row in semantic_rows: |
| 135 | + print( |
| 136 | + " " |
| 137 | + f"{row['level']}: structure={row['structure']}, " |
| 138 | + f"joinAlgorithms={row['joins']}, " |
| 139 | + f"actualResultSizes={row['actual']}, " |
| 140 | + f"estimates={row['estimates']}" |
| 141 | + ) |
| 142 | + else: |
| 143 | + print("- semantic diff: not provided") |
| 144 | + |
| 145 | + print(f"- verdict: {verdict}") |
| 146 | + return 0 |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + raise SystemExit(main()) |
0 commit comments