Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # | |
| 3 # Copyright 2016 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 """Loads a set of web pages several times on a device, and extracts the | |
| 8 predictor database. | |
| 9 """ | |
| 10 | |
| 11 import logging | |
| 12 import optparse | |
| 13 import os | |
| 14 import subprocess | |
| 15 import sys | |
| 16 import time | |
| 17 | |
| 18 _SRC_PATH = os.path.abspath(os.path.join( | |
| 19 os.path.dirname(__file__), os.pardir, os.pardir)) | |
| 20 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) | |
| 21 | |
| 22 from devil.android import device_utils | |
| 23 from devil.android import flag_changer | |
| 24 from devil.android.constants import chrome | |
| 25 from devil.android.sdk import intent | |
| 26 | |
| 27 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) | |
| 28 import devil_chromium | |
| 29 | |
| 30 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) | |
| 31 import device_setup | |
|
pasko
2016/11/10 16:02:00
do we need this dependency?
Benoit L
2016/11/14 12:49:25
Done.
| |
| 32 | |
| 33 | |
| 34 _PAGE_LOAD_TIMEOUT = 20 | |
| 35 | |
| 36 # Local build of Chrome (not Chromium). | |
| 37 _PACKAGE_INFO = chrome.PACKAGE_INFO['chrome'] | |
| 38 | |
| 39 # Command line arguments for Chrome. | |
| 40 _CHROME_FLAGS = [ | |
| 41 # Disable backgound network requests that may pollute WPR archive, pollute | |
| 42 # HTTP cache generation, and introduce noise in loading performance. | |
|
pasko
2016/11/10 16:02:00
this is getting repeated, should some sort of comm
Benoit L
2016/11/14 12:49:25
Done.
| |
| 43 '--disable-background-networking', | |
| 44 '--disable-default-apps', | |
| 45 '--no-proxy-server', | |
| 46 '--safebrowsing-disable-auto-update', | |
| 47 | |
| 48 # Disables actions that chrome performs only on first run or each launches, | |
| 49 # which can interfere with page load performance, or even block its | |
| 50 # execution by waiting for user input. | |
| 51 '--disable-fre', | |
| 52 '--no-default-browser-check', | |
| 53 '--no-first-run', | |
| 54 | |
| 55 '--speculative-resource-prefetching=learning', | |
| 56 ] | |
| 57 | |
| 58 | |
| 59 def _CreateOptionParser(): | |
| 60 parser = optparse.OptionParser( | |
| 61 description='Loads a set of web pages several times on a device, and ' | |
| 62 'extracts the predictor database.') | |
| 63 parser.add_option('--device', help='Device ID') | |
| 64 parser.add_option('--urls_filename', help='File containing a list of URLs ' | |
| 65 '(one per line). URLs can be repeated.') | |
| 66 parser.add_option('--output_filename', help='File to store the database in.') | |
| 67 parser.add_option('--repeats', help='Number of times each URL is loaded.', | |
|
pasko
2016/11/10 16:02:01
nit: s/repeats/url-repeat/ sounds more consistent
Benoit L
2016/11/14 12:49:25
Done.
| |
| 68 default=3) | |
| 69 return parser | |
| 70 | |
| 71 | |
| 72 def _FindDevice(device_id): | |
| 73 """Returns a device matching |device_id| or the first one if None, or None.""" | |
| 74 devices = device_utils.DeviceUtils.HealthyDevices() | |
| 75 if device_id is None: | |
| 76 return devices[0] | |
| 77 matching_devices = [d for d in devices if str(d) == device_id] | |
| 78 if not matching_devices: | |
| 79 return None | |
| 80 return matching_devices[0] | |
| 81 | |
| 82 | |
| 83 def _ResetChromeLocalState(device, package_name): | |
| 84 """Removes the Chrome Profile and the various disk caches.""" | |
| 85 profile_dirs = ['app_chrome/Default', 'cache', 'app_chrome/ShaderCache', | |
|
pasko
2016/11/10 16:02:00
Do you want to avoid dependency on tools/android/l
Benoit L
2016/11/14 12:49:25
Acknowledged.
| |
| 86 'app_tabs'] | |
| 87 cmd = ['rm', '-rf'] | |
| 88 cmd.extend('/data/user/0/%s/%s' % (package_name, d) for d in profile_dirs) | |
| 89 device.adb.Shell(subprocess.list2cmdline(cmd)) | |
| 90 | |
| 91 | |
| 92 def _Setup(device): | |
| 93 """Configures the device.""" | |
| 94 if not device.HasRoot(): | |
| 95 device.EnableRoot() | |
| 96 changer = flag_changer.FlagChanger( | |
| 97 device, '/data/local/tmp/chrome-command-line') | |
|
pasko
2016/11/10 16:02:00
the controller.py is doing it with /data/local/chr
Benoit L
2016/11/14 12:49:25
Done.
| |
| 98 changer.ReplaceFlags(_CHROME_FLAGS) | |
|
pasko
2016/11/10 16:02:00
I like the FlagReplacer contextmanager, getting th
Benoit L
2016/11/14 12:49:25
Acknowledged.
| |
| 99 device.ForceStop(_PACKAGE_INFO.package) | |
| 100 _ResetChromeLocalState(device, _PACKAGE_INFO.package) | |
| 101 | |
| 102 | |
| 103 def _Go(device, urls_filename, output_filename, repeats): | |
| 104 urls = [] | |
| 105 with open(urls_filename) as f: | |
| 106 urls = [line.strip() for line in f.readlines()] | |
| 107 | |
| 108 for repeat in range(repeats): | |
| 109 logging.info('Repeat #%d', repeat) | |
| 110 for url in urls: | |
| 111 logging.info('\tURL: %s', url) | |
| 112 view_intent = intent.Intent(action='android.intent.action.VIEW', | |
| 113 package=_PACKAGE_INFO.package, | |
| 114 activity=_PACKAGE_INFO.activity, | |
| 115 data=url) | |
| 116 device.StartActivity(view_intent, blocking=True) | |
| 117 time.sleep(_PAGE_LOAD_TIMEOUT) | |
| 118 | |
| 119 # Get the file. | |
| 120 device.ForceStop(_PACKAGE_INFO.package) | |
| 121 database_filename = ( | |
| 122 '/data/user/0/%s/app_chrome/Default/Network Action Predictor' % | |
| 123 _PACKAGE_INFO.package) | |
| 124 device.PullFile(database_filename, output_filename) | |
| 125 | |
| 126 | |
| 127 def main(): | |
| 128 logging.basicConfig(level=logging.INFO) | |
| 129 parser = _CreateOptionParser() | |
| 130 options, _ = parser.parse_args() | |
| 131 devil_chromium.Initialize() | |
| 132 device = _FindDevice(options.device) | |
| 133 if device is None: | |
| 134 logging.error('No suitable device.') | |
|
pasko
2016/11/10 16:02:00
nit: would be more informative to:
could not find
Benoit L
2016/11/14 12:49:25
Done.
| |
| 135 sys.exit(1) | |
| 136 _Setup(device) | |
| 137 _Go(device, options.urls_filename, options.output_filename, | |
| 138 int(options.repeats)) | |
| 139 | |
| 140 | |
| 141 if __name__ == '__main__': | |
| 142 main() | |
| OLD | NEW |