OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 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 """Grabs before/after memory dumps using the devtools remote protocol. | 5 """Grabs before/after memory dumps using the devtools remote protocol. |
6 | 6 |
7 To use it you first start Chrome with remote debugging enabled then run the | 7 To use it you first start Chrome with remote debugging enabled then run the |
8 script which will take a memory dump, wait for you to press enter, take | 8 script which will take a memory dump, wait for you to press enter, take |
9 another memory dump and finally save a trace file. For example: | 9 another memory dump and finally save a trace file. For example: |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 $ ./third_party/catapult/tracing/bin/memory_infra_remote_dump --port=1234 | 27 $ ./third_party/catapult/tracing/bin/memory_infra_remote_dump --port=1234 |
28 ... | 28 ... |
29 [Press enter to stop tracing] | 29 [Press enter to stop tracing] |
30 ... | 30 ... |
31 /var/folders/18/gl6q632j20nc_tw5g9l03dhc007g45/T/trace_20s191.json: 835 KB | 31 /var/folders/18/gl6q632j20nc_tw5g9l03dhc007g45/T/trace_20s191.json: 835 KB |
32 """ | 32 """ |
33 | 33 |
34 import argparse | 34 import argparse |
35 import json | 35 import json |
36 import os | 36 import os |
| 37 import requests |
37 import sys | 38 import sys |
38 import tempfile | 39 import tempfile |
39 import time | 40 import time |
40 | 41 |
41 try: | 42 try: |
42 import websocket | 43 import websocket |
43 except ImportError: | 44 except ImportError: |
44 print 'Please run: pip install --user websocket-client' | 45 print 'Please run: pip install --user websocket-client' |
45 sys.exit(1) | 46 sys.exit(1) |
46 | 47 |
47 | 48 |
48 class TracingDevtoolsClient(object): | 49 class TracingDevtoolsClient(object): |
49 def __init__(self, host, port): | 50 def __init__(self, host, port): |
50 url = 'ws://%s:%s/devtools/browser' % (host, port) | 51 r = requests.get('http://%s:%s/json/version' % (host, port)) |
| 52 url = r.json()['webSocketDebuggerUrl'] |
51 print 'Connecting to ' + url | 53 print 'Connecting to ' + url |
52 self.ws = websocket.CreateConnection(url) | 54 self.ws = websocket.create_connection(url) |
53 self.cmd = 0 | 55 self.cmd = 0 |
54 | 56 |
55 def send(self, method, params={}): | 57 def send(self, method, params={}): |
56 self.cmd += 1 | 58 self.cmd += 1 |
57 self.ws.send( | 59 self.ws.send( |
58 json.dumps({'id': self.cmd, 'method': method, 'params': params})) | 60 json.dumps({'id': self.cmd, 'method': method, 'params': params})) |
59 resp = self.recv() | 61 resp = self.recv() |
60 assert resp['id'] == self.cmd | 62 assert resp['id'] == self.cmd |
61 return resp.get('result', {}) | 63 return resp.get('result', {}) |
62 | 64 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 122 |
121 if args.output_trace is None: | 123 if args.output_trace is None: |
122 trace_fd = tempfile.NamedTemporaryFile(prefix='trace_', suffix='.json', | 124 trace_fd = tempfile.NamedTemporaryFile(prefix='trace_', suffix='.json', |
123 delete=False) | 125 delete=False) |
124 else: | 126 else: |
125 trace_fd = open(args.output_trace, 'wb') | 127 trace_fd = open(args.output_trace, 'wb') |
126 | 128 |
127 cli = TracingDevtoolsClient(args.host, args.port) | 129 cli = TracingDevtoolsClient(args.host, args.port) |
128 cli.dump(trace_fd) | 130 cli.dump(trace_fd) |
129 print '\n%s: %d KB' % (trace_fd.name, os.stat(trace_fd.name).st_size / 1000) | 131 print '\n%s: %d KB' % (trace_fd.name, os.stat(trace_fd.name).st_size / 1000) |
OLD | NEW |