|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.11" |
| 4 | +# dependencies = [] |
| 5 | +# /// |
| 6 | + |
| 7 | +# SPDX-License-Identifier: Apache-2.0 |
| 8 | +# SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 9 | + |
| 10 | +"""Compare file sizes between base and HEAD and generate markdown report.""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import sys |
| 15 | +from collections import defaultdict |
| 16 | + |
| 17 | + |
| 18 | +def format_size(size_bytes: int) -> str: |
| 19 | + """Format bytes as human-readable size.""" |
| 20 | + if size_bytes >= 1024**3: |
| 21 | + return f"{size_bytes / (1024**3):.2f} GB" |
| 22 | + elif size_bytes >= 1024**2: |
| 23 | + return f"{size_bytes / (1024**2):.2f} MB" |
| 24 | + elif size_bytes >= 1024: |
| 25 | + return f"{size_bytes / 1024:.2f} KB" |
| 26 | + else: |
| 27 | + return f"{size_bytes} B" |
| 28 | + |
| 29 | + |
| 30 | +def format_change(change_bytes: int) -> str: |
| 31 | + """Format byte change with sign.""" |
| 32 | + sign = "+" if change_bytes > 0 else "" |
| 33 | + return f"{sign}{format_size(abs(change_bytes))}" |
| 34 | + |
| 35 | + |
| 36 | +def format_pct_change(pct: float) -> str: |
| 37 | + """Format percentage change with sign.""" |
| 38 | + sign = "+" if pct > 0 else "" |
| 39 | + return f"{sign}{pct:.1f}%" |
| 40 | + |
| 41 | + |
| 42 | +def main(): |
| 43 | + parser = argparse.ArgumentParser(description="Compare file sizes between base and HEAD") |
| 44 | + parser.add_argument("base_file", help="Base JSONL file") |
| 45 | + parser.add_argument("head_file", help="HEAD JSONL file") |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + # Load base and head data |
| 49 | + base_data = {} |
| 50 | + try: |
| 51 | + with open(args.base_file) as f: |
| 52 | + for line in f: |
| 53 | + record = json.loads(line) |
| 54 | + # Support old records without scale_factor (default to "1.0") |
| 55 | + scale_factor = record.get("scale_factor", "1.0") |
| 56 | + key = (record["benchmark"], scale_factor, record["format"], record["file"]) |
| 57 | + base_data[key] = record["size_bytes"] |
| 58 | + except FileNotFoundError: |
| 59 | + print("_Base file sizes not found._") |
| 60 | + sys.exit(0) |
| 61 | + |
| 62 | + head_data = {} |
| 63 | + try: |
| 64 | + with open(args.head_file) as f: |
| 65 | + for line in f: |
| 66 | + record = json.loads(line) |
| 67 | + scale_factor = record.get("scale_factor", "1.0") |
| 68 | + key = (record["benchmark"], scale_factor, record["format"], record["file"]) |
| 69 | + head_data[key] = record["size_bytes"] |
| 70 | + except FileNotFoundError: |
| 71 | + print("_HEAD file sizes not found._") |
| 72 | + sys.exit(0) |
| 73 | + |
| 74 | + # Compare sizes |
| 75 | + comparisons = [] |
| 76 | + format_totals = defaultdict(lambda: {"base": 0, "head": 0}) |
| 77 | + |
| 78 | + all_keys = set(base_data.keys()) | set(head_data.keys()) |
| 79 | + for key in all_keys: |
| 80 | + benchmark, scale_factor, fmt, file_name = key |
| 81 | + base_size = base_data.get(key, 0) |
| 82 | + head_size = head_data.get(key, 0) |
| 83 | + |
| 84 | + format_totals[fmt]["base"] += base_size |
| 85 | + format_totals[fmt]["head"] += head_size |
| 86 | + |
| 87 | + change = head_size - base_size |
| 88 | + if change == 0: |
| 89 | + continue |
| 90 | + |
| 91 | + if base_size > 0: |
| 92 | + pct_change = (head_size / base_size - 1) * 100 |
| 93 | + elif head_size > 0: |
| 94 | + pct_change = float("inf") |
| 95 | + else: |
| 96 | + pct_change = 0 |
| 97 | + |
| 98 | + comparisons.append( |
| 99 | + { |
| 100 | + "file": file_name, |
| 101 | + "scale_factor": scale_factor, |
| 102 | + "format": fmt, |
| 103 | + "base_size": base_size, |
| 104 | + "head_size": head_size, |
| 105 | + "change": change, |
| 106 | + "pct_change": pct_change, |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + if not comparisons: |
| 111 | + print("_No file size changes detected._") |
| 112 | + return |
| 113 | + |
| 114 | + # Sort by pct_change descending (largest increases first) |
| 115 | + comparisons.sort(key=lambda x: x["pct_change"], reverse=True) |
| 116 | + |
| 117 | + # Output markdown table |
| 118 | + print("| File | Scale | Format | Base | HEAD | Change | % |") |
| 119 | + print("|------|-------|--------|------|------|--------|---|") |
| 120 | + |
| 121 | + for comp in comparisons: |
| 122 | + pct_str = format_pct_change(comp["pct_change"]) if comp["pct_change"] != float("inf") else "new" |
| 123 | + base_str = format_size(comp["base_size"]) if comp["base_size"] > 0 else "-" |
| 124 | + print( |
| 125 | + f"| {comp['file']} | {comp['scale_factor']} | {comp['format']} | {base_str} | " |
| 126 | + f"{format_size(comp['head_size'])} | {format_change(comp['change'])} | {pct_str} |" |
| 127 | + ) |
| 128 | + |
| 129 | + # Output totals |
| 130 | + print("") |
| 131 | + print("**Totals:**") |
| 132 | + for fmt in sorted(format_totals.keys()): |
| 133 | + totals = format_totals[fmt] |
| 134 | + base_total = totals["base"] |
| 135 | + head_total = totals["head"] |
| 136 | + if base_total > 0: |
| 137 | + total_pct = (head_total / base_total - 1) * 100 |
| 138 | + pct_str = f" ({format_pct_change(total_pct)})" |
| 139 | + else: |
| 140 | + pct_str = "" |
| 141 | + print(f"- {fmt}: {format_size(base_total)} \u2192 {format_size(head_total)}{pct_str}") |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments