-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathdiff_code_size.py
More file actions
executable file
·89 lines (68 loc) · 2.59 KB
/
diff_code_size.py
File metadata and controls
executable file
·89 lines (68 loc) · 2.59 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
#!/usr/bin/env python3
"""Examine 2 code size reports and print the difference between them.
"""
# Copyright The Mbed TLS Contributors
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
import json
import os
import sys
def load_sizes(json_file):
with open(json_file) as f:
json_sizes = f.read()
sizes = json.loads(json_sizes)
return sizes
def generate_diff_table(sizes_a, sizes_b):
table = []
total_size_a = 0
total_size_b = 0
for file in sizes_a:
size_a = (sizes_a[file]['text']
+ sizes_a[file]['data']
+ sizes_a[file]['bss'])
total_size_a += size_a
if file in sizes_b:
size_b = (sizes_b[file]['text']
+ sizes_b[file]['data']
+ sizes_b[file]['bss'])
size_diff = size_b - size_a
if size_diff != 0:
table.append((file.split('.')[0], size_a, size_b, size_diff,
(size_diff * 100.0 / size_a)))
else:
# This file is only in A, so there's a code size decrease
table.append((file.split('.')[0], size_a, 0, -size_a, -100.0))
for file in sizes_b:
size_b = (sizes_b[file]['text']
+ sizes_b[file]['data']
+ sizes_b[file]['bss'])
total_size_b += size_b
if file not in sizes_a:
# This file is only in B, so there's a code size increase
table.append((file.split('.')[0], 0, size_b, size_b, 100.0))
total_size_diff = total_size_b - total_size_a
table.append(('TOTAL', total_size_a, total_size_b, total_size_diff,
(total_size_diff * 100.0 / total_size_a)))
return table
def display_diff_table(table):
table_line_format = '{:<40} {:>8} {:>8} {:>+8} {:>+8.2f}%'
print('{:<40} {:>8} {:>8} {:>8} {:>9}'.format(
'Module', 'Old', 'New', 'Delta', '% Delta'))
for line in table:
print(table_line_format.format(*line))
def main():
if len(sys.argv) < 3:
print('Error: Less than 2 JSON files / build directories supplied.', file=sys.stderr)
sys.exit(1)
file_a = sys.argv[1]
file_b = sys.argv[2]
# If the arguments are build directories, find the JSON
# code-size report in core/code_size.json
if os.path.isdir(file_a):
file_a = file_a + '/core/code_size.json'
if os.path.isdir(file_b):
file_b = file_b + '/core/code_size.json'
sizes_a = load_sizes(file_a)
sizes_b = load_sizes(file_b)
display_diff_table(generate_diff_table(sizes_a, sizes_b))
if __name__ == '__main__':
main()