| OLD | NEW |
| 1 #!/usr/bin/env python |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 4 | 5 |
| 5 """Utility methods.""" | 6 """Collect, archive, and analyze Chrome's binary size.""" |
| 6 | 7 |
| 8 import argparse |
| 7 import atexit | 9 import atexit |
| 10 import collections |
| 8 import distutils.spawn | 11 import distutils.spawn |
| 9 import logging | 12 import logging |
| 10 import os | 13 import os |
| 11 import platform | 14 import platform |
| 12 import resource | 15 import resource |
| 13 import sys | 16 import sys |
| 14 | 17 |
| 15 | 18 import archive |
| 16 SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | 19 import console |
| 20 import html_report |
| 17 | 21 |
| 18 | 22 |
| 19 def AddCommonOptionsAndParseArgs(parser, argv, pypy_warn=True): | 23 def _LogPeakRamUsage(): |
| 24 peak_ram_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss |
| 25 peak_ram_usage += resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss |
| 26 logging.info('Peak RAM usage was %d MB.', peak_ram_usage / 1024) |
| 27 |
| 28 |
| 29 def _AddCommonArguments(parser): |
| 20 parser.add_argument('--no-pypy', action='store_true', | 30 parser.add_argument('--no-pypy', action='store_true', |
| 21 help='Do not automatically switch to pypy when available') | 31 help='Do not automatically switch to pypy when available') |
| 22 parser.add_argument('-v', | 32 parser.add_argument('-v', |
| 23 '--verbose', | 33 '--verbose', |
| 24 default=0, | 34 default=0, |
| 25 action='count', | 35 action='count', |
| 26 help='Verbose level (multiple times for more)') | 36 help='Verbose level (multiple times for more)') |
| 27 args = parser.parse_args(argv[1:]) | |
| 28 | 37 |
| 38 |
| 39 def main(): |
| 40 parser = argparse.ArgumentParser(description=__doc__) |
| 41 sub_parsers = parser.add_subparsers() |
| 42 actions = collections.OrderedDict() |
| 43 actions['archive'] = (archive, 'Create a .size file') |
| 44 actions['html_report'] = ( |
| 45 html_report, 'Create a stand-alone html report from a .size file.') |
| 46 actions['console'] = ( |
| 47 console, |
| 48 'Starts an interactive Python console for analyzing .size files.') |
| 49 |
| 50 for name, tup in actions.iteritems(): |
| 51 sub_parser = sub_parsers.add_parser(name, help=tup[1]) |
| 52 _AddCommonArguments(sub_parser) |
| 53 tup[0].AddArguments(sub_parser) |
| 54 sub_parser.set_defaults(func=tup[0].Run) |
| 55 |
| 56 if len(sys.argv) == 1: |
| 57 parser.print_help() |
| 58 sys.exit(1) |
| 59 elif len(sys.argv) == 2 and sys.argv[1] in actions: |
| 60 parser.parse_args(sys.argv[1:] + ['-h']) |
| 61 sys.exit(1) |
| 62 |
| 63 args = parser.parse_args() |
| 29 logging.basicConfig(level=logging.WARNING - args.verbose * 10, | 64 logging.basicConfig(level=logging.WARNING - args.verbose * 10, |
| 30 format='%(levelname).1s %(relativeCreated)6d %(message)s') | 65 format='%(levelname).1s %(relativeCreated)6d %(message)s') |
| 31 | 66 |
| 32 if not args.no_pypy and platform.python_implementation() == 'CPython': | 67 if not args.no_pypy and platform.python_implementation() == 'CPython': |
| 33 # Switch to pypy if it's available. | 68 # Switch to pypy if it's available. |
| 34 pypy_path = distutils.spawn.find_executable('pypy') | 69 pypy_path = distutils.spawn.find_executable('pypy') |
| 35 if pypy_path: | 70 if pypy_path: |
| 36 logging.debug('Switching to pypy.') | 71 logging.debug('Switching to pypy.') |
| 37 os.execv(pypy_path, [pypy_path] + sys.argv) | 72 os.execv(pypy_path, [pypy_path] + sys.argv) |
| 38 # NOTE! Running with python: 6s. Running with pypy: 3s | 73 # Running with python: 6s. Running with pypy: 3s |
| 39 if pypy_warn: | 74 logging.warning('This script runs more than 2x faster if you install pypy.') |
| 40 logging.warning( | |
| 41 'This script runs more than 2x faster if you install pypy.') | |
| 42 | 75 |
| 43 if logging.getLogger().isEnabledFor(logging.DEBUG): | 76 if logging.getLogger().isEnabledFor(logging.DEBUG): |
| 44 atexit.register(_LogPeakRamUsage) | 77 atexit.register(_LogPeakRamUsage) |
| 45 return args | 78 |
| 79 args.func(args, parser) |
| 46 | 80 |
| 47 | 81 |
| 48 def _LogPeakRamUsage(): | 82 if __name__ == '__main__': |
| 49 peak_ram_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | 83 sys.exit(main()) |
| 50 peak_ram_usage += resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss | |
| 51 logging.info('Peak RAM usage was %d MB.', peak_ram_usage / 1024) | |
| OLD | NEW |