| 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 web page with speculative prefetch, and collects loading metrics.""" |
| 8 |
| 9 import argparse |
| 10 import logging |
| 11 import os |
| 12 import sys |
| 13 import time |
| 14 |
| 15 _SRC_PATH = os.path.abspath(os.path.join( |
| 16 os.path.dirname(__file__), os.pardir, os.pardir)) |
| 17 |
| 18 sys.path.append(os.path.join( |
| 19 _SRC_PATH, 'tools', 'android', 'customtabs_benchmark', 'scripts')) |
| 20 import customtabs_benchmark |
| 21 import device_setup |
| 22 |
| 23 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) |
| 24 from options import OPTIONS |
| 25 |
| 26 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) |
| 27 import devil_chromium |
| 28 |
| 29 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) |
| 30 from devil.android.sdk import intent |
| 31 |
| 32 import prefetch_predictor_common |
| 33 |
| 34 |
| 35 _EXTERNAL_PREFETCH_FLAG = ( |
| 36 '--speculative-resource-prefetching=enabled-external-only') |
| 37 |
| 38 |
| 39 def _CreateArgumentParser(): |
| 40 """Creates and returns the argument parser.""" |
| 41 parser = argparse.ArgumentParser( |
| 42 ('Loads a URL with the resource_prefetch_predictor and prints loading ' |
| 43 'metrics.'), parents=[OPTIONS.GetParentParser()]) |
| 44 parser.add_argument('--device', help='Device ID') |
| 45 parser.add_argument('--database', |
| 46 help=('File containing the predictor database, as ' |
| 47 'obtained from generate_database.py.')) |
| 48 parser.add_argument('--url', help='URL to load.') |
| 49 parser.add_argument('--prefetch_delay_ms', |
| 50 help='Prefetch delay in ms. -1 to disable prefetch.') |
| 51 return parser |
| 52 |
| 53 |
| 54 def _Setup(device, database_filename): |
| 55 """Sets up a device and returns an instance of RemoteChromeController.""" |
| 56 chrome_controller = prefetch_predictor_common.Setup(device, ['']) |
| 57 chrome_package = OPTIONS.ChromePackage() |
| 58 device.ForceStop(chrome_package.package) |
| 59 chrome_controller.ResetBrowserState() |
| 60 device_database_filename = prefetch_predictor_common.DatabaseDevicePath() |
| 61 owner = group = None |
| 62 |
| 63 # Make sure that the speculative prefetch predictor is enabled to ensure |
| 64 # that the disk database is re-created. |
| 65 command_line_path = '/data/local/tmp/chrome-command-line' |
| 66 with device_setup.FlagReplacer( |
| 67 device, command_line_path, ['--disable-fre', _EXTERNAL_PREFETCH_FLAG]): |
| 68 # Launch Chrome for the first time to recreate the local state. |
| 69 launch_intent = intent.Intent( |
| 70 action='android.intent.action.MAIN', |
| 71 package=chrome_package.package, |
| 72 activity=chrome_package.activity) |
| 73 device.StartActivity(launch_intent, blocking=True) |
| 74 time.sleep(5) |
| 75 device.ForceStop(chrome_package.package) |
| 76 assert device.FileExists(device_database_filename) |
| 77 stats = device.StatPath(device_database_filename) |
| 78 owner = stats['st_owner'] |
| 79 group = stats['st_group'] |
| 80 # Now push the database. Needs to be done after the first launch, otherwise |
| 81 # the profile directory is owned by root. Also change the owner of the |
| 82 # database, since adb push sets it to root. |
| 83 database_content = open(database_filename, 'r').read() |
| 84 device.WriteFile(device_database_filename, database_content, force_push=True) |
| 85 command = 'chown %s:%s \'%s\'' % (owner, group, device_database_filename) |
| 86 device.RunShellCommand(command, as_root=True) |
| 87 |
| 88 |
| 89 def _Go(device, url, prefetch_delay_ms): |
| 90 disable_prefetch = prefetch_delay_ms == -1 |
| 91 # Startup tracing to ease debugging. |
| 92 chrome_args = (customtabs_benchmark.CHROME_ARGS |
| 93 + ['--trace-startup', '--trace-startup-duration=20']) |
| 94 if not disable_prefetch: |
| 95 chrome_args.append(_EXTERNAL_PREFETCH_FLAG) |
| 96 prefetch_mode = 'disabled' if disable_prefetch else 'speculative_prefetch' |
| 97 result = customtabs_benchmark.RunOnce( |
| 98 device, url, warmup=True, speculation_mode=prefetch_mode, |
| 99 delay_to_may_launch_url=2000, |
| 100 delay_to_launch_url=max(0, prefetch_delay_ms), cold=False, |
| 101 chrome_args=chrome_args, reset_chrome_state=False) |
| 102 print customtabs_benchmark.ParseResult(result) |
| 103 |
| 104 |
| 105 def main(): |
| 106 logging.basicConfig(level=logging.INFO) |
| 107 devil_chromium.Initialize() |
| 108 |
| 109 parser = _CreateArgumentParser() |
| 110 args = parser.parse_args() |
| 111 OPTIONS.SetParsedArgs(args) |
| 112 device = prefetch_predictor_common.FindDevice(args.device) |
| 113 if device is None: |
| 114 logging.error('Could not find device: %s.', args.device) |
| 115 sys.exit(1) |
| 116 |
| 117 _Setup(device, args.database) |
| 118 _Go(device, args.url, int(args.prefetch_delay_ms)) |
| 119 |
| 120 |
| 121 if __name__ == '__main__': |
| 122 main() |
| OLD | NEW |