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

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, 1 month 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
16 import common
pasko 2016/11/17 15:59:04 the name can clash, prefetch_predictor_common feel
Benoit L 2016/11/21 13:40:32 Done.
17
16 18
17 _SRC_PATH = os.path.abspath(os.path.join( 19 _SRC_PATH = os.path.abspath(os.path.join(
18 os.path.dirname(__file__), os.pardir, os.pardir)) 20 os.path.dirname(__file__), os.pardir, os.pardir))
19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) 21 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
20 22
21 from devil.android import device_utils 23 from devil.android import device_utils
22 24
23 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) 25 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
24 import devil_chromium 26 import devil_chromium
25 27
26 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) 28 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading'))
27 import controller 29 import controller
28 from options import OPTIONS 30 from options import OPTIONS
29 import page_track 31 import page_track
30 32
31 33
32 _PAGE_LOAD_TIMEOUT = 20 34 _PAGE_LOAD_TIMEOUT = 20
33 35
34 36
35 def _CreateArgumentParser(): 37 def _CreateArgumentParser():
36 """Creates and returns the argument parser.""" 38 """Creates and returns the argument parser."""
37 parser = argparse.ArgumentParser( 39 parser = common.CreateBaseArgumentParser(
38 description=('Loads a set of web pages several times on a device, and ' 40 'Loads a set of web pages several times on a device, and extracts the '
39 'extracts the predictor database.'), 41 'predictor database.')
40 parents=[OPTIONS.GetParentParser()])
41 parser.add_argument('--device', help='Device ID') 42 parser.add_argument('--device', help='Device ID')
42 parser.add_argument('--urls_filename', help='File containing a list of URLs ' 43 parser.add_argument('--urls_filename', help='File containing a list of URLs '
43 '(one per line). URLs can be repeated.') 44 '(one per line). URLs can be repeated.')
44 parser.add_argument('--output_filename', 45 parser.add_argument('--output_filename',
45 help='File to store the database in.') 46 help='File to store the database in.')
46 parser.add_argument('--url_repeat', 47 parser.add_argument('--url_repeat',
47 help=('Number of times each URL in the input ' 48 help=('Number of times each URL in the input '
48 'file is loaded.'), 49 'file is loaded.'),
49 default=3) 50 default=3)
50 return parser 51 return parser
51 52
52 53
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): 54 def _Go(chrome_controller, urls_filename, output_filename, repeats):
75 urls = [] 55 urls = []
76 with open(urls_filename) as f: 56 with open(urls_filename) as f:
77 urls = [line.strip() for line in f.readlines()] 57 urls = [line.strip() for line in f.readlines()]
78 58
79 with chrome_controller.Open() as connection: 59 with chrome_controller.Open() as connection:
80 for repeat in range(repeats): 60 for repeat in range(repeats):
81 logging.info('Repeat #%d', repeat) 61 logging.info('Repeat #%d', repeat)
82 for url in urls: 62 for url in urls:
83 logging.info('\tLoading %s', url) 63 logging.info('\tLoading %s', url)
84 page_track.PageTrack(connection) # Registers the listeners. 64 page_track.PageTrack(connection) # Registers the listeners.
85 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, 65 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT,
86 stop_delay_multiplier=1.5) 66 stop_delay_multiplier=1.5)
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(common.DatabaseDevicePath(), output_filename)
91 '/data/user/0/%s/app_chrome/Default/Network Action Predictor' %
92 OPTIONS.ChromePackage().package)
93 device.PullFile(database_filename, output_filename)
94 71
95 72
96 def main(): 73 def main():
74 devil_chromium.Initialize()
97 logging.basicConfig(level=logging.INFO) 75 logging.basicConfig(level=logging.INFO)
76
98 parser = _CreateArgumentParser() 77 parser = _CreateArgumentParser()
99 args = parser.parse_args() 78 args = common.ParseAndReturnArgs(parser)
100 OPTIONS.SetParsedArgs(args) 79
101 devil_chromium.Initialize() 80 device = common.FindDevice(args.device)
102 device = _FindDevice(args.device)
103 if device is None: 81 if device is None:
104 logging.error('Could not find device: %s.', args.device) 82 logging.error('Could not find device: %s.', args.device)
105 sys.exit(1) 83 sys.exit(1)
106 chrome_controller = _Setup(device) 84
85 chrome_controller = _Setup(
86 device, ['--speculative-resource-prefetching=learning'])
107 _Go(chrome_controller, args.urls_filename, args.output_filename, 87 _Go(chrome_controller, args.urls_filename, args.output_filename,
108 int(args.url_repeat)) 88 int(args.url_repeat))
109 89
110 90
111 if __name__ == '__main__': 91 if __name__ == '__main__':
112 main() 92 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698