Peter Wen | 0cea52b | 2021-08-18 16:11:06 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame^] | 2 | # Copyright 2017 The Chromium Authors |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Runs resource_sizes.py on two apks and outputs the diff.""" |
| 7 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 10 | import argparse |
| 11 | import json |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 12 | import logging |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | from pylib.constants import host_paths |
| 18 | from pylib.utils import shared_preference_utils |
| 19 | |
| 20 | with host_paths.SysPath(host_paths.BUILD_COMMON_PATH): |
| 21 | import perf_tests_results_helper # pylint: disable=import-error |
| 22 | |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 23 | with host_paths.SysPath(host_paths.TRACING_PATH): |
| 24 | from tracing.value import convert_chart_json # pylint: disable=import-error |
| 25 | |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 26 | _ANDROID_DIR = os.path.dirname(os.path.abspath(__file__)) |
Brian Sheedy | 45ea5b9 | 2019-04-16 17:37:48 | [diff] [blame] | 27 | with host_paths.SysPath(os.path.join(_ANDROID_DIR, 'gyp')): |
| 28 | from util import build_utils # pylint: disable=import-error |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 29 | |
| 30 | |
| 31 | _BASE_CHART = { |
| 32 | 'format_version': '0.1', |
| 33 | 'benchmark_name': 'resource_sizes_diff', |
| 34 | 'benchmark_description': 'APK resource size diff information', |
| 35 | 'trace_rerun_options': [], |
| 36 | 'charts': {}, |
| 37 | } |
| 38 | |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 39 | _CHARTJSON_FILENAME = 'results-chart.json' |
| 40 | _HISTOGRAMS_FILENAME = 'perf_results.json' |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def DiffResults(chartjson, base_results, diff_results): |
| 44 | """Reports the diff between the two given results. |
| 45 | |
| 46 | Args: |
| 47 | chartjson: A dictionary that chartjson results will be placed in, or None |
| 48 | to only print results. |
| 49 | base_results: The chartjson-formatted size results of the base APK. |
| 50 | diff_results: The chartjson-formatted size results of the diff APK. |
| 51 | """ |
Ben Joyce | d782295 | 2021-08-17 17:19:03 | [diff] [blame] | 52 | for graph_title, graph in base_results['charts'].items(): |
| 53 | for trace_title, trace in graph.items(): |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 54 | perf_tests_results_helper.ReportPerfResult( |
| 55 | chartjson, graph_title, trace_title, |
| 56 | diff_results['charts'][graph_title][trace_title]['value'] |
| 57 | - trace['value'], |
| 58 | trace['units'], trace['improvement_direction'], |
| 59 | trace['important']) |
| 60 | |
| 61 | |
| 62 | def AddIntermediateResults(chartjson, base_results, diff_results): |
| 63 | """Copies the intermediate size results into the output chartjson. |
| 64 | |
| 65 | Args: |
| 66 | chartjson: A dictionary that chartjson results will be placed in. |
| 67 | base_results: The chartjson-formatted size results of the base APK. |
| 68 | diff_results: The chartjson-formatted size results of the diff APK. |
| 69 | """ |
Ben Joyce | d782295 | 2021-08-17 17:19:03 | [diff] [blame] | 70 | for graph_title, graph in base_results['charts'].items(): |
| 71 | for trace_title, trace in graph.items(): |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 72 | perf_tests_results_helper.ReportPerfResult( |
| 73 | chartjson, graph_title + '_base_apk', trace_title, |
| 74 | trace['value'], trace['units'], trace['improvement_direction'], |
| 75 | trace['important']) |
| 76 | |
| 77 | # Both base_results and diff_results should have the same charts/traces, but |
| 78 | # loop over them separately in case they don't |
Ben Joyce | d782295 | 2021-08-17 17:19:03 | [diff] [blame] | 79 | for graph_title, graph in diff_results['charts'].items(): |
| 80 | for trace_title, trace in graph.items(): |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 81 | perf_tests_results_helper.ReportPerfResult( |
| 82 | chartjson, graph_title + '_diff_apk', trace_title, |
| 83 | trace['value'], trace['units'], trace['improvement_direction'], |
| 84 | trace['important']) |
| 85 | |
| 86 | |
| 87 | def _CreateArgparser(): |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 88 | def chromium_path(arg): |
| 89 | if arg.startswith('//'): |
| 90 | return os.path.join(host_paths.DIR_SOURCE_ROOT, arg[2:]) |
| 91 | return arg |
| 92 | |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 93 | argparser = argparse.ArgumentParser( |
| 94 | description='Diff resource sizes of two APKs. Arguments not listed here ' |
| 95 | 'will be passed on to both invocations of resource_sizes.py.') |
| 96 | argparser.add_argument('--chromium-output-directory-base', |
| 97 | dest='out_dir_base', |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 98 | type=chromium_path, |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 99 | help='Location of the build artifacts for the base ' |
| 100 | 'APK, i.e. what the size increase/decrease will ' |
| 101 | 'be measured from.') |
| 102 | argparser.add_argument('--chromium-output-directory-diff', |
| 103 | dest='out_dir_diff', |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 104 | type=chromium_path, |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 105 | help='Location of the build artifacts for the diff ' |
| 106 | 'APK.') |
| 107 | argparser.add_argument('--chartjson', |
| 108 | action='store_true', |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 109 | help='DEPRECATED. Use --output-format=chartjson ' |
| 110 | 'instead.') |
| 111 | argparser.add_argument('--output-format', |
| 112 | choices=['chartjson', 'histograms'], |
| 113 | help='Output the results to a file in the given ' |
| 114 | 'format instead of printing the results.') |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 115 | argparser.add_argument('--include-intermediate-results', |
| 116 | action='store_true', |
| 117 | help='Include the results from the resource_sizes.py ' |
| 118 | 'runs in the chartjson output.') |
| 119 | argparser.add_argument('--output-dir', |
| 120 | default='.', |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 121 | type=chromium_path, |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 122 | help='Directory to save chartjson to.') |
| 123 | argparser.add_argument('--base-apk', |
| 124 | required=True, |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 125 | type=chromium_path, |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 126 | help='Path to the base APK, i.e. what the size ' |
| 127 | 'increase/decrease will be measured from.') |
| 128 | argparser.add_argument('--diff-apk', |
| 129 | required=True, |
bsheedy | 852269e | 2017-09-22 19:51:10 | [diff] [blame] | 130 | type=chromium_path, |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 131 | help='Path to the diff APK, i.e. the APK whose size ' |
| 132 | 'increase/decrease will be measured against the ' |
| 133 | 'base APK.') |
| 134 | return argparser |
| 135 | |
| 136 | |
| 137 | def main(): |
| 138 | args, unknown_args = _CreateArgparser().parse_known_args() |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 139 | # TODO(bsheedy): Remove this once all uses of --chartjson are removed. |
| 140 | if args.chartjson: |
| 141 | args.output_format = 'chartjson' |
| 142 | |
| 143 | chartjson = _BASE_CHART.copy() if args.output_format else None |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 144 | |
| 145 | with build_utils.TempDir() as base_dir, build_utils.TempDir() as diff_dir: |
| 146 | # Run resource_sizes.py on the two APKs |
| 147 | resource_sizes_path = os.path.join(_ANDROID_DIR, 'resource_sizes.py') |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 148 | shared_args = (['python', resource_sizes_path, '--output-format=chartjson'] |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 149 | + unknown_args) |
| 150 | |
| 151 | base_args = shared_args + ['--output-dir', base_dir, args.base_apk] |
| 152 | if args.out_dir_base: |
| 153 | base_args += ['--chromium-output-directory', args.out_dir_base] |
bsheedy | a9d1079 | 2017-09-23 21:47:11 | [diff] [blame] | 154 | try: |
| 155 | subprocess.check_output(base_args, stderr=subprocess.STDOUT) |
| 156 | except subprocess.CalledProcessError as e: |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 157 | print(e.output) |
bsheedy | a9d1079 | 2017-09-23 21:47:11 | [diff] [blame] | 158 | raise |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 159 | |
| 160 | diff_args = shared_args + ['--output-dir', diff_dir, args.diff_apk] |
| 161 | if args.out_dir_diff: |
bsheedy | 135012ca | 2017-09-26 00:00:40 | [diff] [blame] | 162 | diff_args += ['--chromium-output-directory', args.out_dir_diff] |
bsheedy | a9d1079 | 2017-09-23 21:47:11 | [diff] [blame] | 163 | try: |
| 164 | subprocess.check_output(diff_args, stderr=subprocess.STDOUT) |
| 165 | except subprocess.CalledProcessError as e: |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 166 | print(e.output) |
bsheedy | a9d1079 | 2017-09-23 21:47:11 | [diff] [blame] | 167 | raise |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 168 | |
| 169 | # Combine the separate results |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 170 | base_file = os.path.join(base_dir, _CHARTJSON_FILENAME) |
| 171 | diff_file = os.path.join(diff_dir, _CHARTJSON_FILENAME) |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 172 | base_results = shared_preference_utils.ExtractSettingsFromJson(base_file) |
| 173 | diff_results = shared_preference_utils.ExtractSettingsFromJson(diff_file) |
| 174 | DiffResults(chartjson, base_results, diff_results) |
| 175 | if args.include_intermediate_results: |
| 176 | AddIntermediateResults(chartjson, base_results, diff_results) |
| 177 | |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 178 | if args.output_format: |
| 179 | chartjson_path = os.path.join(os.path.abspath(args.output_dir), |
| 180 | _CHARTJSON_FILENAME) |
| 181 | logging.critical('Dumping diff chartjson to %s', chartjson_path) |
| 182 | with open(chartjson_path, 'w') as outfile: |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 183 | json.dump(chartjson, outfile) |
| 184 | |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 185 | if args.output_format == 'histograms': |
| 186 | histogram_result = convert_chart_json.ConvertChartJson(chartjson_path) |
| 187 | if histogram_result.returncode != 0: |
| 188 | logging.error('chartjson conversion failed with error: %s', |
| 189 | histogram_result.stdout) |
| 190 | return 1 |
| 191 | |
| 192 | histogram_path = os.path.join(os.path.abspath(args.output_dir), |
| 193 | 'perf_results.json') |
| 194 | logging.critical('Dumping diff histograms to %s', histogram_path) |
| 195 | with open(histogram_path, 'w') as json_file: |
| 196 | json_file.write(histogram_result.stdout) |
Yoshisato Yanagisawa | 0f42f10 | 2021-11-30 14:04:37 | [diff] [blame] | 197 | return 0 |
bsheedy | 889406d | 2018-11-01 19:43:34 | [diff] [blame] | 198 | |
| 199 | |
bsheedy | 20e696b | 2017-09-13 20:31:05 | [diff] [blame] | 200 | if __name__ == '__main__': |
| 201 | sys.exit(main()) |