Chromium Code Reviews| Index: build/android/apksize.py |
| diff --git a/build/android/apksize.py b/build/android/apksize.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..17e0ec11ae1e46e4dcf681bee85e0504eab2abe6 |
| --- /dev/null |
| +++ b/build/android/apksize.py |
| @@ -0,0 +1,211 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +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. |
| + |
| + 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 |
| + self._ProcessFiles() |
| + |
| + def _ProcessFiles(self): |
| + """Uses zipinfo to process apk file information.""" |
| + INITIAL_FILE_EXTENSION_INFO = { |
| + 'number': 0, |
| + 'compressed_bytes': 0, |
| + 'uncompressed_bytes': 0 |
| + } |
| + self._processed_files = collections.defaultdict( |
| + lambda: dict(INITIAL_FILE_EXTENSION_INFO)) |
| + |
| + for f in self._zipfile.infolist(): |
| + _, file_ext = os.path.splitext(f.filename) |
| + file_ext = file_ext[1:] # Drop . from extension. |
| + |
| + 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 |
| + return self._processed_files |
| + |
| + def Compare(self, other_apk): |
| + """Compares size information of two apks. |
| + |
| + Args: |
| + other_apk: Apk to compare size against. |
|
jbudorick
2015/10/27 23:40:03
nit: make it clear that this is another instance o
rnephew (Wrong account)
2015/10/28 00:06:07
Done.
|
| + |
| + Returns: |
| + Dictionary of comparision results. |
| + """ |
| + if not isinstance(other_apk, type(self)): |
| + raise TypeError('Must pass it an ApkSizeInfo object') |
| + |
| + other_lib_compressed = other_apk.processed_files['so']['compressed_bytes'] |
| + other_lib_uncompressed = ( |
| + other_apk.processed_files['so']['uncompressed_bytes']) |
| + this_lib_compressed = self._processed_files['so']['compressed_bytes'] |
| + this_lib_uncompressed = self._processed_files['so']['uncompressed_bytes'] |
| + |
| + # TODO(rnephew) This will be made obsolete with modern and legacy apks being |
| + # separate, a new method to compare will be required eventually. |
| + return collections.OrderedDict([ |
| + ('APK_size_reduction', |
| + other_apk.compressed_size - self.compressed_size), |
| + ('ARM32_Legacy_install_or_upgrade_reduction', |
| + (other_lib_compressed - this_lib_compressed) + |
| + (other_lib_uncompressed - this_lib_uncompressed)), |
|
mikecase (-- gone --)
2015/10/22 01:20:53
Is this correct?
Why is this measuring (other_com
rnephew (Wrong account)
2015/10/22 01:33:02
For 32b legacy apks, yes; both the compressed and
|
| + ('ARM32_Legacy_system_image_reduction', |
| + other_lib_compressed - this_lib_compressed), |
| + ('ARM32_Modern_ARM64_install_or_upgrade_reduction', |
| + other_lib_uncompressed - this_lib_uncompressed), |
| + ('ARM32_Modern_ARM64_system_image_reduction', |
| + other_lib_uncompressed - this_lib_uncompressed), |
| + ]) |
| + |
| + @property |
| + def apk_size(self): |
| + return self._apk_size |
| + |
| + @property |
| + def compressed_size(self): |
| + return self._compressed_size |
| + |
| + @property |
| + def total_files(self): |
| + return self._total_files |
| + |
| + @property |
| + def uncompressed_size(self): |
| + return self._uncompressed_size |
| + |
| + @property |
| + def processed_files(self): |
| + return self._processed_files |
| + |
| +def print_dashboard_readable_size_info(apk): |
| + """Prints size information in dashboard readable format. |
| + |
| + Args: |
| + apk: ApkSizeInfo object |
| + """ |
| + files = apk.processed_files |
| + 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.processed_files |
| + 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(compare_dict): |
| + """Prints size comparison between two apks in human readable format. |
| + |
| + Args: |
| + compare_dict: Dictionary returned from ApkSizeInfo.Compare() |
| + """ |
| + for key, value in compare_dict.iteritems(): |
| + logging.critical(' %-50s %s bytes', key, value) |
| + |
| +def print_dashboard_readable_compare(compare_dict): |
| + """Prints size comparison between two apks in dashboard readable format. |
| + |
| + Args: |
| + compare_dict: Dictionary returned from APkSizeInfo.Compare() |
| + """ |
| + for key, value in compare_dict.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: |
| + compare_dict = apk.Compare(ApkSizeInfo(args.compare)) |
| + if args.perf_dashboard_output: |
| + print_dashboard_readable_compare(compare_dict) |
| + else: |
| + print_human_readable_compare(compare_dict) |
| + 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()) |