Skip to content

Commit d2ef087

Browse files
Add script to diff two code size reports
This takes 2 JSON files containing the code size information and computed the difference between them. It then pretty-prints the sizes for files that are different along with the difference and percentage difference in size. Signed-off-by: David Horstmann <david.horstmann@arm.com>
1 parent ea8f993 commit d2ef087

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

scripts/diff_code_size.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
"""Examine 2 code size reports and print the difference between them.
3+
"""
4+
5+
# Copyright The Mbed TLS Contributors
6+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7+
8+
import json
9+
import sys
10+
11+
def load_sizes(json_file):
12+
with open(json_file) as f:
13+
json_sizes = f.read()
14+
15+
sizes = json.loads(json_sizes)
16+
return sizes
17+
18+
def generate_diff_table(sizes_a, sizes_b):
19+
table = []
20+
total_size_a = 0
21+
total_size_b = 0
22+
23+
for file in sizes_a:
24+
size_a = (sizes_a[file]['text']
25+
+ sizes_a[file]['data']
26+
+ sizes_a[file]['bss'])
27+
total_size_a += size_a
28+
29+
if file in sizes_b:
30+
size_b = (sizes_b[file]['text']
31+
+ sizes_b[file]['data']
32+
+ sizes_b[file]['bss'])
33+
size_diff = size_b - size_a
34+
35+
if size_diff != 0:
36+
table.append((file.split('.')[0], size_a, size_b, size_diff,
37+
(size_diff * 100.0 / size_a)))
38+
else:
39+
# This file is only in A, so there's a code size decrease
40+
table.append((file.split('.')[0], size_a, 0, -size_a, -100.0))
41+
42+
for file in sizes_b:
43+
size_b = (sizes_b[file]['text']
44+
+ sizes_b[file]['data']
45+
+ sizes_b[file]['bss'])
46+
total_size_b += size_b
47+
48+
if file not in sizes_a:
49+
# This file is only in B, so there's a code size increase
50+
table.append((file.split('.')[0], 0, size_b, size_b, 100.0))
51+
52+
total_size_diff = total_size_b - total_size_a
53+
table.append(('TOTAL', total_size_a, total_size_b, total_size_diff,
54+
(total_size_diff * 100.0 / total_size_a)))
55+
56+
return table
57+
58+
def display_diff_table(table):
59+
table_line_format = '{:<40} {:>8} {:>8} {:>+8} {:>+8.2f}%'
60+
61+
print('{:<40} {:>8} {:>8} {:>8} {:>9}'.format(
62+
'Module', 'Old', 'New', 'Delta', '% Delta'))
63+
64+
for line in table:
65+
print(table_line_format.format(*line))
66+
67+
def main():
68+
if len(sys.argv) < 3:
69+
print('Error: Less than 2 JSON files supplied.', file=sys.stderr)
70+
sys.exit(1)
71+
72+
sizes_a = load_sizes(sys.argv[1])
73+
sizes_b = load_sizes(sys.argv[2])
74+
75+
display_diff_table(generate_diff_table(sizes_a, sizes_b))
76+
77+
if __name__ == '__main__':
78+
main()

0 commit comments

Comments
 (0)