Move ObserverList to base namespace.
[chromium-blink-merge.git] / tools / profile_chrome_startup.py
blob850df86379a340e361c0501ddf8c258f4abd5f38
1 #!/usr/bin/env python
3 # Copyright 2015 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 import logging
8 import optparse
9 import os
10 import sys
11 import webbrowser
13 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir,
14 'build', 'android'))
16 from profile_chrome import chrome_startup_controller
17 from profile_chrome import controllers
18 from profile_chrome import flags
19 from profile_chrome import profiler
20 from profile_chrome import systrace_controller
21 from profile_chrome import ui
22 from pylib.device import device_utils
25 def _CreateOptionParser():
26 parser = optparse.OptionParser(description='Record about://tracing profiles '
27 'from Android browsers startup, combined with '
28 'Android systrace. See http://dev.chromium.org'
29 '/developers/how-tos/trace-event-profiling-'
30 'tool for detailed instructions for '
31 'profiling.')
32 parser.add_option('--url', help='URL to visit on startup. Default: '
33 'https://www.google.com', default='https://www.google.com',
34 metavar='URL')
35 parser.add_option('--cold', help='Flush the OS page cache before starting the'
36 ' browser. Note that this require a device with root '
37 'access.', default=False, action='store_true')
38 parser.add_option_group(flags.SystraceOptions(parser))
39 parser.add_option_group(flags.OutputOptions(parser))
41 browsers = sorted(profiler.GetSupportedBrowsers().keys())
42 parser.add_option('-b', '--browser', help='Select among installed browsers. '
43 'One of ' + ', '.join(browsers) + ', "stable" is used by '
44 'default.', type='choice', choices=browsers,
45 default='stable')
46 parser.add_option('-v', '--verbose', help='Verbose logging.',
47 action='store_true')
48 parser.add_option('-z', '--compress', help='Compress the resulting trace '
49 'with gzip. ', action='store_true')
50 parser.add_option('-t', '--time', help='Stops tracing after N seconds, 0 to '
51 'manually stop (startup trace ends after at most 5s).',
52 default=5, metavar='N', type='int')
53 return parser
56 def main():
57 parser = _CreateOptionParser()
58 options, _ = parser.parse_args()
60 if options.verbose:
61 logging.getLogger().setLevel(logging.DEBUG)
63 devices = device_utils.DeviceUtils.HealthyDevices()
64 if len(devices) != 1:
65 logging.error('Exactly 1 device must be attached.')
66 return 1
67 device = devices[0]
68 package_info = profiler.GetSupportedBrowsers()[options.browser]
70 if options.systrace_categories in ['list', 'help']:
71 ui.PrintMessage('\n'.join(
72 systrace_controller.SystraceController.GetCategories(device)))
73 return 0
74 systrace_categories = (options.systrace_categories.split(',')
75 if options.systrace_categories else [])
76 enabled_controllers = []
77 # Enable the systrace and chrome controller. The systrace controller should go
78 # first because otherwise the resulting traces miss early systrace data.
79 if systrace_categories:
80 enabled_controllers.append(systrace_controller.SystraceController(
81 device, systrace_categories, False))
82 enabled_controllers.append(
83 chrome_startup_controller.ChromeStartupTracingController(
84 device, package_info, options.cold, options.url))
85 if options.output:
86 options.output = os.path.expanduser(options.output)
87 result = profiler.CaptureProfile(enabled_controllers,
88 options.time,
89 output=options.output,
90 compress=options.compress,
91 write_json=options.json)
92 if options.view:
93 if sys.platform == 'darwin':
94 os.system('/usr/bin/open %s' % os.path.abspath(result))
95 else:
96 webbrowser.open(result)
99 if __name__ == '__main__':
100 sys.exit(main())