Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #! /usr/bin/python | 1 #! /usr/bin/python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 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 """Loading trace recorder.""" | 6 """Loading trace recorder.""" |
| 7 | 7 |
| 8 import argparse | |
| 9 import datetime | |
| 10 import json | |
| 11 import logging | |
| 8 import os | 12 import os |
| 9 import sys | 13 import sys |
| 14 import time | |
| 10 | 15 |
| 11 _SRC_DIR = os.path.abspath(os.path.join( | 16 _SRC_DIR = os.path.abspath(os.path.join( |
| 12 os.path.dirname(__file__), '..', '..', '..')) | 17 os.path.dirname(__file__), '..', '..', '..')) |
| 13 | 18 |
| 14 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) | 19 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) |
| 15 from devil.android import device_utils | 20 from devil.android import device_utils |
| 16 | 21 |
| 17 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) | 22 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) |
| 18 import devil_chromium | 23 import devil_chromium |
| 19 | 24 |
| 20 import device_setup | 25 import device_setup |
| 21 import devtools_monitor | 26 import devtools_monitor |
| 27 import loading_trace | |
| 22 import page_track | 28 import page_track |
| 23 | 29 import request_track |
| 24 class AndroidTraceRecorder(object): | 30 import tracing |
| 25 """Records a loading trace.""" | |
| 26 def __init__(self, url): | |
| 27 self.url = url | |
| 28 self.devtools_connection = None | |
| 29 self.page_track = None | |
| 30 | |
| 31 def Go(self, connection): | |
| 32 self.devtools_connection = connection | |
| 33 self.page_track = page_track.PageTrack(self.devtools_connection) | |
| 34 self.devtools_connection.SetUpMonitoring() | |
| 35 self.devtools_connection.SendAndIgnoreResponse( | |
| 36 'Page.navigate', {'url': self.url}) | |
| 37 self.devtools_connection.StartMonitoring() | |
| 38 print self.page_track.GetEvents() | |
| 39 | 31 |
| 40 | 32 |
| 41 def DoIt(url): | 33 def RecordAndDumpTrace(device, url, output_filename): |
| 42 devil_chromium.Initialize() | 34 with file(output_filename, 'w') as output,\ |
| 43 devices = device_utils.DeviceUtils.HealthyDevices() | 35 device_setup.DeviceConnection(device) as connection: |
| 44 device = devices[0] | 36 page = page_track.PageTrack(connection) |
| 45 trace_recorder = AndroidTraceRecorder(url) | 37 request = request_track.RequestTrack(connection) |
| 46 device_setup.SetUpAndExecute(device, 'chrome', trace_recorder.Go) | 38 tracing_track = tracing.TracingTrack(connection) |
| 39 connection.SetUpMonitoring() | |
| 40 connection.SendAndIgnoreResponse('Network.clearBrowserCache', {}) | |
| 41 connection.SendAndIgnoreResponse('Page.navigate', {'url': url}) | |
| 42 connection.StartMonitoring() | |
| 43 metadata = {'date': datetime.datetime.utcnow().isoformat(), | |
| 44 'seconds_since_epoch': time.time()} | |
| 45 trace = loading_trace.LoadingTrace(url, metadata, page, request, | |
| 46 tracing_track) | |
| 47 json.dump(trace.ToJsonDict(), output) | |
| 47 | 48 |
| 48 | 49 |
| 50 def main(): | |
| 51 logging.basicConfig(level=logging.INFO) | |
| 52 devil_chromium.Initialize() | |
| 53 | |
| 54 parser = argparse.ArgumentParser() | |
| 55 parser.add_argument('--url', required=True) | |
| 56 parser.add_argument('--output', required=True) | |
| 57 args = parser.parse_args() | |
| 58 url = args.url | |
| 59 if not url.startswith('http'): | |
| 60 url = 'http://' + url | |
| 61 device = device_utils.DeviceUtils.HealthyDevices()[0] | |
| 62 RecordAndDumpTrace(device, url, args.output) | |
| 63 | |
|
mattcary
2016/01/19 15:24:47
two blank lines?
Benoit L
2016/01/19 15:54:50
Done.
| |
| 49 if __name__ == '__main__': | 64 if __name__ == '__main__': |
| 50 DoIt(sys.argv[1]) | 65 main() |
| OLD | NEW |