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.
13 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), os
.pardir
,
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
import android_commands
23 from pylib
.device
import device_utils
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 '
33 parser
.add_option('--url', help='URL to visit on startup. Default: '
34 'https://www.google.com', default
='https://www.google.com',
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
,
47 parser
.add_option('-v', '--verbose', help='Verbose logging.',
49 parser
.add_option('-z', '--compress', help='Compress the resulting trace '
50 'with gzip. ', action
='store_true')
55 parser
= _CreateOptionParser()
56 options
, _
= parser
.parse_args()
59 logging
.getLogger().setLevel(logging
.DEBUG
)
61 devices
= android_commands
.GetAttachedDevices()
63 logging
.error('Exactly 1 device must be attached.')
65 device
= device_utils
.DeviceUtils(devices
[0])
66 package_info
= profiler
.GetSupportedBrowsers()[options
.browser
]
68 if options
.systrace_categories
in ['list', 'help']:
69 ui
.PrintMessage('\n'.join(
70 systrace_controller
.SystraceController
.GetCategories(device
)))
72 systrace_categories
= (options
.systrace_categories
.split(',')
73 if options
.systrace_categories
else [])
74 enabled_controllers
= []
75 # Enable the systrace and chrome controller. The systrace controller should go
76 # first because otherwise the resulting traces miss early systrace data.
77 if systrace_categories
:
78 enabled_controllers
.append(systrace_controller
.SystraceController(
79 device
, systrace_categories
, False))
80 enabled_controllers
.append(
81 chrome_startup_controller
.ChromeStartupTracingController(
82 device
, package_info
, options
.cold
, options
.url
))
84 options
.output
= os
.path
.expanduser(options
.output
)
85 result
= profiler
.CaptureProfile(enabled_controllers
, 0,
86 output
=options
.output
,
87 compress
=options
.compress
,
88 write_json
=options
.json
)
90 if sys
.platform
== 'darwin':
91 os
.system('/usr/bin/open %s' % os
.path
.abspath(result
))
93 webbrowser
.open(result
)
96 if __name__
== '__main__':