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

Side by Side Diff: build/android/apksize.py

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