|
| 1 | +import pathlib, re, sys |
| 2 | + |
| 3 | +try: |
| 4 | + p = pathlib.Path("comparison.md") |
| 5 | + if not p.exists(): |
| 6 | + print("comparison.md not found, skipping post-processing.") |
| 7 | + sys.exit(0) |
| 8 | + |
| 9 | + lines = p.read_text(encoding="utf-8").splitlines() |
| 10 | + processed_lines = [] |
| 11 | + in_code = False |
| 12 | + |
| 13 | + def strip_worker_suffix(text: str) -> str: |
| 14 | + # Also strip "Benchmark" prefix if it somehow sneaked in |
| 15 | + text = re.sub(r"^Benchmark", "", text) |
| 16 | + return re.sub(r"(\S+?)-\d+(\s|$)", r"\1\2", text) |
| 17 | + |
| 18 | + def get_icon(diff_val: float) -> str: |
| 19 | + if diff_val > 10: |
| 20 | + return "🐌" |
| 21 | + if diff_val < -10: |
| 22 | + return "🚀" |
| 23 | + return "➡️" |
| 24 | + |
| 25 | + def clean_superscripts(text: str) -> str: |
| 26 | + return re.sub(r"[¹²³⁴⁵⁶⁷⁸⁹⁰]", "", text) |
| 27 | + |
| 28 | + def parse_val(token: str): |
| 29 | + if "%" in token or "=" in token: |
| 30 | + return None |
| 31 | + token = clean_superscripts(token) |
| 32 | + token = token.split("±")[0].strip() |
| 33 | + token = token.split("(")[0].strip() |
| 34 | + if not token: |
| 35 | + return None |
| 36 | + |
| 37 | + m = re.match(r"^([-+]?\d*\.?\d+)([a-zA-Zµ]+)?$", token) |
| 38 | + if not m: |
| 39 | + return None |
| 40 | + try: |
| 41 | + val = float(m.group(1)) |
| 42 | + except ValueError: |
| 43 | + return None |
| 44 | + suffix = (m.group(2) or "").replace("µ", "u") |
| 45 | + |
| 46 | + if suffix in ["n", "ns"]: return val * 1e-9 |
| 47 | + if suffix in ["u", "us"]: return val * 1e-6 |
| 48 | + if suffix in ["m", "ms"]: return val * 1e-3 |
| 49 | + if suffix == "s": return val |
| 50 | + if suffix == "": return val # Handle unitless numbers (e.g. counts) if any |
| 51 | + |
| 52 | + # If we reach here, it's an unexpected unit |
| 53 | + raise ValueError(f"Unexpected unit: {suffix}") |
| 54 | + |
| 55 | + def extract_two_numbers(tokens): |
| 56 | + found = [] |
| 57 | + for t in tokens[1:]: # skip name |
| 58 | + if t in {"±", "∞", "~", "│", "|"}: |
| 59 | + continue |
| 60 | + if "%" in t or "=" in t: |
| 61 | + continue |
| 62 | + # Skip p=... n=... |
| 63 | + if t.startswith("p=") or t.startswith("n="): |
| 64 | + continue |
| 65 | + |
| 66 | + val = parse_val(t) |
| 67 | + if val is not None: |
| 68 | + found.append(val) |
| 69 | + if len(found) == 2: |
| 70 | + break |
| 71 | + return found |
| 72 | + |
| 73 | + # Pass 0: Calculate widths |
| 74 | + max_content_width = 0 |
| 75 | + |
| 76 | + for line in lines: |
| 77 | + if line.strip().startswith("```"): |
| 78 | + in_code = not in_code |
| 79 | + continue |
| 80 | + if not in_code: |
| 81 | + continue |
| 82 | + |
| 83 | + if re.match(r"^\s*[¹²³⁴⁵⁶⁷⁸⁹⁰]", line) or re.search(r"need\s*>?=\s*\d+\s+samples", line): |
| 84 | + continue |
| 85 | + if not line.strip() or line.strip().startswith(("goos:", "goarch:", "pkg:", "cpu:")): |
| 86 | + continue |
| 87 | + if "│" in line and ("vs base" in line or "old" in line or "new" in line): |
| 88 | + continue |
| 89 | + |
| 90 | + curr_line = strip_worker_suffix(line).rstrip() |
| 91 | + w = len(curr_line) |
| 92 | + if w > max_content_width: |
| 93 | + max_content_width = w |
| 94 | + |
| 95 | + diff_col_start = max_content_width - 13 |
| 96 | + # User asked to shift first column by 8 chars. |
| 97 | + # This means the whole table shifts right? Or just the text inside? |
| 98 | + # I'll prepend 8 spaces to the final output line. |
| 99 | + INDENT = " " |
| 100 | + |
| 101 | + for line in lines: |
| 102 | + if line.strip().startswith("```"): |
| 103 | + in_code = not in_code |
| 104 | + processed_lines.append(line) |
| 105 | + continue |
| 106 | + |
| 107 | + if not in_code: |
| 108 | + processed_lines.append(line) |
| 109 | + continue |
| 110 | + |
| 111 | + # Footnotes |
| 112 | + if re.match(r"^\s*[¹²³⁴⁵⁶⁷⁸⁹⁰]", line) or re.search(r"need\s*>?=\s*\d+\s+samples", line): |
| 113 | + processed_lines.append(INDENT + line) |
| 114 | + continue |
| 115 | + |
| 116 | + # Header |
| 117 | + if "│" in line and ("vs base" in line or "old" in line or "new" in line): |
| 118 | + stripped_header = line.rstrip().rstrip("│").rstrip() |
| 119 | + stripped_header = re.sub(r"\s+Diff\s*$", "", stripped_header, flags=re.IGNORECASE) |
| 120 | + |
| 121 | + if len(stripped_header) < diff_col_start: |
| 122 | + new_header = stripped_header + " " * (diff_col_start - len(stripped_header)) |
| 123 | + else: |
| 124 | + new_header = stripped_header + " " |
| 125 | + |
| 126 | + if "vs base" in line: |
| 127 | + new_header += "Diff" |
| 128 | + |
| 129 | + new_header += "│" |
| 130 | + processed_lines.append(INDENT + new_header) |
| 131 | + continue |
| 132 | + |
| 133 | + # Metadata |
| 134 | + if not line.strip() or line.strip().startswith(("goos:", "goarch:", "pkg:", "cpu:")): |
| 135 | + processed_lines.append(INDENT + line) |
| 136 | + continue |
| 137 | + |
| 138 | + # Data Lines |
| 139 | + line = strip_worker_suffix(line) |
| 140 | + tokens = line.split() |
| 141 | + if not tokens: |
| 142 | + processed_lines.append(INDENT + line) |
| 143 | + continue |
| 144 | + |
| 145 | + numbers = extract_two_numbers(tokens) |
| 146 | + |
| 147 | + def append_aligned(left_part, content): |
| 148 | + if len(left_part) < diff_col_start: |
| 149 | + aligned = left_part + " " * (diff_col_start - len(left_part)) |
| 150 | + else: |
| 151 | + aligned = left_part + " " |
| 152 | + return f"{INDENT}{aligned}{content}" |
| 153 | + |
| 154 | + # Geomean |
| 155 | + |
| 156 | + if len(numbers) == 2 and numbers[0] != 0: |
| 157 | + diff_val = (numbers[1] - numbers[0]) / numbers[0] * 100 |
| 158 | + icon = get_icon(diff_val) |
| 159 | + left = line.rstrip() |
| 160 | + processed_lines.append(append_aligned(left, f"{diff_val:+.2f}% {icon}")) |
| 161 | + continue |
| 162 | + |
| 163 | + processed_lines.append(INDENT + line) |
| 164 | + |
| 165 | + p.write_text("\n".join(processed_lines) + "\n", encoding="utf-8") |
| 166 | + |
| 167 | +except Exception as e: |
| 168 | + print(f"Error post-processing comparison.md: {e}") |
| 169 | + sys.exit(1) |
0 commit comments