def MakeRequest( url, source_url, start_time=None, headers_time=None, end_time=None, magic_content_type=False, initiator_type='other'): """Make a dependent request. Args: url: a url, or number which will be used as a url. source_url: a url or number which will be used as the source (initiating) url. If the source url is not present, then url will be a root. The convention in tests is to use a source_url of 'null' in this case. start_time: The request start time in milliseconds. If None, this is set to the current request id in seconds. If None, the two other time parameters below must also be None. headers_time: The timestamp when resource headers were received, or None. end_time: The timestamp when the resource was finished, or None. magic_content_type (bool): if true, set a magic content type that makes url always be detected as a valid source and destination request. initiator_type: the initiator type to use. Returns: A request_track.Request. """ assert ((start_time is None and headers_time is None and end_time is None) or (start_time is not None and headers_time is not None and end_time is not None)), \ 'Need no time specified or all times specified' if start_time is None: # Use the request id in seconds for timestamps. This guarantees increasing # times which makes request dependencies behave as expected. start_time = headers_time = end_time = MakeRequest._next_request_id * 1000 assert initiator_type in ('other', 'parser') timing = request_track.TimingAsList(request_track.TimingFromDict({ # connectEnd should be ignored. 'connectEnd': (end_time - start_time) / 2, 'receiveHeadersEnd': headers_time - start_time, 'loadingFinished': end_time - start_time, 'requestTime': start_time / 1000.0})) rq = request_track.Request.FromJsonDict({ 'timestamp': start_time / 1000.0, 'request_id': str(MakeRequest._next_request_id), 'url': 'http://' + str(url), 'initiator': {'type': initiator_type, 'url': 'http://' + str(source_url)}, 'response_headers': {'Content-Type': 'null' if not magic_content_type else 'magic-debug-content' }, 'timing': timing }) MakeRequest._next_request_id += 1 return rq
def testUpdateRequestCost(self): requests = self.trace.request_track.GetEvents() requests[0].timing = request_track.TimingFromDict({ 'requestTime': 12, 'loadingFinished': 10 }) dependencies_lens = request_dependencies_lens.RequestDependencyLens( self.trace) g = dependency_graph.RequestDependencyGraph(requests, dependencies_lens) self.assertEqual(10, g.Cost()) request_id = requests[0].request_id g.UpdateRequestsCost({request_id: 100}) self.assertEqual(100, g.Cost()) g.UpdateRequestsCost({'unrelated_id': 1000}) self.assertEqual(100, g.Cost())
def MakeRequestWithTiming(url, source_url, timing_dict, magic_content_type=False, initiator_type='other'): """Make a dependent request. Args: url: a url, or number which will be used as a url. source_url: a url or number which will be used as the source (initiating) url. If the source url is not present, then url will be a root. The convention in tests is to use a source_url of 'null' in this case. timing_dict: (dict) Suitable to be passed to request_track.TimingFromDict(). initiator_type: the initiator type to use. Returns: A request_track.Request. """ assert initiator_type in ('other', 'parser') timing = request_track.TimingFromDict(timing_dict) rq = request_track.Request.FromJsonDict({ 'timestamp': timing.request_time, 'request_id': str(MakeRequestWithTiming._next_request_id), 'url': 'http://' + str(url), 'initiator': { 'type': initiator_type, 'url': 'http://' + str(source_url) }, 'response_headers': { 'Content-Type': 'null' if not magic_content_type else 'magic-debug-content' }, 'timing': request_track.TimingAsList(timing) }) MakeRequestWithTiming._next_request_id += 1 return rq
def _RequestAt(self, timestamp_msec, duration=1): timestamp_sec = float(timestamp_msec) / 1000 rq = request_track.Request.FromJsonDict({ 'url': 'http://bla-%s-.com' % timestamp_msec, 'request_id': '0.%s' % self._request_index, 'frame_id': '123.%s' % timestamp_msec, 'initiator': { 'type': 'other' }, 'timestamp': timestamp_sec, 'timing': request_track.TimingFromDict({ 'requestTime': timestamp_sec, 'loadingFinished': duration }) }) self._request_index += 1 return rq
def testCost(self): requests = self.trace.request_track.GetEvents() for (index, request) in enumerate(requests): request.timing = request_track.TimingFromDict({ 'requestTime': index, 'receiveHeadersEnd': 10, 'loadingFinished': 10 }) dependencies_lens = request_dependencies_lens.RequestDependencyLens( self.trace) g = dependency_graph.RequestDependencyGraph(requests, dependencies_lens) # First redirect -> Second redirect -> Redirected Request -> Request -> # JS Request 2 self.assertEqual(7010, g.Cost()) # Not on the critical path g.UpdateRequestsCost({TestRequests.JS_REQUEST.request_id: 0}) self.assertEqual(7010, g.Cost()) g.UpdateRequestsCost( {TestRequests.FIRST_REDIRECT_REQUEST.request_id: 0}) self.assertEqual(7000, g.Cost()) g.UpdateRequestsCost( {TestRequests.SECOND_REDIRECT_REQUEST.request_id: 0}) self.assertEqual(6990, g.Cost())