xref: /OK3568_Linux_fs/buildroot/utils/size-stats-compare (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/usr/bin/env python3
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun# Copyright (C) 2016 Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun# This program is free software; you can redistribute it and/or modify
6*4882a593Smuzhiyun# it under the terms of the GNU General Public License as published by
7*4882a593Smuzhiyun# the Free Software Foundation; either version 2 of the License, or
8*4882a593Smuzhiyun# (at your option) any later version.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun# This program is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun# but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*4882a593Smuzhiyun# General Public License for more details.
14*4882a593Smuzhiyun#
15*4882a593Smuzhiyun# You should have received a copy of the GNU General Public License
16*4882a593Smuzhiyun# along with this program; if not, write to the Free Software
17*4882a593Smuzhiyun# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# TODO (improvements)
20*4882a593Smuzhiyun# - support K,M,G size suffixes for threshold
21*4882a593Smuzhiyun# - output CSV file in addition to stdout reporting
22*4882a593Smuzhiyun
23*4882a593Smuzhiyunimport csv
24*4882a593Smuzhiyunimport argparse
25*4882a593Smuzhiyunimport sys
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun
28*4882a593Smuzhiyundef read_file_size_csv(inputf, detail=None):
29*4882a593Smuzhiyun    """Extract package or file sizes from CSV file into size dictionary"""
30*4882a593Smuzhiyun    sizes = {}
31*4882a593Smuzhiyun    reader = csv.reader(inputf)
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun    header = next(reader)
34*4882a593Smuzhiyun    if header[0] != 'File name' or header[1] != 'Package name' or \
35*4882a593Smuzhiyun       header[2] != 'File size' or header[3] != 'Package size':
36*4882a593Smuzhiyun        print(("Input file %s does not contain the expected header. Are you "
37*4882a593Smuzhiyun               "sure this file corresponds to the file-size-stats.csv "
38*4882a593Smuzhiyun               "file created by 'make graph-size'?") % inputf.name)
39*4882a593Smuzhiyun        sys.exit(1)
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun    for row in reader:
42*4882a593Smuzhiyun        if detail:
43*4882a593Smuzhiyun            sizes[row[0]] = int(row[2])
44*4882a593Smuzhiyun        else:
45*4882a593Smuzhiyun            sizes[row[1]] = int(row[3])
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun    return sizes
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun
50*4882a593Smuzhiyundef compare_sizes(old, new):
51*4882a593Smuzhiyun    """Return delta/added/removed dictionaries based on two input size
52*4882a593Smuzhiyun    dictionaries"""
53*4882a593Smuzhiyun    delta = {}
54*4882a593Smuzhiyun    oldkeys = set(old.keys())
55*4882a593Smuzhiyun    newkeys = set(new.keys())
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun    # packages/files in both
58*4882a593Smuzhiyun    for entry in newkeys.intersection(oldkeys):
59*4882a593Smuzhiyun        delta[entry] = ('', new[entry] - old[entry])
60*4882a593Smuzhiyun    # packages/files only in new
61*4882a593Smuzhiyun    for entry in newkeys.difference(oldkeys):
62*4882a593Smuzhiyun        delta[entry] = ('added', new[entry])
63*4882a593Smuzhiyun    # packages/files only in old
64*4882a593Smuzhiyun    for entry in oldkeys.difference(newkeys):
65*4882a593Smuzhiyun        delta[entry] = ('removed', -old[entry])
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun    return delta
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun
70*4882a593Smuzhiyundef print_results(result, threshold):
71*4882a593Smuzhiyun    """Print the given result dictionary sorted by size, ignoring any entries
72*4882a593Smuzhiyun    below or equal to threshold"""
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun    from six import iteritems
75*4882a593Smuzhiyun    list_result = list(iteritems(result))
76*4882a593Smuzhiyun    # result is a dictionary: name -> (flag, size difference)
77*4882a593Smuzhiyun    # list_result is a list of tuples: (name, (flag, size difference))
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun    for entry in sorted(list_result, key=lambda entry: entry[1][1]):
80*4882a593Smuzhiyun        if threshold is not None and abs(entry[1][1]) <= threshold:
81*4882a593Smuzhiyun            continue
82*4882a593Smuzhiyun        print('%12s %7s %s' % (entry[1][1], entry[1][0], entry[0]))
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun# main #########################################################################
86*4882a593Smuzhiyun
87*4882a593Smuzhiyundescription = """
88*4882a593SmuzhiyunCompare rootfs size between Buildroot compilations, for example after changing
89*4882a593Smuzhiyunconfiguration options or after switching to another Buildroot release.
90*4882a593Smuzhiyun
91*4882a593SmuzhiyunThis script compares the file-size-stats.csv file generated by 'make graph-size'
92*4882a593Smuzhiyunwith the corresponding file from another Buildroot compilation.
93*4882a593SmuzhiyunThe size differences can be reported per package or per file.
94*4882a593SmuzhiyunSize differences smaller or equal than a given threshold can be ignored.
95*4882a593Smuzhiyun"""
96*4882a593Smuzhiyun
97*4882a593Smuzhiyunparser = argparse.ArgumentParser(description=description,
98*4882a593Smuzhiyun                                 formatter_class=argparse.RawDescriptionHelpFormatter)
99*4882a593Smuzhiyun
100*4882a593Smuzhiyunparser.add_argument('-d', '--detail', action='store_true',
101*4882a593Smuzhiyun                    help='''report differences for individual files rather than
102*4882a593Smuzhiyun                            packages''')
103*4882a593Smuzhiyunparser.add_argument('-t', '--threshold', type=int,
104*4882a593Smuzhiyun                    help='''ignore size differences smaller or equal than this
105*4882a593Smuzhiyun                            value (bytes)''')
106*4882a593Smuzhiyunparser.add_argument('old_file_size_csv', type=argparse.FileType('r'),
107*4882a593Smuzhiyun                    metavar='old-file-size-stats.csv',
108*4882a593Smuzhiyun                    help="""old CSV file with file and package size statistics,
109*4882a593Smuzhiyun                            generated by 'make graph-size'""")
110*4882a593Smuzhiyunparser.add_argument('new_file_size_csv', type=argparse.FileType('r'),
111*4882a593Smuzhiyun                    metavar='new-file-size-stats.csv',
112*4882a593Smuzhiyun                    help='new CSV file with file and package size statistics')
113*4882a593Smuzhiyunargs = parser.parse_args()
114*4882a593Smuzhiyun
115*4882a593Smuzhiyunif args.detail:
116*4882a593Smuzhiyun    keyword = 'file'
117*4882a593Smuzhiyunelse:
118*4882a593Smuzhiyun    keyword = 'package'
119*4882a593Smuzhiyun
120*4882a593Smuzhiyunold_sizes = read_file_size_csv(args.old_file_size_csv, args.detail)
121*4882a593Smuzhiyunnew_sizes = read_file_size_csv(args.new_file_size_csv, args.detail)
122*4882a593Smuzhiyun
123*4882a593Smuzhiyundelta = compare_sizes(old_sizes, new_sizes)
124*4882a593Smuzhiyun
125*4882a593Smuzhiyunprint('Size difference per %s (bytes), threshold = %s' % (keyword, args.threshold))
126*4882a593Smuzhiyunprint(80*'-')
127*4882a593Smuzhiyunprint_results(delta, args.threshold)
128*4882a593Smuzhiyunprint(80*'-')
129*4882a593Smuzhiyunprint_results({'TOTAL': ('', sum(new_sizes.values()) - sum(old_sizes.values()))},
130*4882a593Smuzhiyun              threshold=None)
131