File: format_benchmark.py

package info (click to toggle)
sqlglot 28.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,672 kB
  • sloc: python: 84,517; sql: 22,534; makefile: 48
file content (81 lines) | stat: -rwxr-xr-x 2,871 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Format benchmark comparison output with visual indicators for GitHub markdown."""

import re
import sys


def format_benchmark_output(content):
    """Add visual formatting to benchmark comparison output."""
    lines = content.split("\n")
    formatted_lines = []

    for line in lines:
        # Skip empty lines and headers
        if not line.strip() or line.startswith("|") and "---" in line:
            formatted_lines.append(line)
            continue

        # Process benchmark result lines
        if "|" in line and ("faster" in line or "slower" in line):
            # Extract the speed factor (e.g., "1.23x faster" or "1.10x slower")
            speed_match = re.search(r"(\d+\.\d+)x\s+(faster|slower)", line)
            if speed_match:
                factor = float(speed_match.group(1))
                direction = speed_match.group(2)

                # Add visual indicators based on performance
                if direction == "faster":
                    # Green indicator for faster
                    if factor >= 2.0:
                        indicator = "🟢🟢"  # Double green for 2x+ faster
                    elif factor >= 1.1:
                        indicator = "🟢"  # Single green for 1.1x+ faster
                    else:
                        indicator = "⚪"  # White for marginal improvement
                    formatted_text = f"{indicator} **{speed_match.group(0)}**"
                else:
                    # Red indicator for slower
                    if factor >= 2.0:
                        indicator = "🔴🔴"  # Double red for 2x+ slower
                    elif factor >= 1.1:
                        indicator = "🔴"  # Single red for 1.1x+ slower
                    else:
                        indicator = "⚪"  # White for marginal slowdown
                    formatted_text = f"{indicator} **{speed_match.group(0)}**"

                # Replace the original text with formatted version
                line = line.replace(speed_match.group(0), formatted_text)
            elif "not significant" in line:
                # Add neutral indicator for non-significant changes
                line = re.sub(r"not significant", "⚪ not significant", line)

        formatted_lines.append(line)

    return "\n".join(formatted_lines)


def main():
    if len(sys.argv) != 2:
        print("Usage: python format_benchmark.py <input_file>")
        sys.exit(1)

    input_file = sys.argv[1]

    try:
        with open(input_file, "r") as f:
            content = f.read()

        formatted = format_benchmark_output(content)
        print(formatted)

    except FileNotFoundError:
        print(f"Error: File '{input_file}' not found")
        sys.exit(1)
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()