OLD | NEW |
---|---|
(Empty) | |
1 import argparse | |
mikecase (-- gone --)
2015/10/14 20:16:44
I think you need to add the Chromium copyright not
rnephew (Wrong account)
2015/10/15 13:57:02
Done.
| |
2 import collections | |
3 import logging | |
4 import os | |
5 import sys | |
6 import zipfile | |
7 | |
8 from pylib import constants | |
9 | |
10 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib', | |
11 'common')) | |
12 import perf_tests_results_helper # pylint: disable=import-error | |
13 | |
14 | |
15 # TODO(rnephew): Add support for split apks. | |
16 class ApkSizeInfo(object): | |
17 | |
18 def __init__(self, path): | |
19 """ApkSizeInfo constructor. | |
20 | |
21 Args: | |
22 path: Path to apk. | |
23 """ | |
24 if not os.path.isfile(path): | |
25 raise IOError('Not a valid file path for apk.') | |
26 if not os.access(path, os.R_OK): | |
27 raise IOError('File is not readable.') | |
28 if not zipfile.is_zipfile(path): | |
29 raise TypeError('Not a valid apk') | |
30 logging.info('APK: %s', path) | |
31 self._apk_size = os.path.getsize(path) | |
32 self._zipfile = zipfile.ZipFile(path, 'r') | |
33 self._processed_files = None | |
34 self._compressed_size = 0 | |
35 self._total_files = 0 | |
36 self._uncompressed_size = 0 | |
37 self._ProcessFiles() | |
38 | |
39 def _ProcessFiles(self): | |
40 """Uses zipinfo to process apk file information.""" | |
41 INITIAL_FILE_EXTENSION_INFO = { | |
42 'number': 0, | |
43 'compressed_bytes': 0, | |
44 'uncompressed_bytes': 0 | |
45 } | |
46 self._processed_files = collections.defaultdict( | |
47 lambda: dict(INITIAL_FILE_EXTENSION_INFO)) | |
48 | |
49 for f in self._zipfile.infolist(): | |
50 _, file_ext = os.path.splitext(f.filename) | |
51 file_ext = file_ext[1:] # Drop . from extension. | |
52 | |
53 self._compressed_size += f.compress_size | |
54 self._total_files += 1 | |
55 self._uncompressed_size += f.file_size | |
56 self._processed_files[file_ext]['number'] += 1 | |
57 self._processed_files[file_ext]['compressed_bytes'] += f.compress_size | |
58 self._processed_files[file_ext]['uncompressed_bytes'] += f.file_size | |
59 return self._processed_files | |
60 | |
61 def Compare(self, other_apk): | |
62 """Compares size information of two apks. | |
63 | |
64 Args: | |
65 other_apk: Apk to compare size against. | |
66 | |
67 Returns: | |
68 Dictionary of comparision results. | |
69 """ | |
70 if not isinstance(other_apk, type(self)): | |
71 raise TypeError('Must pass it an ApkSizeInfo object') | |
72 | |
73 other_lib_compressed = other_apk.processed_files['so']['compressed_bytes'] | |
74 other_lib_uncompressed = ( | |
75 other_apk.processed_files['so']['uncompressed_bytes']) | |
76 this_lib_compressed = self._processed_files['so']['compressed_bytes'] | |
77 this_lib_uncompressed = self._processed_files['so']['uncompressed_bytes'] | |
78 | |
79 # TODO(rnephew) This will be made obsolete with modern and legacy apks being | |
80 # separate, a new method to compare will be required eventually. | |
81 return collections.OrderedDict([ | |
82 ('APK_size_reduction', | |
83 other_apk.compressed_size - self.compressed_size), | |
84 ('ARM32_Legacy_install_or_upgrade_reduction', | |
85 (other_lib_compressed - this_lib_compressed) + | |
86 (other_lib_uncompressed - this_lib_uncompressed)), | |
87 ('ARM32_Legacy_system_image_reduction', | |
88 other_lib_compressed - this_lib_compressed), | |
89 ('ARM32_Legacy_patch_size_reduction', | |
andrewhayden
2015/10/14 23:38:58
The details of Google's application update technol
rnephew (Wrong account)
2015/10/15 13:57:02
Done.
| |
90 other_lib_compressed - this_lib_compressed), | |
91 ('ARM32_Modern_ARM64_install_or_upgrade_reduction', | |
92 other_lib_uncompressed - this_lib_uncompressed), | |
93 ('ARM32_Modern_ARM64_system_image_reduction', | |
94 other_lib_uncompressed - this_lib_uncompressed), | |
95 ('ARM32_Modern_ARM64_patch_size_reduction', | |
andrewhayden
2015/10/14 23:38:58
Same here, not feasible to get this number in any
rnephew (Wrong account)
2015/10/15 13:57:02
Done.
| |
96 other_lib_compressed - this_lib_compressed) | |
97 ]) | |
98 | |
99 @property | |
100 def apk_size(self): | |
101 return self._apk_size | |
102 | |
103 @property | |
104 def compressed_size(self): | |
105 return self._compressed_size | |
106 | |
107 @property | |
108 def total_files(self): | |
109 return self._total_files | |
110 | |
111 @property | |
112 def uncompressed_size(self): | |
113 return self._uncompressed_size | |
114 | |
115 @property | |
116 def processed_files(self): | |
117 return self._processed_files | |
118 | |
119 def print_dashboard_readable_size_info(apk): | |
120 """Prints size information in dashboard readable format. | |
121 | |
122 Args: | |
123 apk: ApkSizeInfo object | |
124 """ | |
125 files = apk.processed_files | |
126 perf_tests_results_helper.PrintPerfResult( | |
127 'apk_size', 'total_files', [apk.total_files], 'files') | |
128 perf_tests_results_helper.PrintPerfResult( | |
129 'apk_size', 'total_size_compressed', [apk.compressed_size], 'bytes') | |
130 perf_tests_results_helper.PrintPerfResult( | |
131 'apk_size', 'total_size_uncompressed', [apk.uncompressed_size], 'bytes') | |
132 perf_tests_results_helper.PrintPerfResult( | |
133 'apk_size', 'apk_overhead', [apk.apk_size - apk.compressed_size], 'bytes') | |
134 for ext in files: | |
135 perf_tests_results_helper.PrintPerfResult( | |
136 'apk_size', '%s_files' % ext, [files[ext]['number']], 'files') | |
137 perf_tests_results_helper.PrintPerfResult( | |
138 'apk_size', '%s_compressed_size' % ext, | |
139 [files[ext]['compressed_bytes']], 'bytes') | |
140 perf_tests_results_helper.PrintPerfResult( | |
141 'apk_size', '%s_uncompressed_size' % ext, | |
142 [files[ext]['uncompressed_bytes']], 'bytes') | |
143 | |
144 def print_human_readable_size_info(apk): | |
145 """Prints size information in human readable format. | |
146 | |
147 Args: | |
148 apk: ApkSizeInfo object | |
149 """ | |
150 files = apk.processed_files | |
151 logging.critical('Stats for files as they exist within the apk:') | |
152 for ext in files: | |
153 logging.critical(' %-8s %s bytes in %s files', ext, | |
154 files[ext]['compressed_bytes'], files[ext]['number']) | |
155 logging.critical('--------------------------------------') | |
156 logging.critical( | |
157 'All Files: %s bytes in %s files', apk.compressed_size, apk.total_files) | |
158 logging.critical('APK Size: %s', apk.apk_size) | |
159 logging.critical('APK overhead: %s', apk.apk_size - apk.compressed_size) | |
160 logging.critical('--------------------------------------') | |
161 logging.critical('Stats for files when extracted from the apk:') | |
162 for ext in files: | |
163 logging.critical(' %-8s %s bytes in %s files', ext, | |
164 files[ext]['uncompressed_bytes'], files[ext]['number']) | |
165 logging.critical('--------------------------------------') | |
166 logging.critical( | |
167 'All Files: %s bytes in %s files', apk.uncompressed_size, apk.total_files) | |
168 | |
169 def print_human_readable_compare(compare_dict): | |
170 """Prints size comparison between two apks in human readable format. | |
171 | |
172 Args: | |
173 compare_dict: Dictionary returned from ApkSizeInfo.Compare() | |
174 """ | |
175 for key, value in compare_dict.iteritems(): | |
176 logging.critical(' %-50s %s bytes', key, value) | |
177 | |
178 def print_dashboard_readable_compare(compare_dict): | |
179 """Prints size comparison between two apks in dashboard readable format. | |
180 | |
181 Args: | |
182 compare_dict: Dictionary returned from APkSizeInfo.Compare() | |
183 """ | |
184 for key, value in compare_dict.iteritems(): | |
185 perf_tests_results_helper.PrintPerfResult( | |
186 'apk_size_compare', key, [value], 'bytes') | |
187 | |
188 def main(): | |
189 parser = argparse.ArgumentParser() | |
190 parser.add_argument('file_path') | |
191 parser.add_argument('-c', '--compare', help=('APK to compare against.')) | |
192 parser.add_argument('-d', '--perf-dashboard-output', action='store_true', | |
193 help=('Sets it to return data in bot readable format')) | |
194 args = parser.parse_args() | |
195 | |
196 apk = ApkSizeInfo(args.file_path) | |
197 if args.compare: | |
198 compare_dict = apk.Compare(ApkSizeInfo(args.compare)) | |
199 if args.perf_dashboard_output: | |
200 print_dashboard_readable_compare(compare_dict) | |
201 else: | |
202 print_human_readable_compare(compare_dict) | |
203 else: | |
204 if args.perf_dashboard_output: | |
205 print_dashboard_readable_size_info(apk) | |
206 else: | |
207 print_human_readable_size_info(apk) | |
208 | |
209 if __name__ == '__main__': | |
210 sys.exit(main()) | |
OLD | NEW |