| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 Args: | 119 Args: |
| 120 url: (str) URL to match in requests. | 120 url: (str) URL to match in requests. |
| 121 before_timestamp: (int) Only keep requests submitted before a given | 121 before_timestamp: (int) Only keep requests submitted before a given |
| 122 timestamp. | 122 timestamp. |
| 123 | 123 |
| 124 Returns: | 124 Returns: |
| 125 A list of candidates, ordered by timestamp. | 125 A list of candidates, ordered by timestamp. |
| 126 """ | 126 """ |
| 127 candidates = self._requests_by_url.get(url, []) | 127 candidates = self._requests_by_url.get(url, []) |
| 128 candidates = [r for r in candidates if ( | 128 candidates = [r for r in candidates if ( |
| 129 r.timestamp + max(0, r.timing.receive_headers_end) <= before_timestamp)] | 129 r.timestamp + max( |
| 130 0, r.timing.receive_headers_end / 1000) <= before_timestamp)] |
| 130 candidates.sort(key=operator.attrgetter('timestamp')) | 131 candidates.sort(key=operator.attrgetter('timestamp')) |
| 131 return candidates | 132 return candidates |
| 132 | 133 |
| 133 def _FindBestMatchingInitiator(self, request, matches): | 134 def _FindBestMatchingInitiator(self, request, matches): |
| 134 """Returns the best matching request within a list of matches. | 135 """Returns the best matching request within a list of matches. |
| 135 | 136 |
| 136 Iteratively removes candidates until one is left: | 137 Iteratively removes candidates until one is left: |
| 137 - With the same parent frame. | 138 - With the same parent frame. |
| 138 - From the same frame. | 139 - From the same frame. |
| 139 | 140 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 150 if not matches: | 151 if not matches: |
| 151 return None | 152 return None |
| 152 if len(matches) == 1: | 153 if len(matches) == 1: |
| 153 return matches[0] | 154 return matches[0] |
| 154 # Several matches, try to reduce this number to 1. Otherwise, return the | 155 # Several matches, try to reduce this number to 1. Otherwise, return the |
| 155 # most recent one. | 156 # most recent one. |
| 156 if request.frame_id in self._frame_to_parent: # Main frame has no parent. | 157 if request.frame_id in self._frame_to_parent: # Main frame has no parent. |
| 157 parent_frame_id = self._frame_to_parent[request.frame_id] | 158 parent_frame_id = self._frame_to_parent[request.frame_id] |
| 158 same_parent_matches = [ | 159 same_parent_matches = [ |
| 159 r for r in matches | 160 r for r in matches |
| 160 if self._frame_to_parent[r.frame_id] == parent_frame_id] | 161 if r.frame_id in self._frame_to_parent and |
| 162 self._frame_to_parent[r.frame_id] == parent_frame_id] |
| 161 if not same_parent_matches: | 163 if not same_parent_matches: |
| 162 logging.warning('All matches are from non-sibling frames.') | 164 logging.warning('All matches are from non-sibling frames.') |
| 163 return matches[-1] | 165 return matches[-1] |
| 164 if len(same_parent_matches) == 1: | 166 if len(same_parent_matches) == 1: |
| 165 return same_parent_matches[0] | 167 return same_parent_matches[0] |
| 166 same_frame_matches = [r for r in matches if r.frame_id == request.frame_id] | 168 same_frame_matches = [r for r in matches if r.frame_id == request.frame_id] |
| 167 if not same_frame_matches: | 169 if not same_frame_matches: |
| 168 logging.warning('All matches are from non-sibling frames.') | 170 logging.warning('All matches are from non-sibling frames.') |
| 169 return matches[-1] | 171 return matches[-1] |
| 170 if len(same_frame_matches) == 1: | 172 if len(same_frame_matches) == 1: |
| 171 return same_frame_matches[0] | 173 return same_frame_matches[0] |
| 172 else: | 174 else: |
| 173 logging.warning('Several matches') | 175 logging.warning('Several matches') |
| 174 return same_frame_matches[-1] | 176 return same_frame_matches[-1] |
| 175 | 177 |
| 176 | 178 |
| 177 if __name__ == '__main__': | 179 if __name__ == '__main__': |
| 178 import json | 180 import json |
| 179 import sys | 181 import sys |
| 180 trace_filename = sys.argv[1] | 182 trace_filename = sys.argv[1] |
| 181 json_dict = json.load(open(trace_filename, 'r')) | 183 json_dict = json.load(open(trace_filename, 'r')) |
| 182 lens = RequestDependencyLens( | 184 lens = RequestDependencyLens( |
| 183 loading_trace.LoadingTrace.FromJsonDict(json_dict)) | 185 loading_trace.LoadingTrace.FromJsonDict(json_dict)) |
| 184 depedencies = lens.GetRequestDependencies() | 186 depedencies = lens.GetRequestDependencies() |
| 185 for (first, second, dep_reason) in depedencies: | 187 for (first, second, dep_reason) in depedencies: |
| 186 print '%s -> %s\t(%s)' % (first.request_id, second.request_id, dep_reason) | 188 print '%s -> %s\t(%s)' % (first.request_id, second.request_id, dep_reason) |
| OLD | NEW |