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 re | |
11 import sys | |
12 import time | |
13 import webbrowser | |
14 | |
15 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, | |
16 'build', 'android')) | |
17 | |
18 from profile_chrome import controllers | |
19 from profile_chrome import flags | |
20 from profile_chrome import profiler | |
21 from profile_chrome import systrace_controller | |
22 from profile_chrome import ui | |
23 from pylib import android_commands | |
24 from pylib import flag_changer | |
25 from pylib.device import device_utils | |
26 from pylib.perf import cache_control | |
27 | |
28 class ChromeStartupTracingController(controllers.BaseController): | |
Sami
2015/01/27 17:08:33
Please add a test for this like we do with the oth
Benoit L
2015/01/27 18:41:41
Done.
| |
29 def __init__(self, device, package_info, cold, url): | |
30 self._device = device | |
31 self._package_info = package_info | |
32 self._cold = cold | |
33 self._url = url | |
34 self._trace_file = None | |
35 self._trace_finish_re = re.compile(r' Completed startup tracing to (.*)') | |
36 self._device.old_interface.StartMonitoringLogcat(clear=False) | |
37 | |
38 def __repr__(self): | |
pasko
2015/01/27 17:00:24
is this necessary?
Sami
2015/01/27 17:08:33
CaptureProfile uses it to print out the type of tr
Benoit L
2015/01/27 18:41:41
Done.
Benoit L
2015/01/27 18:41:42
No, but prettier.
In profiler.py, this gets printe
| |
39 return "Browser Startup Trace" | |
40 | |
41 def _SetupTracing(self): | |
42 changer = flag_changer.FlagChanger( | |
43 self._device, self._package_info.cmdline_file) | |
44 changer.AddFlags(['--trace-startup']) | |
45 self._device.old_interface.CloseApplication(self._package_info.package) | |
46 if self._cold: | |
47 self._device.old_interface.EnableAdbRoot() | |
48 cache_control.CacheControl(self._device).DropRamCaches() | |
49 self._device.old_interface.StartActivity( | |
50 package=self._package_info.package, | |
51 activity=self._package_info.activity, | |
52 data=self._url, | |
53 extras={'create_new_tab' : True}) | |
54 | |
55 def StartTracing(self, interval): | |
56 self._SetupTracing() | |
57 self._device.old_interface.SyncLogCat() | |
58 | |
59 def StopTracing(self): | |
Sami
2015/01/27 17:08:32
Can you make sure to remove the --trace-startup fl
Benoit L
2015/01/27 18:41:41
Indeed, thank you for pointing this out.
Done.
pasko
2015/01/28 12:26:03
What happens if the tracing is not stopped properl
Sami
2015/01/28 12:44:14
adb_profile_chrome isn't just used for running ben
pasko
2015/01/28 12:50:53
Agreed, when startup is not affected, pushing the
| |
60 self._trace_file = self._device.old_interface.WaitForLogMatch( | |
61 self._trace_finish_re, None, timeout=120).group(1) | |
62 | |
63 def PullTrace(self): | |
64 # Wait a bit for the browser to finish writing the trace file. | |
65 time.sleep(3) | |
66 trace_file = self._trace_file.replace('/storage/emulated/0/', '/sdcard/') | |
67 host_file = os.path.join(os.path.curdir, os.path.basename(trace_file)) | |
68 self._device.PullFile(trace_file, host_file) | |
69 return host_file | |
70 | |
71 | |
72 def _CreateOptionParser(): | |
73 parser = optparse.OptionParser(description='Record about://tracing profiles ' | |
74 'from Android browsers startup, combined with ' | |
75 'Android systrace. See http://dev.chromium.org' | |
76 '/developers/how-tos/trace-event-profiling-' | |
77 'tool for detailed instructions for ' | |
78 'profiling.') | |
79 parser.add_option('--kind', help='Kind of startup. Possible values are "warm"' | |
80 ' and "cold". The "warm" start does not perform special ' | |
81 'steps, while the "cols" flushes the OS page cache before ' | |
pasko
2015/01/27 17:00:24
s/cols/cold/
this option seems to be ignored and
Benoit L
2015/01/27 18:41:41
You're right, oops.
Done.
| |
82 'start. Note that "cold" requires a device with root ' | |
83 'access.', default="warm", choices=['warm', 'cold']) | |
84 parser.add_option('--url', help='URL to visit on startup. Default: ' | |
85 'https://wwww.google.com', default='https://www.google.com', | |
86 metavar='URL') | |
87 parser.add_option('--cold', help='Flush the OS page cache before starting the' | |
88 ' browser. Note that this require a device with root ' | |
89 'access.', default=False, action='store_true') | |
90 parser.add_option_group(flags.SystraceOptions(parser)) | |
91 parser.add_option_group(flags.OutputOptions(parser)) | |
92 | |
93 browsers = sorted(profiler.GetSupportedBrowsers().keys()) | |
94 parser.add_option('-b', '--browser', help='Select among installed browsers. ' | |
pasko
2015/01/27 17:00:24
maybe these options could also be factored out?
Benoit L
2015/01/27 18:41:41
It seemed to be more trouble than necessary, but I
pasko
2015/01/28 12:26:04
Ack, good enough for now.
| |
95 'One of ' + ', '.join(browsers) + ', "stable" is used by ' | |
96 'default.', type='choice', choices=browsers, | |
97 default='stable') | |
98 parser.add_option('-v', '--verbose', help='Verbose logging.', | |
99 action='store_true') | |
100 parser.add_option('-z', '--compress', help='Compress the resulting trace ' | |
101 'with gzip. ', action='store_true') | |
102 return parser | |
103 | |
104 | |
105 def main(): | |
106 parser = _CreateOptionParser() | |
107 options, _ = parser.parse_args() | |
108 | |
109 if options.verbose: | |
110 logging.getLogger().setLevel(logging.DEBUG) | |
111 | |
112 devices = android_commands.GetAttachedDevices() | |
113 if len(devices) != 1: | |
114 logging.error('Exactly 1 device must be attached.') | |
115 return 1 | |
116 device = device_utils.DeviceUtils(devices[0]) | |
117 package_info = profiler.GetSupportedBrowsers()[options.browser] | |
118 | |
119 if options.systrace_categories in ['list', 'help']: | |
120 ui.PrintMessage('\n'.join( | |
121 systrace_controller.SystraceController.GetCategories(device))) | |
122 return 0 | |
123 systrace_categories = options.systrace_categories.split(',') | |
124 | |
125 enabled_controllers = [] | |
126 # Enable the systrace and chrome controller. The systrace controller should go | |
127 # first because otherwise the resulting traces miss early systrace data. | |
128 if systrace_categories: | |
129 enabled_controllers.append(systrace_controller.SystraceController( | |
130 device, systrace_categories, False)) | |
131 enabled_controllers.append( | |
132 ChromeStartupTracingController(device, package_info, options.cold, | |
133 options.url)) | |
134 if options.output: | |
135 options.output = os.path.expanduser(options.output) | |
136 result = profiler.CaptureProfile(enabled_controllers, 0, | |
137 output=options.output, | |
138 compress=options.compress, | |
139 write_json=options.json) | |
140 if options.view: | |
141 if sys.platform == 'darwin': | |
142 os.system('/usr/bin/open %s' % os.path.abspath(result)) | |
143 else: | |
144 webbrowser.open(result) | |
145 | |
146 | |
147 if __name__ == '__main__': | |
148 sys.exit(main()) | |
OLD | NEW |