-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathcompare-file-sizes.py
More file actions
171 lines (141 loc) · 5.41 KB
/
compare-file-sizes.py
File metadata and controls
171 lines (141 loc) · 5.41 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright the Vortex contributors
"""Compare file sizes between base and HEAD and generate markdown report."""
import argparse
import json
import sys
from collections import defaultdict
def format_size(size_bytes: int) -> str:
"""Format bytes as human-readable size."""
if size_bytes >= 1024**3:
return f"{size_bytes / (1024**3):.2f} GB"
elif size_bytes >= 1024**2:
return f"{size_bytes / (1024**2):.2f} MB"
elif size_bytes >= 1024:
return f"{size_bytes / 1024:.2f} KB"
else:
return f"{size_bytes} B"
def format_change(change_bytes: int) -> str:
"""Format byte change with sign."""
sign = "+" if change_bytes > 0 else ""
return f"{sign}{format_size(abs(change_bytes))}"
def format_pct_change(pct: float) -> str:
"""Format percentage change with sign."""
sign = "+" if pct > 0 else ""
return f"{sign}{pct:.1f}%"
def main():
parser = argparse.ArgumentParser(description="Compare file sizes between base and HEAD")
parser.add_argument("base_file", help="Base JSONL file")
parser.add_argument("head_file", help="HEAD JSONL file")
args = parser.parse_args()
# Load base and head data
base_data = {}
try:
with open(args.base_file) as f:
for line in f:
record = json.loads(line)
# Support old records without scale_factor (default to "1.0")
scale_factor = record.get("scale_factor", "1.0")
key = (record["benchmark"], scale_factor, record["format"], record["file"])
base_data[key] = record["size_bytes"]
except FileNotFoundError:
print("_Base file sizes not found._")
sys.exit(0)
head_data = {}
try:
with open(args.head_file) as f:
for line in f:
record = json.loads(line)
scale_factor = record.get("scale_factor", "1.0")
key = (record["benchmark"], scale_factor, record["format"], record["file"])
head_data[key] = record["size_bytes"]
except FileNotFoundError:
print("_HEAD file sizes not found._")
sys.exit(0)
# Compare sizes
comparisons = []
format_totals = defaultdict(lambda: {"base": 0, "head": 0})
all_keys = set(base_data.keys()) | set(head_data.keys())
for key in all_keys:
benchmark, scale_factor, fmt, file_name = key
base_size = base_data.get(key, 0)
head_size = head_data.get(key, 0)
format_totals[fmt]["base"] += base_size
format_totals[fmt]["head"] += head_size
change = head_size - base_size
if change == 0:
continue
if base_size > 0:
pct_change = (head_size / base_size - 1) * 100
elif head_size > 0:
pct_change = float("inf")
else:
pct_change = 0
comparisons.append(
{
"file": file_name,
"scale_factor": scale_factor,
"format": fmt,
"base_size": base_size,
"head_size": head_size,
"change": change,
"pct_change": pct_change,
}
)
if not comparisons:
print("_No file size changes detected._")
return
# Sort by pct_change descending (largest increases first)
comparisons.sort(key=lambda x: x["pct_change"], reverse=True)
# Build summary line for collapsible header
total_base = sum(format_totals[fmt]["base"] for fmt in format_totals)
total_head = sum(format_totals[fmt]["head"] for fmt in format_totals)
if total_base > 0:
overall_pct = (total_head / total_base - 1) * 100
overall_pct_str = format_pct_change(overall_pct)
else:
overall_pct_str = "new"
increases = sum(1 for c in comparisons if c["change"] > 0)
decreases = sum(1 for c in comparisons if c["change"] < 0)
# Output collapsible markdown
print("<details>")
summary = (
f"<summary>File Size Changes ({len(comparisons)} files changed, "
f"{overall_pct_str} overall, {increases}↑ {decreases}↓)</summary>"
)
print(summary)
print("")
print("<br>")
print("")
# Output markdown table
print("| File | Scale | Format | Base | HEAD | Change | % |")
print("|------|-------|--------|------|------|--------|---|")
for comp in comparisons:
pct_str = format_pct_change(comp["pct_change"]) if comp["pct_change"] != float("inf") else "new"
base_str = format_size(comp["base_size"]) if comp["base_size"] > 0 else "-"
print(
f"| {comp['file']} | {comp['scale_factor']} | {comp['format']} | {base_str} | "
f"{format_size(comp['head_size'])} | {format_change(comp['change'])} | {pct_str} |"
)
# Output totals
print("")
print("**Totals:**")
for fmt in sorted(format_totals.keys()):
totals = format_totals[fmt]
base_total = totals["base"]
head_total = totals["head"]
if base_total > 0:
total_pct = (head_total / base_total - 1) * 100
pct_str = f" ({format_pct_change(total_pct)})"
else:
pct_str = ""
print(f"- {fmt}: {format_size(base_total)} \u2192 {format_size(head_total)}{pct_str}")
print("")
print("</details>")
if __name__ == "__main__":
main()