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

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

Issue 2496563002: tools: Add a tool to load pages on a device, and pull the predictor database. (Closed)
Patch Set: lint. 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
(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 argparse
12 import logging
13 import os
14 import sys
15
16
17 _SRC_PATH = os.path.abspath(os.path.join(
18 os.path.dirname(__file__), os.pardir, os.pardir))
19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
20
21 from devil.android import device_utils
22
23 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
24 import devil_chromium
25
26 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading'))
27 import controller
28 from options import OPTIONS
29 import page_track
30
31
32 _PAGE_LOAD_TIMEOUT = 20
33
34
35 def _CreateArgumentParser():
36 """Creates and returns the argument parser."""
37 parser = argparse.ArgumentParser(
38 description=('Loads a set of web pages several times on a device, and '
39 'extracts the predictor database.'),
40 parents=[OPTIONS.GetParentParser()])
41 parser.add_argument('--device', help='Device ID')
42 parser.add_argument('--urls_filename', help='File containing a list of URLs '
43 '(one per line). URLs can be repeated.')
44 parser.add_argument('--output_filename',
45 help='File to store the database in.')
46 parser.add_argument('--url_repeat',
47 help=('Number of times each URL in the input '
48 'file is loaded.'),
49 default=3)
50 return parser
51
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):
75 urls = []
76 with open(urls_filename) as f:
77 urls = [line.strip() for line in f.readlines()]
78
79 with chrome_controller.Open() as connection:
80 for repeat in range(repeats):
81 logging.info('Repeat #%d', repeat)
82 for url in urls:
83 logging.info('\tLoading %s', url)
84 page_track.PageTrack(connection) # Registers the listeners.
85 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT,
86 stop_delay_multiplier=1.5)
87
88 device = chrome_controller.GetDevice()
89 device.ForceStop(OPTIONS.ChromePackage().package)
90 database_filename = (
91 '/data/user/0/%s/app_chrome/Default/Network Action Predictor' %
92 OPTIONS.ChromePackage().package)
93 device.PullFile(database_filename, output_filename)
94
95
96 def main():
97 logging.basicConfig(level=logging.INFO)
98 parser = _CreateArgumentParser()
99 args = parser.parse_args()
100 OPTIONS.SetParsedArgs(args)
101 devil_chromium.Initialize()
102 device = _FindDevice(args.device)
103 if device is None:
104 logging.error('Could not find device: %s.', args.device)
105 sys.exit(1)
106 chrome_controller = _Setup(device)
107 _Go(chrome_controller, args.urls_filename, args.output_filename,
108 int(args.url_repeat))
109
110
111 if __name__ == '__main__':
112 main()
OLDNEW
« tools/android/loading/devtools_monitor.py ('K') | « tools/android/loading/devtools_monitor.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698