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