Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: tools/android/loading/wpr_helper.py

Issue 2687803004: [tools/android/loading] Helper script running Chrome on device with WPR (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 #
3 # Copyright 2015 The Chromium Authors. All rights reserved.
alexilin 2017/02/10 14:37:22 nit: s/2015/2017/
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """Helper script to launch Chrome on device and WebPageReplay on host."""
8
9 import logging
10 import optparse
11 import os
12 import subprocess
13 import sys
14 import time
15
16 _SRC_PATH = os.path.abspath(os.path.join(
17 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir))
18
19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
20 from devil.android import device_utils
21 from devil.android.constants import chrome
22 from devil.android.perf import cache_control
23 from devil.android.sdk import intent
24
25 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
26 import devil_chromium
27
28 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading'))
29 import device_setup
30
31
32 # Command line arguments for Chrome.
33 _CHROME_ARGS = [
34 # Disable backgound network requests that may pollute WPR archive, pollute
35 # HTTP cache generation, and introduce noise in loading performance.
36 '--disable-background-networking',
37 '--disable-default-apps',
38 '--no-proxy-server',
39 # TODO(droger): Remove once crbug.com/354743 is fixed.
40 '--safebrowsing-disable-auto-update',
41
42 # Disables actions that chrome performs only on first run or each launches,
43 # which can interfere with page load performance, or even block its
44 # execution by waiting for user input.
45 '--disable-fre',
46 '--no-default-browser-check',
47 '--no-first-run',
48 ]
49
50
51 def ResetChromeLocalState(device, package):
52 """Remove the Chrome Profile and the various disk caches."""
53 profile_dirs = ['app_chrome/Default', 'cache', 'app_chrome/ShaderCache',
54 'app_tabs']
55 cmd = ['rm', '-rf']
56 cmd.extend(
57 '/data/data/{}/{}'.format(package, d) for d in profile_dirs)
58 device.adb.Shell(subprocess.list2cmdline(cmd))
59
60
61 def RunChrome(device, cold, chrome_args, package_info):
62 """Runs Chrome on the device.
63
64 Args:
65 device: (DeviceUtils) device to run the tests on.
66 cold: (bool) Whether caches should be dropped.
67 chrome_args: ([str]) List of arguments to pass to Chrome.
68 package_info: (PackageInfo) Chrome package info.
69 """
70 if not device.HasRoot():
71 device.EnableRoot()
72
73 cmdline_file = package_info.cmdline_file
74 package = package_info.package
75 with device_setup.FlagReplacer(device, cmdline_file, chrome_args):
76 device.ForceStop(package)
77
78 if cold:
79 ResetChromeLocalState(device, package)
80 cache_control.CacheControl(device).DropRamCaches()
81
82 start_intent = intent.Intent(package=package, data='about:blank',
83 activity=package_info.activity)
84 try:
85 device.StartActivity(start_intent, blocking=True)
86 print(
87 '\n\n'
88 ' +---------------------------------------------+\n'
89 ' | Chrome launched, press Ctrl-C to interrupt. |\n'
90 ' +---------------------------------------------+')
91 while True:
92 time.sleep(1)
93 except KeyboardInterrupt:
94 pass
95 finally:
96 device.ForceStop(package)
97
98
99 def _CreateOptionParser():
100 description = 'Launches Chrome on a device, connected to a WebPageReplay ' \
101 'instance running on the host. The WPR archive must be ' \
102 'passed as parameter.'
103 parser = optparse.OptionParser(description=description,
104 usage='Usage: %prog [options] wpr_archive')
105
106 # Device-related options.
107 d = optparse.OptionGroup(parser, 'Device options')
108 d.add_option('--device', help='Device ID')
109 d.add_option('--cold', help='Purge all caches before running Chrome.',
110 default=False, action='store_true')
111 d.add_option('--chrome_package_name',
112 help='Chrome package name (e.g. "chrome" or "chromium") '
113 '[default: %default].', default='chrome')
114 parser.add_option_group(d)
115
116 # WebPageReplay-related options.
117 w = optparse.OptionGroup(parser, 'WebPageReplay options')
118 w.add_option('--record',
119 help='Enable this to record a new WPR archive.',
120 action='store_true', default=False)
121 w.add_option('--wpr_log', help='WPR log path.')
122 w.add_option('--network_condition', help='Network condition for emulation.')
123 parser.add_option_group(w)
124
125 return parser
126
127
128 def main():
129 parser = _CreateOptionParser()
130 options, args = parser.parse_args()
131 if len(args) != 1:
132 parser.error("Incorrect number of arguments.")
133 devil_chromium.Initialize()
134 devices = device_utils.DeviceUtils.HealthyDevices()
135 device = devices[0]
136 if len(devices) != 1 and options.device is None:
137 logging.error('Several devices attached, must specify one with --device.')
138 sys.exit(0)
139 if options.device is not None:
140 matching_devices = [d for d in devices if str(d) == options.device]
141 if not matching_devices:
142 logging.error('Device not found.')
143 sys.exit(0)
144 device = matching_devices[0]
145
146 with device_setup.RemoteWprHost(device, args[0], options.record,
147 options.network_condition,
148 out_log_path=options.wpr_log) as wpr_attr:
149 RunChrome(device, options.cold, _CHROME_ARGS + wpr_attr.chrome_args,
150 chrome.PACKAGE_INFO[options.chrome_package_name])
151
152
153 if __name__ == '__main__':
154 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698