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

Side by Side Diff: tools/resource_prefetch_predictor/prefetch_benchmark.py

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

Powered by Google App Engine
This is Rietveld 408576698