Chromium Code Reviews| 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 """ | 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 prefetch_predictor_common | |
| 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 | |
| 28 from options import OPTIONS | 29 from options import OPTIONS |
| 29 import page_track | 30 import page_track |
| 30 | 31 |
| 32 import prefetch_predictor_common | |
|
pasko
2016/11/30 12:53:46
why importing twice?
Benoit L
2016/11/30 15:25:50
...
Sorry about that.
Done.
| |
| 33 | |
| 31 | 34 |
| 32 _PAGE_LOAD_TIMEOUT = 20 | 35 _PAGE_LOAD_TIMEOUT = 20 |
| 33 | 36 |
| 34 | 37 |
| 35 def _CreateArgumentParser(): | 38 def _CreateArgumentParser(): |
| 36 """Creates and returns the argument parser.""" | 39 """Creates and returns the argument parser.""" |
| 37 parser = argparse.ArgumentParser( | 40 parser = argparse.ArgumentParser( |
| 38 description=('Loads a set of web pages several times on a device, and ' | 41 ('Loads a set of web pages several times on a device, and extracts the ' |
| 39 'extracts the predictor database.'), | 42 'predictor database.'), parents=[OPTIONS.GetParentParser()]) |
| 40 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.') |
| 46 parser.add_argument('--url_repeat', | 48 parser.add_argument('--url_repeat', |
| 47 help=('Number of times each URL in the input ' | 49 help=('Number of times each URL in the input ' |
| 48 'file is loaded.'), | 50 'file is loaded.'), |
| 49 default=3) | 51 default=3) |
| 50 return parser | 52 return parser |
| 51 | 53 |
| 52 | 54 |
| 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): | 55 def _Go(chrome_controller, urls_filename, output_filename, repeats): |
| 75 urls = [] | 56 urls = [] |
| 76 with open(urls_filename) as f: | 57 with open(urls_filename) as f: |
| 77 urls = [line.strip() for line in f.readlines()] | 58 urls = [line.strip() for line in f.readlines()] |
| 78 | 59 |
| 79 with chrome_controller.Open() as connection: | 60 with chrome_controller.Open() as connection: |
| 80 for repeat in range(repeats): | 61 for repeat in range(repeats): |
| 81 logging.info('Repeat #%d', repeat) | 62 logging.info('Repeat #%d', repeat) |
| 82 for url in urls: | 63 for url in urls: |
| 83 logging.info('\tLoading %s', url) | 64 logging.info('\tLoading %s', url) |
| 84 page_track.PageTrack(connection) # Registers the listeners. | 65 page_track.PageTrack(connection) # Registers the listeners. |
| 85 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, | 66 connection.MonitorUrl(url, timeout_seconds=_PAGE_LOAD_TIMEOUT, |
| 86 stop_delay_multiplier=1.5) | 67 stop_delay_multiplier=1.5) |
| 87 | 68 |
| 88 device = chrome_controller.GetDevice() | 69 device = chrome_controller.GetDevice() |
| 89 device.ForceStop(OPTIONS.ChromePackage().package) | 70 device.ForceStop(OPTIONS.ChromePackage().package) |
| 90 database_filename = ( | 71 device.PullFile(prefetch_predictor_common.DatabaseDevicePath(), |
| 91 '/data/user/0/%s/app_chrome/Default/Network Action Predictor' % | 72 output_filename) |
| 92 OPTIONS.ChromePackage().package) | |
| 93 device.PullFile(database_filename, output_filename) | |
| 94 | 73 |
| 95 | 74 |
| 96 def main(): | 75 def main(): |
| 76 devil_chromium.Initialize() | |
| 97 logging.basicConfig(level=logging.INFO) | 77 logging.basicConfig(level=logging.INFO) |
| 78 | |
| 98 parser = _CreateArgumentParser() | 79 parser = _CreateArgumentParser() |
| 99 args = parser.parse_args() | 80 args = parser.parse_args() |
| 100 OPTIONS.SetParsedArgs(args) | 81 OPTIONS.SetParsedArgs(args) |
| 101 devil_chromium.Initialize() | 82 |
| 102 device = _FindDevice(args.device) | 83 device = prefetch_predictor_common.FindDevice(args.device) |
| 103 if device is None: | 84 if device is None: |
| 104 logging.error('Could not find device: %s.', args.device) | 85 logging.error('Could not find device: %s.', args.device) |
| 105 sys.exit(1) | 86 sys.exit(1) |
| 106 chrome_controller = _Setup(device) | 87 |
| 88 chrome_controller = prefetch_predictor_common.Setup( | |
| 89 device, ['--speculative-resource-prefetching=learning']) | |
| 107 _Go(chrome_controller, args.urls_filename, args.output_filename, | 90 _Go(chrome_controller, args.urls_filename, args.output_filename, |
| 108 int(args.url_repeat)) | 91 int(args.url_repeat)) |
| 109 | 92 |
| 110 | 93 |
| 111 if __name__ == '__main__': | 94 if __name__ == '__main__': |
| 112 main() | 95 main() |
| OLD | NEW |