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

Side by Side Diff: tools/resource_prefetch_predictor/generate_database.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
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # 2 #
3 # Copyright 2016 The Chromium Authors. All rights reserved. 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 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Loads a set of web pages several times on a device, and extracts the 7 """Loads a set of web pages several times on a device, and extracts the
8 predictor database. 8 predictor database.
9 """ 9 """
10 10
11 import argparse 11 import argparse
12 import logging 12 import logging
13 import os 13 import os
14 import sys 14 import sys
15 15 import time
16 16
17 _SRC_PATH = os.path.abspath(os.path.join( 17 _SRC_PATH = os.path.abspath(os.path.join(
18 os.path.dirname(__file__), os.pardir, os.pardir)) 18 os.path.dirname(__file__), os.pardir, os.pardir))
19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) 19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
20 20
21 from devil.android import device_utils 21 from devil.android import device_utils
22 22
23 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) 23 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
24 import devil_chromium 24 import devil_chromium
25 25
26 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) 26 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading'))
27 import controller
28 from options import OPTIONS 27 from options import OPTIONS
29 import page_track 28 import page_track
30 29
30 import prefetch_predictor_common
31
31 32
32 _PAGE_LOAD_TIMEOUT = 20 33 _PAGE_LOAD_TIMEOUT = 20
33 34
34 35
35 def _CreateArgumentParser(): 36 def _CreateArgumentParser():
36 """Creates and returns the argument parser.""" 37 """Creates and returns the argument parser."""
37 parser = argparse.ArgumentParser( 38 parser = argparse.ArgumentParser(
38 description=('Loads a set of web pages several times on a device, and ' 39 ('Loads a set of web pages several times on a device, and extracts the '
39 'extracts the predictor database.'), 40 'predictor database.'), parents=[OPTIONS.GetParentParser()])
40 parents=[OPTIONS.GetParentParser()])
41 parser.add_argument('--device', help='Device ID') 41 parser.add_argument('--device', help='Device ID')
42 parser.add_argument('--urls_filename', help='File containing a list of URLs ' 42 parser.add_argument('--urls_filename', help='File containing a list of URLs '
43 '(one per line). URLs can be repeated.') 43 '(one per line). URLs can be repeated.')
44 parser.add_argument('--output_filename', 44 parser.add_argument('--output_filename',
45 help='File to store the database in.') 45 help='File to store the database in.')
46 parser.add_argument('--url_repeat', 46 parser.add_argument('--url_repeat',
47 help=('Number of times each URL in the input ' 47 help=('Number of times each URL in the input '
48 'file is loaded.'), 48 'file is loaded.'),
49 default=3) 49 default=3)
50 return parser 50 return parser
51 51
52 52
53 def _FindDevice(device_id):
54 """Returns a device matching |device_id| or the first one if None, or None."""
55 devices = device_utils.DeviceUtils.HealthyDevices()
56 if device_id is None:
57 return devices[0]
58 matching_devices = [d for d in devices if str(d) == device_id]
59 if not matching_devices:
60 return None
61 return matching_devices[0]
62
63
64 def _Setup(device):
65 """Sets up a device and returns an instance of RemoteChromeController."""
66 chrome_controller = controller.RemoteChromeController(device)
67 device.ForceStop(OPTIONS.ChromePackage().package)
68 chrome_controller.AddChromeArguments(
69 ['--speculative-resource-prefetching=learning'])
70 chrome_controller.ResetBrowserState()
71 return chrome_controller
72
73
74 def _Go(chrome_controller, urls_filename, output_filename, repeats): 53 def _Go(chrome_controller, urls_filename, output_filename, repeats):
75 urls = [] 54 urls = []
76 with open(urls_filename) as f: 55 with open(urls_filename) as f:
77 urls = [line.strip() for line in f.readlines()] 56 urls = [line.strip() for line in f.readlines()]
78 57
79 with chrome_controller.Open() as connection: 58 with chrome_controller.Open() as connection:
80 for repeat in range(repeats): 59 for repeat in range(repeats):
81 logging.info('Repeat #%d', repeat) 60 logging.info('Repeat #%d', repeat)
82 for url in urls: 61 for url in urls:
83 logging.info('\tLoading %s', url) 62 logging.info('\tLoading %s', url)
84 page_track.PageTrack(connection) # Registers the listeners. 63 page_track.PageTrack(connection) # Registers the listeners.
85 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, 64 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT,
86 stop_delay_multiplier=1.5) 65 stop_delay_multiplier=1.5)
66 time.sleep(2) # Reduces flakiness.
87 67
88 device = chrome_controller.GetDevice() 68 device = chrome_controller.GetDevice()
89 device.ForceStop(OPTIONS.ChromePackage().package) 69 device.ForceStop(OPTIONS.ChromePackage().package)
90 database_filename = ( 70 device.PullFile(prefetch_predictor_common.DatabaseDevicePath(),
91 '/data/user/0/%s/app_chrome/Default/Network Action Predictor' % 71 output_filename)
92 OPTIONS.ChromePackage().package)
93 device.PullFile(database_filename, output_filename)
94 72
95 73
96 def main(): 74 def main():
75 devil_chromium.Initialize()
97 logging.basicConfig(level=logging.INFO) 76 logging.basicConfig(level=logging.INFO)
77
98 parser = _CreateArgumentParser() 78 parser = _CreateArgumentParser()
99 args = parser.parse_args() 79 args = parser.parse_args()
100 OPTIONS.SetParsedArgs(args) 80 OPTIONS.SetParsedArgs(args)
101 devil_chromium.Initialize() 81
102 device = _FindDevice(args.device) 82 device = prefetch_predictor_common.FindDevice(args.device)
103 if device is None: 83 if device is None:
104 logging.error('Could not find device: %s.', args.device) 84 logging.error('Could not find device: %s.', args.device)
105 sys.exit(1) 85 sys.exit(1)
106 chrome_controller = _Setup(device) 86
87 chrome_controller = prefetch_predictor_common.Setup(
88 device, ['--speculative-resource-prefetching=learning'])
107 _Go(chrome_controller, args.urls_filename, args.output_filename, 89 _Go(chrome_controller, args.urls_filename, args.output_filename,
108 int(args.url_repeat)) 90 int(args.url_repeat))
109 91
110 92
111 if __name__ == '__main__': 93 if __name__ == '__main__':
112 main() 94 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698