OLD | NEW |
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 Also generates a WPR archive for another page. |
9 """ | 10 """ |
10 | 11 |
11 import argparse | 12 import argparse |
12 import logging | 13 import logging |
13 import os | 14 import os |
14 import sys | 15 import sys |
15 import time | 16 import time |
16 | 17 |
17 _SRC_PATH = os.path.abspath(os.path.join( | 18 _SRC_PATH = os.path.abspath(os.path.join( |
18 os.path.dirname(__file__), os.pardir, os.pardir)) | 19 os.path.dirname(__file__), os.pardir, os.pardir)) |
19 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) | 20 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) |
20 | 21 |
21 from devil.android import device_utils | 22 from devil.android import device_utils |
22 | 23 |
23 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) | 24 sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) |
24 import devil_chromium | 25 import devil_chromium |
25 | 26 |
26 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) | 27 sys.path.append(os.path.join(_SRC_PATH, 'tools', 'android', 'loading')) |
| 28 import device_setup |
27 from options import OPTIONS | 29 from options import OPTIONS |
28 import page_track | 30 import page_track |
29 | 31 |
30 import prefetch_predictor_common | 32 import prefetch_predictor_common |
31 | 33 |
32 | 34 |
33 _PAGE_LOAD_TIMEOUT = 20 | 35 _PAGE_LOAD_TIMEOUT = 40 |
34 | 36 |
35 | 37 |
36 def _CreateArgumentParser(): | 38 def _CreateArgumentParser(): |
37 """Creates and returns the argument parser.""" | 39 """Creates and returns the argument parser.""" |
38 parser = argparse.ArgumentParser( | 40 parser = argparse.ArgumentParser( |
39 ('Loads a set of web pages several times on a device, and extracts the ' | 41 ('Loads a set of web pages several times on a device, and extracts the ' |
40 'predictor database.'), parents=[OPTIONS.GetParentParser()]) | 42 'predictor database.'), parents=[OPTIONS.GetParentParser()]) |
41 parser.add_argument('--device', help='Device ID') | 43 parser.add_argument('--device', help='Device ID') |
42 parser.add_argument('--urls_filename', help='File containing a list of URLs ' | 44 parser.add_argument('--urls_filename', help='File containing a list of URLs ' |
43 '(one per line). URLs can be repeated.') | 45 '(one per line). URLs can be repeated.') |
44 parser.add_argument('--output_filename', | 46 parser.add_argument('--output_filename', |
45 help='File to store the database in.') | 47 help='File to store the database in.') |
| 48 parser.add_argument('--test_url', help='URL to record an archive of.') |
| 49 parser.add_argument('--wpr_archive', help='WPR archive path.') |
46 parser.add_argument('--url_repeat', | 50 parser.add_argument('--url_repeat', |
47 help=('Number of times each URL in the input ' | 51 help=('Number of times each URL in the input ' |
48 'file is loaded.'), | 52 'file is loaded.'), default=3) |
49 default=3) | |
50 return parser | 53 return parser |
51 | 54 |
52 | 55 |
53 def _Go(chrome_controller, urls_filename, output_filename, repeats): | 56 def _GenerateDatabase(chrome_controller, urls_filename, output_filename, |
| 57 repeats): |
54 urls = [] | 58 urls = [] |
55 with open(urls_filename) as f: | 59 with open(urls_filename) as f: |
56 urls = [line.strip() for line in f.readlines()] | 60 urls = [line.strip() for line in f.readlines()] |
57 | 61 |
58 with chrome_controller.Open() as connection: | 62 with chrome_controller.Open() as connection: |
59 for repeat in range(repeats): | 63 for repeat in range(repeats): |
60 logging.info('Repeat #%d', repeat) | 64 logging.info('Repeat #%d', repeat) |
61 for url in urls: | 65 for url in urls: |
62 logging.info('\tLoading %s', url) | 66 logging.info('\tLoading %s', url) |
63 page_track.PageTrack(connection) # Registers the listeners. | 67 page_track.PageTrack(connection) # Registers the listeners. |
64 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, | 68 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, |
65 stop_delay_multiplier=1.5) | 69 stop_delay_multiplier=1.5) |
66 time.sleep(2) # Reduces flakiness. | 70 time.sleep(2) # Reduces flakiness. |
67 | 71 |
68 device = chrome_controller.GetDevice() | 72 device = chrome_controller.GetDevice() |
69 device.ForceStop(OPTIONS.ChromePackage().package) | 73 device.ForceStop(OPTIONS.ChromePackage().package) |
70 device.PullFile(prefetch_predictor_common.DatabaseDevicePath(), | 74 device.PullFile(prefetch_predictor_common.DatabaseDevicePath(), |
71 output_filename) | 75 output_filename) |
72 | 76 |
73 | 77 |
| 78 def _GenerateWprArchive(device, url, archive_path): |
| 79 with device_setup.RemoteWprHost(device, archive_path, record=True) as wpr: |
| 80 chrome_controller = prefetch_predictor_common.Setup( |
| 81 device, wpr.chrome_args) |
| 82 with chrome_controller.Open() as connection: |
| 83 page_track.PageTrack(connection) # Registers the listeners. |
| 84 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, |
| 85 stop_delay_multiplier=1.5) |
| 86 |
| 87 |
74 def main(): | 88 def main(): |
75 devil_chromium.Initialize() | 89 devil_chromium.Initialize() |
76 logging.basicConfig(level=logging.INFO) | 90 logging.basicConfig(level=logging.INFO) |
77 | 91 |
78 parser = _CreateArgumentParser() | 92 parser = _CreateArgumentParser() |
79 args = parser.parse_args() | 93 args = parser.parse_args() |
80 OPTIONS.SetParsedArgs(args) | 94 OPTIONS.SetParsedArgs(args) |
81 | 95 |
82 device = prefetch_predictor_common.FindDevice(args.device) | 96 device = prefetch_predictor_common.FindDevice(args.device) |
83 if device is None: | 97 if device is None: |
84 logging.error('Could not find device: %s.', args.device) | 98 logging.error('Could not find device: %s.', args.device) |
85 sys.exit(1) | 99 sys.exit(1) |
86 | 100 |
87 chrome_controller = prefetch_predictor_common.Setup( | 101 chrome_controller = prefetch_predictor_common.Setup( |
88 device, ['--speculative-resource-prefetching=learning']) | 102 device, ['--speculative-resource-prefetching=learning']) |
89 _Go(chrome_controller, args.urls_filename, args.output_filename, | 103 _GenerateDatabase(chrome_controller, args.urls_filename, |
90 int(args.url_repeat)) | 104 args.output_filename, int(args.url_repeat)) |
| 105 _GenerateWprArchive(device, args.test_url, args.wpr_archive) |
91 | 106 |
92 | 107 |
93 if __name__ == '__main__': | 108 if __name__ == '__main__': |
94 main() | 109 main() |
OLD | NEW |