Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 import argparse | |
| 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 logging.info('APK: %s', path) | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: Slightly strangely placed logging statement.
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 29 if not zipfile.is_zipfile(path): | |
| 30 raise TypeError('Not a valid apk') | |
| 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 | |
| 38 def ProcessFiles(self): | |
| 39 """ Uses zipinfo to process apk file information.""" | |
| 40 if self._processed_files: | |
| 41 return self._processed_files | |
| 42 self._processed_files = {} | |
| 43 for f in self._zipfile.infolist(): | |
| 44 _, file_ext = os.path.splitext(f.filename) | |
| 45 file_ext = file_ext[1:] # Drop . from extension. | |
| 46 if file_ext in self._processed_files: | |
| 47 self._compressed_size += f.compress_size | |
| 48 self._total_files += 1 | |
| 49 self._uncompressed_size += f.file_size | |
| 50 self._processed_files[file_ext]['number'] += 1 | |
| 51 self._processed_files[file_ext]['compressed'] += f.compress_size | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
Maybe add units into the key name, like 'compresse
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 52 self._processed_files[file_ext]['uncompressed'] += f.file_size | |
| 53 else: | |
| 54 | |
| 55 self._compressed_size += f.compress_size | |
| 56 self._total_files += 1 | |
| 57 self._uncompressed_size += f.file_size | |
| 58 self._processed_files[file_ext] = { | |
| 59 'number': 1, | |
| 60 'compressed': f.compress_size, | |
| 61 'uncompressed': f.file_size, | |
| 62 } | |
| 63 return self._processed_files | |
| 64 | |
| 65 def Compare(self, old_apk): | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
maybe rename old_apk to other_apk. Doesn't appear
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 66 """Compares size information of two apks. | |
| 67 | |
| 68 Args: | |
| 69 old_apk: Apk to compare size against. | |
| 70 """ | |
| 71 if not isinstance(old_apk, type(self)): | |
| 72 raise TypeError('Must pass it an ApkSizeInfo object') | |
| 73 apk_one = old_apk.ProcessFiles() | |
| 74 apk_two = self.ProcessFiles() | |
| 75 old_lib_compressed = apk_one['so']['compressed'] | |
| 76 old_lib_uncompressed = apk_one['so']['uncompressed'] | |
| 77 new_lib_compressed = apk_two['so']['compressed'] | |
| 78 new_lib_uncompressed = apk_two['so']['uncompressed'] | |
| 79 reduction_apk_size = old_apk.compressed_size - self.compressed_size | |
| 80 reduction_compressed_files = old_lib_compressed - new_lib_compressed | |
| 81 reduction_uncompressed_files = old_lib_uncompressed - new_lib_uncompressed | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: ^ for the above 10 or so lines, maybe just fi
rnephew (Reviews Here)
2015/10/07 21:18:24
I did that with some of them, but not all; it made
| |
| 82 reduction_install_legacy = ( | |
| 83 reduction_compressed_files + reduction_uncompressed_files) | |
| 84 # TODO(rnephew) This will be made obsolete with modern and legacy apks being | |
| 85 # seperate, a new method to compare will be required eventually. | |
| 86 ret_dict = collections.OrderedDict([ | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: Rename ret_dict to something more descriptive
rnephew (Reviews Here)
2015/10/07 21:18:24
Just got rid of it, no need to make a variable whe
| |
| 87 ('APK_size_reduction', reduction_apk_size), | |
| 88 ('ARM32_Legacy_install_or_upgrade_reduction', reduction_install_legacy), | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
Does this size comparison only work for ARM32?
rnephew (Reviews Here)
2015/10/07 21:18:24
I picked the names based off the document here:
ht
| |
| 89 ('ARM32_Legacy_system_image_reduction', reduction_compressed_files), | |
| 90 ('ARM32_Legacy_patch_size_reduction', reduction_compressed_files), | |
| 91 ('ARM32_Modern_ARM64_install_or_upgrade_reduction', | |
| 92 reduction_uncompressed_files), | |
| 93 ('ARM32_Modern_ARM64_system_image_reduction', | |
| 94 reduction_uncompressed_files), | |
| 95 ('ARM32_Modern_ARM64_patch_size_reduction', reduction_compressed_files) | |
| 96 ]) | |
| 97 return ret_dict | |
| 98 | |
| 99 @property | |
| 100 def apk_size(self): | |
| 101 return self._apk_size | |
| 102 | |
| 103 @property | |
| 104 def compressed_size(self): | |
| 105 if self._compressed_size is None: | |
| 106 self.ProcessFiles() | |
| 107 return self._compressed_size | |
| 108 | |
| 109 @property | |
| 110 def total_files(self): | |
| 111 if self._total_files is None: | |
| 112 self.ProcessFiles() | |
| 113 return self._total_files | |
| 114 | |
| 115 @property | |
| 116 def uncompressed_size(self): | |
| 117 if self._uncompressed_size is None: | |
| 118 self.ProcessFiles() | |
| 119 return self._uncompressed_size | |
| 120 | |
| 121 def dashboard_readable_size_info(apk): | |
| 122 """Prints size information in dashboard readable format. | |
| 123 | |
| 124 Args: | |
| 125 apk: ApkSizeInfo object | |
| 126 """ | |
| 127 files = apk.ProcessFiles() | |
| 128 perf_tests_results_helper.PrintPerfResult( | |
| 129 'apk_size', 'total_files', [apk.total_files], 'files') | |
| 130 perf_tests_results_helper.PrintPerfResult( | |
| 131 'apk_size', 'total_size_compressed', [apk.compressed_size], 'bytes') | |
| 132 perf_tests_results_helper.PrintPerfResult( | |
| 133 'apk_size', 'total_size_uncompressed', [apk.uncompressed_size], 'bytes') | |
| 134 perf_tests_results_helper.PrintPerfResult( | |
| 135 'apk_size', 'apk_overhead', [apk.apk_size - apk.compressed_size], 'bytes') | |
| 136 for ext in files: | |
| 137 perf_tests_results_helper.PrintPerfResult( | |
| 138 'apk_size', '%s_files' % ext, [files[ext]['number']], 'files') | |
| 139 perf_tests_results_helper.PrintPerfResult( | |
| 140 'apk_size', '%s_compressed_size' % ext, [files[ext]['compressed']], | |
| 141 'bytes') | |
| 142 perf_tests_results_helper.PrintPerfResult( | |
| 143 'apk_size', '%s_uncompressed_size' % ext, [files[ext]['uncompressed']], | |
| 144 'bytes') | |
| 145 | |
| 146 def human_readable_size_info(apk): | |
| 147 """Prints size information in human readable format. | |
| 148 | |
| 149 Args: | |
| 150 apk: ApkSizeInfo object | |
| 151 """ | |
| 152 files = apk.ProcessFiles() | |
| 153 logging.critical('Stats for files as they exist within the apk:') | |
| 154 for ext in files: | |
| 155 logging.critical(' %-8s %s bytes in %s files', ext, | |
| 156 files[ext]['compressed'], files[ext]['number']) | |
| 157 logging.critical('--------------------------------------') | |
| 158 logging.critical( | |
| 159 'All Files: %s bytes in %s files', apk.compressed_size, apk.total_files) | |
| 160 logging.critical('APK Size: %s', apk.apk_size) | |
| 161 logging.critical('APK overhead: %s', apk.apk_size - apk.compressed_size) | |
| 162 logging.critical('--------------------------------------') | |
| 163 logging.critical('Stats for files when extracted from the apk:') | |
| 164 for ext in files: | |
| 165 logging.critical(' %-8s %s bytes in %s files', ext, | |
| 166 files[ext]['uncompressed'], files[ext]['number']) | |
| 167 logging.critical('--------------------------------------') | |
| 168 logging.critical( | |
| 169 'All Files: %s bytes in %s files', apk.uncompressed_size, apk.total_files) | |
| 170 | |
| 171 def human_readable_compare(results): | |
| 172 """Prints size comparison between two apks in human readable format. | |
| 173 | |
| 174 Args: | |
| 175 apk: ApkSizeInfo object | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
Fix this Args doc.
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 176 """ | |
| 177 for key, value in results.iteritems(): | |
| 178 logging.critical(' %-50s %s bytes', key, value) | |
| 179 | |
| 180 def dashboard_readable_compare(results): | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: I would rename these functions to have "outpu
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 181 """Prints size comparison between two apks in dashboard readable format. | |
| 182 | |
| 183 Args: | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
Fix this Args doc.
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 184 apk: ApkSizeInfo object | |
| 185 """ | |
| 186 for key, value in results.iteritems(): | |
| 187 perf_tests_results_helper.PrintPerfResult( | |
| 188 'apk_size_compare', key, [value], 'bytes') | |
| 189 | |
| 190 def main(): | |
| 191 parser = argparse.ArgumentParser() | |
| 192 parser.add_argument("file_path") | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: single quotes probably
rnephew (Reviews Here)
2015/10/07 21:18:23
Done.
| |
| 193 parser.add_argument('-b', '--bot-mode', action='store_true', | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
maybe rename to --perf-dashboard-formatting or --p
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 194 help=('Sets it to return data in bot readable format')) | |
| 195 parser.add_argument('-c', '--compare', help=('APK to compare against.')) | |
| 196 args = parser.parse_args() | |
| 197 | |
| 198 apk = ApkSizeInfo(args.file_path) | |
| 199 if args.compare: | |
| 200 if args.bot_mode: | |
| 201 dashboard_readable_compare(apk.Compare(ApkSizeInfo(args.compare))) | |
| 202 else: | |
| 203 human_readable_compare(apk.Compare(ApkSizeInfo(args.compare))) | |
| 204 elif args.bot_mode: | |
| 205 dashboard_readable_size_info(apk) | |
| 206 else: | |
| 207 human_readable_size_info(apk) | |
|
mikecase (-- gone --)
2015/10/07 19:33:01
nit: I think this would look better as...
if args
rnephew (Reviews Here)
2015/10/07 21:18:24
Done.
| |
| 208 | |
| 209 if __name__ == '__main__': | |
| 210 sys.exit(main()) | |
| OLD | NEW |