1 #!/usr/bin/env vpython3
3 # Copyright 2021 The Chromium Authors
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 '''Implements Chrome-Fuchsia package binary size differ.'''
14 from binary_sizes import ReadPackageSizesJson
15 from binary_sizes import PACKAGES_SIZES_FILE
17 # Eng is not responsible for changes that cause "reasonable growth" if the
18 # uncompressed binary size does not grow.
19 # First-warning will fail the test if the uncompressed and compressed size
20 # grow, while always-fail will fail the test regardless of uncompressed growth
21 # (solely based on compressed growth).
22 _FIRST_WARNING_DELTA_BYTES = 12 * 1024 # 12 KiB
23 _ALWAYS_FAIL_DELTA_BYTES = 100 * 1024 # 100 KiB
24 _TRYBOT_DOC = 'https://chromium.googlesource.com/chromium/src/+/main/docs/speed/binary_size/fuchsia_binary_size_trybot.md'
27 ROLLER_SIZE_WARNING = 2
31 def ComputePackageDiffs(before_sizes_file, after_sizes_file, author=None):
32 '''Computes difference between after and before diff, for each package.'''
33 before_sizes = ReadPackageSizesJson(before_sizes_file)
34 after_sizes = ReadPackageSizesJson(after_sizes_file)
36 assert before_sizes.keys() == after_sizes.keys(), (
37 'Package files cannot'
38 ' be compared with different packages: '
39 '{} vs {}'.format(before_sizes.keys(), after_sizes.keys()))
41 growth = {'compressed': {}, 'uncompressed': {}}
44 for package_name in before_sizes:
45 growth['compressed'][package_name] = (after_sizes[package_name].compressed -
46 before_sizes[package_name].compressed)
47 growth['uncompressed'][package_name] = (
48 after_sizes[package_name].uncompressed -
49 before_sizes[package_name].uncompressed)
50 # Developers are only responsible if uncompressed increases.
51 if ((growth['compressed'][package_name] >= _FIRST_WARNING_DELTA_BYTES
52 and growth['uncompressed'][package_name] > 0)
53 # However, if compressed growth is unusually large, fail always.
54 or growth['compressed'][package_name] >= _ALWAYS_FAIL_DELTA_BYTES):
56 summary = ('Size check failed! The following package(s) are affected:'
58 status_code = SIZE_FAILURE
59 summary += (('- {} (compressed) grew by {} bytes (uncompressed growth:'
60 ' {} bytes).<br>').format(
61 package_name, growth['compressed'][package_name],
62 growth['uncompressed'][package_name]))
63 summary += ('Note that this bot compares growth against trunk, and is '
64 'not aware of CL chaining.<br>')
66 # Allow rollers to pass even with size increases. See crbug.com/1355914.
67 if author and '-autoroll' in author and status_code == SIZE_FAILURE:
68 summary = summary.replace('Size check failed! ', '')
70 'The following growth by an autoroller will be ignored:<br><br>' +
72 status_code = ROLLER_SIZE_WARNING
73 growth['status_code'] = status_code
74 summary += ('<br>See the following document for more information about'
75 ' this trybot:<br>{}'.format(_TRYBOT_DOC))
76 growth['summary'] = summary
78 # TODO(crbug.com/1266085): Investigate using these fields.
79 growth['archive_filenames'] = []
85 parser = argparse.ArgumentParser()
88 type=os.path.realpath,
90 help='Location of the build without the patch',
94 type=os.path.realpath,
96 help='Location of the build with the patch',
98 parser.add_argument('--author', help='Author of change')
101 type=os.path.realpath,
103 help='Output path for the trybot result .json file',
105 parser.add_argument('--verbose',
108 help='Enable verbose output')
109 args = parser.parse_args()
112 print('Fuchsia binary sizes')
113 print('Working directory', os.getcwd())
115 for var in vars(args):
116 print(' {}: {}'.format(var, getattr(args, var) or ''))
118 if not os.path.isdir(args.before_dir) or not os.path.isdir(args.after_dir):
120 'Could not find build output directory "{}" or "{}".'.format(
121 args.before_dir, args.after_dir))
124 before_sizes_file = os.path.join(args.before_dir, test_name,
126 after_sizes_file = os.path.join(args.after_dir, test_name,
128 if not os.path.isfile(before_sizes_file):
130 'Could not find before sizes file: "{}"'.format(before_sizes_file))
132 if not os.path.isfile(after_sizes_file):
134 'Could not find after sizes file: "{}"'.format(after_sizes_file))
136 test_completed = False
138 growth = ComputePackageDiffs(before_sizes_file,
141 test_completed = True
142 with open(args.results_path, 'wt') as results_file:
143 json.dump(growth, results_file)
145 _, value, trace = sys.exc_info()
146 traceback.print_tb(trace)
149 return 0 if test_completed else 1
152 if __name__ == '__main__':