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
6import argparse
7import collections
8import json
9import logging
10import os
11import sys
12import zipfile
13
14_BASE_CHART = {
15    'format_version': '0.1',
16    'benchmark_name': 'apk_size',
17    'benchmark_description': 'APK size information.',
18    'trace_rerun_options': [],
19    'charts': {}
20}
21
22
23# TODO(rnephew): Add support for split apks.
24class ApkSizeInfo(object):
25
26  def __init__(self, path):
27    """ApkSizeInfo constructor.
28
29    Args:
30      path: Path to apk.
31    """
32    if not os.path.isfile(path):
33      raise IOError('Not a valid file path for apk.')
34    if not os.access(path, os.R_OK):
35      raise IOError('File is not readable.')
36    if not zipfile.is_zipfile(path):
37      raise TypeError('Not a valid apk')
38    logging.info('APK: %s', path)
39    self._apk_size = os.path.getsize(path)
40    self._zipfile = zipfile.ZipFile(path, 'r')
41    self._processed_files = None
42    self._compressed_size = 0
43    self._total_files = 0
44    self._uncompressed_size = 0
45    self._ProcessFiles()
46
47  def _ProcessFiles(self):
48    """Uses zipinfo to process apk file information."""
49    INITIAL_FILE_EXTENSION_INFO = {
50        'number': 0,
51        'compressed_bytes': 0,
52        'uncompressed_bytes': 0
53    }
54    self._processed_files = collections.defaultdict(
55        lambda: dict(INITIAL_FILE_EXTENSION_INFO))
56
57    for f in self._zipfile.infolist():
58      _, file_ext = os.path.splitext(f.filename)
59      file_ext = file_ext[1:] # Drop . from extension.
60
61      self._compressed_size += f.compress_size
62      self._total_files += 1
63      self._uncompressed_size += f.file_size
64      self._processed_files[file_ext]['number'] += 1
65      self._processed_files[file_ext]['compressed_bytes'] += f.compress_size
66      self._processed_files[file_ext]['uncompressed_bytes'] += f.file_size
67    return self._processed_files
68
69  def Compare(self, other_apk):
70    """Compares size information of two apks.
71
72    Args:
73      other_apk: ApkSizeInfo instance to compare size against.
74
75    Returns:
76      Dictionary of comparision results.
77    """
78    if not isinstance(other_apk, type(self)):
79      raise TypeError('Must pass it an ApkSizeInfo object')
80
81    other_lib_compressed = other_apk.processed_files['so']['compressed_bytes']
82    other_lib_uncompressed = (
83        other_apk.processed_files['so']['uncompressed_bytes'])
84    this_lib_compressed = self._processed_files['so']['compressed_bytes']
85    this_lib_uncompressed = self._processed_files['so']['uncompressed_bytes']
86
87    # TODO(rnephew) This will be made obsolete with modern and legacy apks being
88    # separate, a new method to compare will be required eventually.
89    return collections.OrderedDict([
90        ('APK_size_reduction',
91            other_apk.compressed_size - self.compressed_size),
92        ('ARM32_Legacy_install_or_upgrade_reduction',
93            (other_lib_compressed - this_lib_compressed) +
94            (other_lib_uncompressed - this_lib_uncompressed)),
95        ('ARM32_Legacy_system_image_reduction',
96            other_lib_compressed - this_lib_compressed),
97        ('ARM32_Modern_ARM64_install_or_upgrade_reduction',
98            other_lib_uncompressed - this_lib_uncompressed),
99        ('ARM32_Modern_ARM64_system_image_reduction',
100            other_lib_uncompressed - this_lib_uncompressed),
101    ])
102
103  @property
104  def apk_size(self):
105    return self._apk_size
106
107  @property
108  def compressed_size(self):
109    return self._compressed_size
110
111  @property
112  def total_files(self):
113    return self._total_files
114
115  @property
116  def uncompressed_size(self):
117    return self._uncompressed_size
118
119  @property
120  def processed_files(self):
121    return self._processed_files
122
123def add_value(chart_data, graph_title, trace_title, value, units,
124              improvement_direction='down', important=True):
125  chart_data['charts'].setdefault(graph_title, {})
126  chart_data['charts'][graph_title][trace_title] = {
127      'type': 'scalar',
128      'value': value,
129      'units': units,
130      'imporvement_direction': improvement_direction,
131      'important': important
132  }
133
134def chartjson_size_info(apk, output_dir):
135  """Sends size information to perf dashboard.
136
137  Args:
138    apk: ApkSizeInfo object
139  """
140  data = _BASE_CHART.copy()
141  files = apk.processed_files
142  add_value(data, 'files', 'total', apk.total_files, 'count')
143  add_value(data, 'size', 'total_size_compressed', apk.compressed_size, 'bytes')
144  add_value(data, 'size', 'total_size_uncompressed', apk.uncompressed_size,
145            'bytes')
146  add_value(data, 'size', 'apk_overhead', apk.apk_size - apk.compressed_size,
147           'bytes')
148  for ext in files:
149    add_value(data, 'files', ext, files[ext]['number'], 'count')
150    add_value(data, 'size_compressed', ext, files[ext]['compressed_bytes'],
151              'bytes')
152    add_value(data, 'size_uncompressed', ext, files[ext]['uncompressed_bytes'],
153              'bytes')
154
155  logging.info('Outputing data to json file %s', output_dir)
156  with open(os.path.join(output_dir, 'results-chart.json'), 'w') as json_file:
157    json.dump(data, json_file)
158
159def print_human_readable_size_info(apk):
160  """Prints size information in human readable format.
161
162  Args:
163    apk: ApkSizeInfo object
164  """
165  files = apk.processed_files
166  logging.critical('Stats for files as they exist within the apk:')
167  for ext in files:
168    logging.critical('  %-8s %s bytes in %s files', ext,
169                     files[ext]['compressed_bytes'], files[ext]['number'])
170  logging.critical('--------------------------------------')
171  logging.critical(
172      'All Files: %s bytes in %s files', apk.compressed_size, apk.total_files)
173  logging.critical('APK Size: %s', apk.apk_size)
174  logging.critical('APK overhead: %s', apk.apk_size - apk.compressed_size)
175  logging.critical('--------------------------------------')
176  logging.critical('Stats for files when extracted from the apk:')
177  for ext in files:
178    logging.critical('  %-8s %s bytes in %s files', ext,
179                     files[ext]['uncompressed_bytes'], files[ext]['number'])
180  logging.critical('--------------------------------------')
181  logging.critical(
182      'All Files: %s bytes in %s files', apk.uncompressed_size, apk.total_files)
183
184def chartjson_compare(compare_dict, output_dir):
185  """Sends size comparison between two apks to perf dashboard.
186
187  Args:
188    compare_dict: Dictionary returned from APkSizeInfo.Compare()
189  """
190  data = _BASE_CHART.copy()
191  for key, value in compare_dict.iteritems():
192    add_value(data, 'compare', key, value, 'bytes')
193
194  logging.info('Outputing data to json file %s', output_dir)
195  with open(os.path.join(output_dir, 'results-chart.json'), 'w') as json_file:
196    json.dump(data, json_file)
197
198def print_human_readable_compare(compare_dict):
199  """Prints size comparison between two apks in human readable format.
200
201  Args:
202    compare_dict: Dictionary returned from ApkSizeInfo.Compare()
203  """
204  for key, value in compare_dict.iteritems():
205    logging.critical('  %-50s %s bytes', key, value)
206
207def main():
208  parser = argparse.ArgumentParser()
209  parser.add_argument('file_path')
210  parser.add_argument('-c', '--compare', help='APK to compare against.')
211  parser.add_argument('-o', '--output-dir',
212                      help='Sets it to return data in bot readable format')
213  parser.add_argument('-d', '--device', help='Dummy option for perf runner.')
214  args = parser.parse_args()
215
216  apk = ApkSizeInfo(args.file_path)
217  if args.compare:
218    compare_dict = apk.Compare(ApkSizeInfo(args.compare))
219    print_human_readable_compare(compare_dict)
220    if args.output_dir:
221      chartjson_compare(compare_dict, args.output_dir)
222  else:
223    print_human_readable_size_info(apk)
224    if args.output_dir:
225       chartjson_size_info(apk, args.output_dir)
226
227if __name__ == '__main__':
228  sys.exit(main())
229