OLD | NEW |
1 #! /usr/bin/python | 1 #! /usr/bin/python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
7 import cgi | 7 import cgi |
8 import json | 8 import json |
9 import logging | 9 import logging |
10 import os | 10 import os |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 url: url as a string to load. | 52 url: url as a string to load. |
53 """ | 53 """ |
54 load_intent = intent.Intent( | 54 load_intent = intent.Intent( |
55 package=OPTIONS.ChromePackage().package, | 55 package=OPTIONS.ChromePackage().package, |
56 activity=OPTIONS.ChromePackage().activity, | 56 activity=OPTIONS.ChromePackage().activity, |
57 data=url) | 57 data=url) |
58 logging.warning('Loading ' + url) | 58 logging.warning('Loading ' + url) |
59 device.StartActivity(load_intent, blocking=True) | 59 device.StartActivity(load_intent, blocking=True) |
60 | 60 |
61 | 61 |
62 def _WriteJson(output, json_data): | |
63 """Write JSON data in a nice way. | |
64 | |
65 Args: | |
66 output: a file object | |
67 json_data: JSON data as a dict. | |
68 """ | |
69 json.dump(json_data, output, sort_keys=True, indent=2) | |
70 | |
71 | |
72 def _GetPrefetchHtml(graph_view, name=None): | 62 def _GetPrefetchHtml(graph_view, name=None): |
73 """Generate prefetch page for the resources in resource graph. | 63 """Generate prefetch page for the resources in resource graph. |
74 | 64 |
75 Args: | 65 Args: |
76 graph_view: (LoadingGraphView) | 66 graph_view: (LoadingGraphView) |
77 name: optional string used in the generated page. | 67 name: optional string used in the generated page. |
78 | 68 |
79 Returns: | 69 Returns: |
80 HTML as a string containing all the link rel=prefetch directives necessary | 70 HTML as a string containing all the link rel=prefetch directives necessary |
81 for prefetching the given ResourceGraph. | 71 for prefetching the given ResourceGraph. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 # We hope that the tmpfile name is unique enough for the device. | 137 # We hope that the tmpfile name is unique enough for the device. |
148 target = os.path.join('/sdcard/Download', os.path.basename(tmp.name)) | 138 target = os.path.join('/sdcard/Download', os.path.basename(tmp.name)) |
149 device = device_setup.GetFirstDevice() | 139 device = device_setup.GetFirstDevice() |
150 device.adb.Push(tmp.name, target) | 140 device.adb.Push(tmp.name, target) |
151 logging.warning('Pushed prefetch %s to device at %s' % (tmp.name, target)) | 141 logging.warning('Pushed prefetch %s to device at %s' % (tmp.name, target)) |
152 _LoadPage(device, 'file://' + target) | 142 _LoadPage(device, 'file://' + target) |
153 time.sleep(OPTIONS.prefetch_delay_seconds) | 143 time.sleep(OPTIONS.prefetch_delay_seconds) |
154 logging.warning('Warm fetch') | 144 logging.warning('Warm fetch') |
155 warm_data = _LogRequests(url, clear_cache_override=False) | 145 warm_data = _LogRequests(url, clear_cache_override=False) |
156 with open(json_output, 'w') as f: | 146 with open(json_output, 'w') as f: |
157 _WriteJson(f, warm_data) | 147 json.dump(warm_data, f) |
158 logging.warning('Wrote ' + json_output) | 148 logging.warning('Wrote ' + json_output) |
159 with open(json_output + '.cold', 'w') as f: | 149 with open(json_output + '.cold', 'w') as f: |
160 _WriteJson(f, cold_data) | 150 json.dump(cold_data, f) |
161 logging.warning('Wrote ' + json_output + '.cold') | 151 logging.warning('Wrote ' + json_output + '.cold') |
162 else: | 152 else: |
163 with open(json_output, 'w') as f: | 153 with open(json_output, 'w') as f: |
164 _WriteJson(f, cold_data) | 154 json.dump(cold_data, f) |
165 logging.warning('Wrote ' + json_output) | 155 logging.warning('Wrote ' + json_output) |
166 | 156 |
167 | 157 |
168 def _ProcessTraceFile(filename): | 158 def _ProcessTraceFile(filename): |
169 with open(filename) as f: | 159 with open(filename) as f: |
170 return _ProcessJsonTrace(json.load(f)) | 160 return _ProcessJsonTrace(json.load(f)) |
171 | 161 |
172 | 162 |
173 def _ProcessJsonTrace(json_dict): | 163 def _ProcessJsonTrace(json_dict): |
174 trace = loading_trace.LoadingTrace.FromJsonDict(json_dict) | 164 trace = loading_trace.LoadingTrace.FromJsonDict(json_dict) |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 parser.add_argument('command', help=' '.join(COMMAND_MAP.keys())) | 308 parser.add_argument('command', help=' '.join(COMMAND_MAP.keys())) |
319 parser.add_argument('rest', nargs=argparse.REMAINDER) | 309 parser.add_argument('rest', nargs=argparse.REMAINDER) |
320 args = parser.parse_args() | 310 args = parser.parse_args() |
321 devil_chromium.Initialize() | 311 devil_chromium.Initialize() |
322 COMMAND_MAP.get(args.command, | 312 COMMAND_MAP.get(args.command, |
323 lambda _: InvalidCommand(args.command))(args.rest) | 313 lambda _: InvalidCommand(args.command))(args.rest) |
324 | 314 |
325 | 315 |
326 if __name__ == '__main__': | 316 if __name__ == '__main__': |
327 main() | 317 main() |
OLD | NEW |