-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbloaty
executable file
·60 lines (54 loc) · 1.91 KB
/
bloaty
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
#!/usr/bin/env python3
# Small utility on top of cargo bloat.
import json
import subprocess
import sys
import os
command = sys.argv[1]
BASELINE_FILE = "bloaty.log"
def run_cargo_bloat():
completed = subprocess.run("cargo bloat --release --message-format json -n 0", shell=True, check=True, capture_output=True)
if completed.returncode != 0:
sys.exit(completed.stderr)
baseline = json.loads(completed.stdout)
data = {}
for f in baseline["functions"]:
name = f["name"]
size = f["size"]
if name in baseline:
data[name] += size
else:
data[name] = size
return {"functions":data}
def read_baseline():
if os.path.isfile(BASELINE_FILE):
with open(BASELINE_FILE, "r") as infile:
return json.load(infile)
else:
sys.exit("No baseline - run: bloaty save <description>")
if command == "save":
data = run_cargo_bloat()
data["bloatydescription"] = ' '.join(sys.argv[2:])
with open(BASELINE_FILE, "w") as outfile:
json_object = json.dumps(data, indent=4)
outfile.write(json_object)
elif command == "show":
baseline = read_baseline()
print(baseline["bloatydescription"])
elif command == "diff":
baseline = read_baseline()
print("# Comparing with " + baseline["bloatydescription"])
current = run_cargo_bloat()
total_diff_bytes = 0
for (name, current_size) in current["functions"].items():
if name in baseline["functions"]:
old_size = baseline["functions"][name]
diff_bytes = current_size - old_size
if diff_bytes != 0:
total_diff_bytes += diff_bytes
print(f"{name}: {diff_bytes} - old={old_size}, new={current_size}")
else:
print(f"New function: {name}, size={current_size}")
print(f"Total: {total_diff_bytes}")
else:
sys.exit(f"Unknown command: {command} - use save/show/diff")