Chromium Code Reviews| Index: build/android/apksize.py |
| diff --git a/build/android/apksize.py b/build/android/apksize.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b917428a2c98423553b25c3d401065c9c08ee134 |
| --- /dev/null |
| +++ b/build/android/apksize.py |
| @@ -0,0 +1,211 @@ |
| +import argparse |
| +import collections |
| +import logging |
| +import os |
| +import sys |
| +import zipfile |
| + |
| +from pylib import constants |
| + |
| +sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib', |
| + 'common')) |
| +import perf_tests_results_helper # pylint: disable=import-error |
| + |
| + |
| +# TODO(rnephew): Add support for split apks. |
| +class ApkSizeInfo(object): |
| + |
| + def __init__(self, path): |
| + """ ApkSizeInfo constructor. |
|
jbudorick
2015/10/08 14:23:29
nit: no space after """
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + |
| + Args: |
| + path: Path to apk. |
| + """ |
| + if not os.path.isfile(path): |
| + raise IOError('Not a valid file path for apk.') |
| + if not os.access(path, os.R_OK): |
| + raise IOError('File is not readable.') |
| + if not zipfile.is_zipfile(path): |
| + raise TypeError('Not a valid apk') |
| + logging.info('APK: %s', path) |
| + self._apk_size = os.path.getsize(path) |
| + self._zipfile = zipfile.ZipFile(path, 'r') |
| + self._processed_files = None |
| + self._compressed_size = 0 |
| + self._total_files = 0 |
| + self._uncompressed_size = 0 |
| + |
| + def ProcessFiles(self): |
|
jbudorick
2015/10/08 14:23:29
What's the reasoning behind doing this lazily rath
rnephew (Reviews Here)
2015/10/08 16:29:22
Made this a private function called by the constru
rnephew (Wrong account)
2015/10/15 13:57:02
Made it a private function called by the construct
|
| + """ Uses zipinfo to process apk file information.""" |
|
jbudorick
2015/10/08 14:23:29
nit: same
rnephew (Reviews Here)
2015/10/08 16:29:21
Done.
rnephew (Wrong account)
2015/10/15 13:57:02
Done.
|
| + if self._processed_files: |
| + return self._processed_files |
| + self._processed_files = {} |
|
jbudorick
2015/10/08 14:23:29
You could do
INITIAL_FILE_EXTENSION_INFO = {
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + for f in self._zipfile.infolist(): |
| + _, file_ext = os.path.splitext(f.filename) |
| + file_ext = file_ext[1:] # Drop . from extension. |
| + if file_ext in self._processed_files: |
| + self._compressed_size += f.compress_size |
| + self._total_files += 1 |
| + self._uncompressed_size += f.file_size |
| + self._processed_files[file_ext]['number'] += 1 |
| + self._processed_files[file_ext]['compressed_bytes'] += f.compress_size |
| + self._processed_files[file_ext]['uncompressed_bytes'] += f.file_size |
| + else: |
| + |
|
jbudorick
2015/10/08 14:23:29
nit: remove blank line or move it before the else
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + self._compressed_size += f.compress_size |
| + self._total_files += 1 |
| + self._uncompressed_size += f.file_size |
| + self._processed_files[file_ext] = { |
| + 'number': 1, |
| + 'compressed_bytes': f.compress_size, |
| + 'uncompressed_bytes': f.file_size, |
| + } |
| + return self._processed_files |
| + |
| + def Compare(self, other_pak): |
| + """Compares size information of two apks. |
| + |
| + Args: |
| + other_pak: Apk to compare size against. |
|
jbudorick
2015/10/08 14:23:29
other_pak -> other_apk?
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + """ |
|
jbudorick
2015/10/08 14:23:29
Returns:
rnephew (Reviews Here)
2015/10/08 16:29:21
Done.
|
| + if not isinstance(other_pak, type(self)): |
| + raise TypeError('Must pass it an ApkSizeInfo object') |
| + apk_one = other_pak.ProcessFiles() |
|
jbudorick
2015/10/08 14:23:29
nit: "one" and "two" should be named to reflect wh
rnephew (Reviews Here)
2015/10/08 16:29:21
Done.
|
| + apk_two = self.ProcessFiles() |
| + old_lib_compressed = apk_one['so']['compressed_bytes'] |
| + old_lib_uncompressed = apk_one['so']['uncompressed_bytes'] |
| + new_lib_compressed = apk_two['so']['compressed_bytes'] |
| + new_lib_uncompressed = apk_two['so']['uncompressed_bytes'] |
| + # TODO(rnephew) This will be made obsolete with modern and legacy apks being |
| + # seperate, a new method to compare will be required eventually. |
|
jbudorick
2015/10/08 14:23:29
nit: separate
rnephew (Reviews Here)
2015/10/08 16:29:21
Done.
|
| + return collections.OrderedDict([ |
| + ('APK_size_reduction', |
| + other_pak.compressed_size - self.compressed_size), |
| + ('ARM32_Legacy_install_or_upgrade_reduction', |
| + (old_lib_compressed - new_lib_compressed) + |
| + (old_lib_uncompressed - new_lib_uncompressed)), |
| + ('ARM32_Legacy_system_image_reduction', |
| + old_lib_compressed - new_lib_compressed), |
| + ('ARM32_Legacy_patch_size_reduction', |
| + old_lib_compressed - new_lib_compressed), |
| + ('ARM32_Modern_ARM64_install_or_upgrade_reduction', |
| + old_lib_uncompressed - new_lib_uncompressed), |
| + ('ARM32_Modern_ARM64_system_image_reduction', |
| + old_lib_uncompressed - new_lib_uncompressed), |
| + ('ARM32_Modern_ARM64_patch_size_reduction', |
| + old_lib_compressed - new_lib_compressed) |
| + ]) |
| + |
| + @property |
| + def apk_size(self): |
| + return self._apk_size |
| + |
| + @property |
| + def compressed_size(self): |
| + if self._compressed_size is None: |
| + self.ProcessFiles() |
| + return self._compressed_size |
| + |
| + @property |
| + def total_files(self): |
| + if self._total_files is None: |
| + self.ProcessFiles() |
| + return self._total_files |
| + |
| + @property |
| + def uncompressed_size(self): |
| + if self._uncompressed_size is None: |
| + self.ProcessFiles() |
| + return self._uncompressed_size |
| + |
| +def print_dashboard_readable_size_info(apk): |
| + """Prints size information in dashboard readable format. |
| + |
| + Args: |
| + apk: ApkSizeInfo object |
| + """ |
| + files = apk.ProcessFiles() |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', 'total_files', [apk.total_files], 'files') |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', 'total_size_compressed', [apk.compressed_size], 'bytes') |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', 'total_size_uncompressed', [apk.uncompressed_size], 'bytes') |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', 'apk_overhead', [apk.apk_size - apk.compressed_size], 'bytes') |
| + for ext in files: |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', '%s_files' % ext, [files[ext]['number']], 'files') |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', '%s_compressed_size' % ext, |
| + [files[ext]['compressed_bytes']], 'bytes') |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size', '%s_uncompressed_size' % ext, |
| + [files[ext]['uncompressed_bytes']], 'bytes') |
| + |
| +def print_human_readable_size_info(apk): |
| + """Prints size information in human readable format. |
| + |
| + Args: |
| + apk: ApkSizeInfo object |
| + """ |
| + files = apk.ProcessFiles() |
| + logging.critical('Stats for files as they exist within the apk:') |
| + for ext in files: |
| + logging.critical(' %-8s %s bytes in %s files', ext, |
| + files[ext]['compressed_bytes'], files[ext]['number']) |
| + logging.critical('--------------------------------------') |
| + logging.critical( |
| + 'All Files: %s bytes in %s files', apk.compressed_size, apk.total_files) |
| + logging.critical('APK Size: %s', apk.apk_size) |
| + logging.critical('APK overhead: %s', apk.apk_size - apk.compressed_size) |
| + logging.critical('--------------------------------------') |
| + logging.critical('Stats for files when extracted from the apk:') |
| + for ext in files: |
| + logging.critical(' %-8s %s bytes in %s files', ext, |
| + files[ext]['uncompressed_bytes'], files[ext]['number']) |
| + logging.critical('--------------------------------------') |
| + logging.critical( |
| + 'All Files: %s bytes in %s files', apk.uncompressed_size, apk.total_files) |
| + |
| +def print_human_readable_compare(apk): |
| + """Prints size comparison between two apks in human readable format. |
| + |
| + Args: |
| + apk: ApkSizeInfo object |
|
jbudorick
2015/10/08 14:23:29
"apk" isn't an ApkSizeInfo object, and it probably
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + """ |
| + for key, value in apk.iteritems(): |
| + logging.critical(' %-50s %s bytes', key, value) |
| + |
| +def print_dashboard_readable_compare(apk): |
| + """Prints size comparison between two apks in dashboard readable format. |
| + |
| + Args: |
| + apk: ApkSizeInfo object |
|
jbudorick
2015/10/08 14:23:29
same
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + """ |
| + for key, value in apk.iteritems(): |
| + perf_tests_results_helper.PrintPerfResult( |
| + 'apk_size_compare', key, [value], 'bytes') |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('file_path') |
| + parser.add_argument('-c', '--compare', help=('APK to compare against.')) |
| + parser.add_argument('-d', '--perf-dashboard-output', action='store_true', |
| + help=('Sets it to return data in bot readable format')) |
| + args = parser.parse_args() |
| + |
| + apk = ApkSizeInfo(args.file_path) |
| + if args.compare: |
| + if args.perf_dashboard_output: |
| + print_dashboard_readable_compare(apk.Compare(ApkSizeInfo(args.compare))) |
|
jbudorick
2015/10/08 14:23:29
The Compare call is the same in both cases, so ext
rnephew (Reviews Here)
2015/10/08 16:29:22
Done.
|
| + else: |
| + print_human_readable_compare(apk.Compare(ApkSizeInfo(args.compare))) |
| + else: |
| + if args.perf_dashboard_output: |
| + print_dashboard_readable_size_info(apk) |
| + else: |
| + print_human_readable_size_info(apk) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |