Move BrowserCloseTest.* over to unit tests.
[chromium-blink-merge.git] / tools / profile_chrome_startup.py
blob0f84711e18aaae9341ad5773f1bd07c97fa13cef
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. An empty URL launches Chrome with'
34 ' a MAIN action instead of VIEW.',
35 default='https://www.google.com', metavar='URL')
36 parser.add_option('--cold', help='Flush the OS page cache before starting the'
37 ' browser. Note that this require a device with root '
38 'access.', default=False, action='store_true')
39 parser.add_option_group(flags.SystraceOptions(parser))
40 parser.add_option_group(flags.OutputOptions(parser))
42 browsers = sorted(profiler.GetSupportedBrowsers().keys())
43 parser.add_option('-b', '--browser', help='Select among installed browsers. '
44 'One of ' + ', '.join(browsers) + ', "stable" is used by '
45 'default.', type='choice', choices=browsers,
46 default='stable')
47 parser.add_option('-v', '--verbose', help='Verbose logging.',
48 action='store_true')
49 parser.add_option('-z', '--compress', help='Compress the resulting trace '
50 'with gzip. ', action='store_true')
51 parser.add_option('-t', '--time', help='Stops tracing after N seconds, 0 to '
52 'manually stop (startup trace ends after at most 5s).',
53 default=5, metavar='N', type='int')
54 return parser
57 def main():
58 parser = _CreateOptionParser()
59 options, _ = parser.parse_args()
61 if options.verbose:
62 logging.getLogger().setLevel(logging.DEBUG)
64 devices = device_utils.DeviceUtils.HealthyDevices()
65 if len(devices) != 1:
66 logging.error('Exactly 1 device must be attached.')
67 return 1
68 device = devices[0]
69 package_info = profiler.GetSupportedBrowsers()[options.browser]
71 if options.systrace_categories in ['list', 'help']:
72 ui.PrintMessage('\n'.join(
73 systrace_controller.SystraceController.GetCategories(device)))
74 return 0
75 systrace_categories = (options.systrace_categories.split(',')
76 if options.systrace_categories else [])
77 enabled_controllers = []
78 # Enable the systrace and chrome controller. The systrace controller should go
79 # first because otherwise the resulting traces miss early systrace data.
80 if systrace_categories:
81 enabled_controllers.append(systrace_controller.SystraceController(
82 device, systrace_categories, False))
83 enabled_controllers.append(
84 chrome_startup_controller.ChromeStartupTracingController(
85 device, package_info, options.cold, options.url))
86 if options.output:
87 options.output = os.path.expanduser(options.output)
88 result = profiler.CaptureProfile(enabled_controllers,
89 options.time,
90 output=options.output,
91 compress=options.compress,
92 write_json=options.json)
93 if options.view:
94 if sys.platform == 'darwin':
95 os.system('/usr/bin/open %s' % os.path.abspath(result))
96 else:
97 webbrowser.open(result)
100 if __name__ == '__main__':
101 sys.exit(main())