| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Gathers and infers dependencies between requests. | 5 """Gathers and infers dependencies between requests. |
| 6 | 6 |
| 7 When executed as a script, loads a trace and outputs the dependencies. | 7 When executed as a script, loads a trace and outputs the dependencies. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import collections | 10 import collections |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 Args: | 58 Args: |
| 59 request: (Request) the request we wish to get the initiator of. | 59 request: (Request) the request we wish to get the initiator of. |
| 60 | 60 |
| 61 Returns: | 61 Returns: |
| 62 None if no dependency is found from this request, or | 62 None if no dependency is found from this request, or |
| 63 (initiator (Request), blocked_request (Request), reason (str)). | 63 (initiator (Request), blocked_request (Request), reason (str)). |
| 64 """ | 64 """ |
| 65 reason = request.initiator['type'] | 65 reason = request.initiator['type'] |
| 66 assert reason in request_track.Request.INITIATORS | 66 assert reason in request_track.Request.INITIATORS |
| 67 # Redirect suffixes are added in RequestTrack. | 67 if reason == 'redirect': |
| 68 if request.request_id.endswith(request_track.RequestTrack.REDIRECT_SUFFIX): | |
| 69 return self._GetInitiatingRequestRedirect(request) | 68 return self._GetInitiatingRequestRedirect(request) |
| 70 elif reason == 'parser': | 69 elif reason == 'parser': |
| 71 return self._GetInitiatingRequestParser(request) | 70 return self._GetInitiatingRequestParser(request) |
| 72 elif reason == 'script': | 71 elif reason == 'script': |
| 73 return self._GetInitiatingRequestScript(request) | 72 return self._GetInitiatingRequestScript(request) |
| 74 else: | 73 else: |
| 75 assert reason == 'other' | 74 assert reason == 'other' |
| 76 return self._GetInitiatingRequestOther(request) | 75 return self._GetInitiatingRequestOther(request) |
| 77 | 76 |
| 78 def _GetInitiatingRequestRedirect(self, request): | 77 def _GetInitiatingRequestRedirect(self, request): |
| 79 request_id = request.request_id[:request.request_id.index( | 78 assert request_track.Request.INITIATING_REQUEST in request.initiator |
| 80 request_track.RequestTrack.REDIRECT_SUFFIX)] | 79 initiating_request_id = request.initiator[ |
| 81 assert request_id in self._requests_by_id | 80 request_track.Request.INITIATING_REQUEST] |
| 82 dependent_request = self._requests_by_id[request_id] | 81 assert initiating_request_id in self._requests_by_id |
| 83 assert request.timing.request_time < \ | 82 return (self._requests_by_id[initiating_request_id], request, 'redirect') |
| 84 dependent_request.timing.request_time, '.\n'.join( | |
| 85 [str(request), str(dependent_request)]) | |
| 86 return (request, dependent_request, 'redirect') | |
| 87 | 83 |
| 88 def _GetInitiatingRequestParser(self, request): | 84 def _GetInitiatingRequestParser(self, request): |
| 89 url = request.initiator['url'] | 85 url = request.initiator['url'] |
| 90 candidates = self._FindMatchingRequests(url, request.timing.request_time) | 86 candidates = self._FindMatchingRequests(url, request.timing.request_time) |
| 91 if not candidates: | 87 if not candidates: |
| 92 return None | 88 return None |
| 93 initiating_request = self._FindBestMatchingInitiator(request, candidates) | 89 initiating_request = self._FindBestMatchingInitiator(request, candidates) |
| 94 return (initiating_request, request, 'parser') | 90 return (initiating_request, request, 'parser') |
| 95 | 91 |
| 96 def _GetInitiatingRequestScript(self, request): | 92 def _GetInitiatingRequestScript(self, request): |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 if __name__ == '__main__': | 184 if __name__ == '__main__': |
| 189 import json | 185 import json |
| 190 import sys | 186 import sys |
| 191 trace_filename = sys.argv[1] | 187 trace_filename = sys.argv[1] |
| 192 json_dict = json.load(open(trace_filename, 'r')) | 188 json_dict = json.load(open(trace_filename, 'r')) |
| 193 lens = RequestDependencyLens( | 189 lens = RequestDependencyLens( |
| 194 loading_trace.LoadingTrace.FromJsonDict(json_dict)) | 190 loading_trace.LoadingTrace.FromJsonDict(json_dict)) |
| 195 depedencies = lens.GetRequestDependencies() | 191 depedencies = lens.GetRequestDependencies() |
| 196 for (first, second, dep_reason) in depedencies: | 192 for (first, second, dep_reason) in depedencies: |
| 197 print '%s -> %s\t(%s)' % (first.request_id, second.request_id, dep_reason) | 193 print '%s -> %s\t(%s)' % (first.request_id, second.request_id, dep_reason) |
| OLD | NEW |