def __init__(self, trace, user_lens, activity_lens, network_lens, navigation_start_msec): requests = trace.request_track.GetEvents() dependencies_lens = request_dependencies_lens.RequestDependencyLens( trace) prefetch_view = PrefetchSimulationView(trace, dependencies_lens, user_lens) preloaded_requests = prefetch_view.PreloadedRequests( requests[0], dependencies_lens, trace) self._navigation_start_msec = navigation_start_msec self._satisfied_msec = user_lens.SatisfiedMs() graph = LoadingGraphView.FromTrace(trace) self._inversions = graph.GetInversionsAtTime(self._satisfied_msec) self._byte_frac = self._GenerateByteFrac(network_lens) self._requests = user_lens.CriticalRequests() self._preloaded_requests = ([ r for r in preloaded_requests if r in self._requests ]) self._cpu_busyness = _ComputeCpuBusyness(activity_lens, navigation_start_msec, self._satisfied_msec) prefetch_view.UpdateNodeCosts(lambda n: 0 if n.preloaded else n.cost) self._no_state_prefetch_ms = prefetch_view.Cost()
def testToFromJsonDict(self): self.assertEqual(40 + 12, self.prefetch_view.Cost()) json_dict = self.prefetch_view.ToJsonDict() new_view = PrefetchSimulationView.FromJsonDict(json_dict) self.assertEqual(40 + 12, new_view.Cost()) # Updated Costs. self.prefetch_view.UpdateNodeCosts(lambda _: 100) self.assertEqual(500 + 40 + 12, self.prefetch_view.Cost()) json_dict = self.prefetch_view.ToJsonDict() new_view = PrefetchSimulationView.FromJsonDict(json_dict) self.assertEqual(500 + 40 + 12, new_view.Cost())
def testPreloadedRequests(self): first_request = TestRequests.FIRST_REDIRECT_REQUEST preloaded_requests = PrefetchSimulationView.PreloadedRequests( first_request, self.dependencies_lens, self.trace) self.assertListEqual([first_request], preloaded_requests) self._SetUp( [{'args': {'data': {'url': 'http://bla.com/nyancat.js'}}, 'cat': 'blink.net', 'id': '0xaf9f14fa9dd6c314', 'name': 'Resource', 'ph': 'X', 'ts': 1, 'dur': 120, 'pid': 12, 'tid': 12}, {'args': {'step': 'Preload'}, 'cat': 'blink.net', 'id': '0xaf9f14fa9dd6c314', 'name': 'Resource', 'ph': 'T', 'ts': 12, 'pid': 12, 'tid': 12}]) preloaded_requests = PrefetchSimulationView.PreloadedRequests( first_request, self.dependencies_lens, self.trace) self.assertListEqual([TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.JS_REQUEST, TestRequests.JS_REQUEST_OTHER_FRAME, TestRequests.JS_REQUEST_UNRELATED_FRAME], preloaded_requests)
def testParserDiscoverableRequests(self): first_request = TestRequests.FIRST_REDIRECT_REQUEST discovered_requests = PrefetchSimulationView.ParserDiscoverableRequests( first_request, self.dependencies_lens) self.assertListEqual( [TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.JS_REQUEST, TestRequests.JS_REQUEST_OTHER_FRAME, TestRequests.JS_REQUEST_UNRELATED_FRAME], discovered_requests)
def ExtractDiscoverableUrls(loading_trace_path, subresource_discoverer): """Extracts discoverable resource urls from a loading trace according to a sub-resource discoverer. Args: loading_trace_path: The loading trace's path. subresource_discoverer: The sub-resources discoverer that should white-list the resources to keep in cache for the NoState-Prefetch benchmarks. Returns: A set of urls. """ assert subresource_discoverer in SUBRESOURCE_DISCOVERERS, \ 'unknown prefetch simulation {}'.format(subresource_discoverer) # Load trace and related infos. logging.info('loading %s' % loading_trace_path) trace = LoadingTrace.FromJsonFile(loading_trace_path) dependencies_lens = RequestDependencyLens(trace) first_resource_request = trace.request_track.GetFirstResourceRequest() # Build the list of discovered requests according to the desired simulation. discovered_requests = [] if subresource_discoverer == EMPTY_CACHE_DISCOVERER: pass elif subresource_discoverer == FULL_CACHE_DISCOVERER: discovered_requests = trace.request_track.GetEvents() elif subresource_discoverer == REDIRECTED_MAIN_DISCOVERER: discovered_requests = \ [dependencies_lens.GetRedirectChain(first_resource_request)[-1]] elif subresource_discoverer == PARSER_DISCOVERER: discovered_requests = PrefetchSimulationView.ParserDiscoverableRequests( first_resource_request, dependencies_lens) elif subresource_discoverer == HTML_PRELOAD_SCANNER_DISCOVERER: discovered_requests = PrefetchSimulationView.PreloadedRequests( first_resource_request, dependencies_lens, trace) else: assert False whitelisted_urls = set() logging.info('white-listing %s' % first_resource_request.url) for request in _FilterOutDataAndIncompleteRequests(discovered_requests): logging.info('white-listing %s' % request.url) whitelisted_urls.add(request.url) return whitelisted_urls
def testExpandRedirectChains(self): self.assertListEqual([ TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.SECOND_REDIRECT_REQUEST, TestRequests.REDIRECTED_REQUEST ], PrefetchSimulationView._ExpandRedirectChains( [TestRequests.FIRST_REDIRECT_REQUEST], self.dependencies_lens))
def testExpandRedirectChains(self): self.assertListEqual( [ TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.SECOND_REDIRECT_REQUEST, TestRequests.REDIRECTED_REQUEST, ], PrefetchSimulationView._ExpandRedirectChains([TestRequests.FIRST_REDIRECT_REQUEST], self.dependencies_lens), )
def _DiscoverRequests(dependencies_lens, subresource_discoverer): trace = dependencies_lens.loading_trace first_resource_request = trace.request_track.GetFirstResourceRequest() if subresource_discoverer == Discoverer.EmptyCache: requests = [] elif subresource_discoverer == Discoverer.FullCache: requests = dependencies_lens.loading_trace.request_track.GetEvents() elif subresource_discoverer == Discoverer.MainDocument: requests = [dependencies_lens.GetRedirectChain(first_resource_request)[-1]] elif subresource_discoverer == Discoverer.Parser: requests = PrefetchSimulationView.ParserDiscoverableRequests( first_resource_request, dependencies_lens) elif subresource_discoverer == Discoverer.HTMLPreloadScanner: requests = PrefetchSimulationView.PreloadedRequests( first_resource_request, dependencies_lens, trace) else: assert False logging.info('number of requests discovered by %s: %d', subresource_discoverer, len(requests)) return requests
def _SetUp(self, added_trace_events=None): trace_events = [ {'ts': 5, 'ph': 'X', 'dur': 10, 'pid': 2, 'tid': 1, 'cat': 'blink.net'}] if added_trace_events is not None: trace_events += added_trace_events self.trace = TestRequests.CreateLoadingTrace(trace_events) self.dependencies_lens = request_dependencies_lens.RequestDependencyLens( self.trace) self.user_satisfied_lens = test_utils.MockUserSatisfiedLens(self.trace) self.user_satisfied_lens._postload_msec = 12 self.prefetch_view = PrefetchSimulationView( self.trace, self.dependencies_lens, self.user_satisfied_lens) for e in self.prefetch_view.graph.graph.Edges(): e.cost = 10