Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: build/android/apksize.py

Issue 1397553002: [Android] Apk size tool. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/apksize.py
diff --git a/build/android/apksize.py b/build/android/apksize.py
new file mode 100644
index 0000000000000000000000000000000000000000..8b2294271c828be17dd158a97f175e2c0e8b8e0a
--- /dev/null
+++ b/build/android/apksize.py
@@ -0,0 +1,210 @@
+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.')
+ 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.
+ if not zipfile.is_zipfile(path):
+ raise TypeError('Not a valid apk')
+ 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):
+ """ Uses zipinfo to process apk file information."""
+ if self._processed_files:
+ return self._processed_files
+ self._processed_files = {}
+ 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'] += 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.
+ self._processed_files[file_ext]['uncompressed'] += f.file_size
+ else:
+
+ self._compressed_size += f.compress_size
+ self._total_files += 1
+ self._uncompressed_size += f.file_size
+ self._processed_files[file_ext] = {
+ 'number': 1,
+ 'compressed': f.compress_size,
+ 'uncompressed': f.file_size,
+ }
+ return self._processed_files
+
+ 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.
+ """Compares size information of two apks.
+
+ Args:
+ old_apk: Apk to compare size against.
+ """
+ if not isinstance(old_apk, type(self)):
+ raise TypeError('Must pass it an ApkSizeInfo object')
+ apk_one = old_apk.ProcessFiles()
+ apk_two = self.ProcessFiles()
+ old_lib_compressed = apk_one['so']['compressed']
+ old_lib_uncompressed = apk_one['so']['uncompressed']
+ new_lib_compressed = apk_two['so']['compressed']
+ new_lib_uncompressed = apk_two['so']['uncompressed']
+ reduction_apk_size = old_apk.compressed_size - self.compressed_size
+ reduction_compressed_files = old_lib_compressed - new_lib_compressed
+ 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
+ reduction_install_legacy = (
+ reduction_compressed_files + reduction_uncompressed_files)
+ # TODO(rnephew) This will be made obsolete with modern and legacy apks being
+ # seperate, a new method to compare will be required eventually.
+ 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
+ ('APK_size_reduction', reduction_apk_size),
+ ('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
+ ('ARM32_Legacy_system_image_reduction', reduction_compressed_files),
+ ('ARM32_Legacy_patch_size_reduction', reduction_compressed_files),
+ ('ARM32_Modern_ARM64_install_or_upgrade_reduction',
+ reduction_uncompressed_files),
+ ('ARM32_Modern_ARM64_system_image_reduction',
+ reduction_uncompressed_files),
+ ('ARM32_Modern_ARM64_patch_size_reduction', reduction_compressed_files)
+ ])
+ return ret_dict
+
+ @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 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')
+ perf_tests_results_helper.PrintPerfResult(
+ 'apk_size', '%s_uncompressed_size' % ext, [files[ext]['uncompressed']],
+ 'bytes')
+
+def 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'], 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'], files[ext]['number'])
+ logging.critical('--------------------------------------')
+ logging.critical(
+ 'All Files: %s bytes in %s files', apk.uncompressed_size, apk.total_files)
+
+def human_readable_compare(results):
+ """Prints size comparison between two apks in human readable format.
+
+ Args:
+ 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.
+ """
+ for key, value in results.iteritems():
+ logging.critical(' %-50s %s bytes', key, value)
+
+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.
+ """Prints size comparison between two apks in dashboard readable format.
+
+ Args:
mikecase (-- gone --) 2015/10/07 19:33:01 Fix this Args doc.
rnephew (Reviews Here) 2015/10/07 21:18:24 Done.
+ apk: ApkSizeInfo object
+ """
+ for key, value in results.iteritems():
+ perf_tests_results_helper.PrintPerfResult(
+ 'apk_size_compare', key, [value], 'bytes')
+
+def main():
+ parser = argparse.ArgumentParser()
+ 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.
+ 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.
+ help=('Sets it to return data in bot readable format'))
+ parser.add_argument('-c', '--compare', help=('APK to compare against.'))
+ args = parser.parse_args()
+
+ apk = ApkSizeInfo(args.file_path)
+ if args.compare:
+ if args.bot_mode:
+ dashboard_readable_compare(apk.Compare(ApkSizeInfo(args.compare)))
+ else:
+ human_readable_compare(apk.Compare(ApkSizeInfo(args.compare)))
+ elif args.bot_mode:
+ dashboard_readable_size_info(apk)
+ else:
+ 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.
+
+if __name__ == '__main__':
+ sys.exit(main())
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698