def testChromeProxyMetricForSafebrowsingOn(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([EVENT_MALWARE_PROXY]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForSafebrowsingOn(None, results) results.AssertHasPageSpecificScalarValue('safebrowsing', 'timeout responses', 1) # Clear results and metrics to test no response for safebrowsing results = test_page_test_results.TestPageTestResults(self) metric.SetEvents([]) metric.AddResultsForSafebrowsingOn(None, results) results.AssertHasPageSpecificScalarValue('safebrowsing', 'timeout responses', 1)
def testChromeProxyMetricForDataSaving(self): metric = metrics.ChromeProxyMetric() events = [ EVENT_HTML_DIRECT, EVENT_HTML_PROXY_VIA, EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_DIRECT] metric.SetEvents(events) self.assertTrue(len(events), len(list(metric.IterResponses(None)))) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForDataSaving(None, results) results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForDataSaving(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForHTTPFallback(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([ EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK ]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForHTTPFallback(None, results) results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 2) metric.SetEvents([EVENT_HTML_PROXY_VIA, EVENT_IMAGE_PROXY_VIA]) exception_occurred = False try: metric.AddResultsForHTTPFallback(None, results) except common_metrics.ChromeProxyMetricException: exception_occurred = True # The responses came through the SPDY proxy, but were expected through the # HTTP fallback proxy. self.assertTrue(exception_occurred) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForHTTPFallback(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForBypass(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([ EVENT_HTML_DIRECT, EVENT_HTML_PROXY_VIA, EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_DIRECT ]) results = test_page_test_results.TestPageTestResults(self) bypass_exception = False try: metric.AddResultsForBypass(None, results) except common_metrics.ChromeProxyMetricException: bypass_exception = True # Two of the first three events have Via headers. self.assertTrue(bypass_exception) # Use directly fetched image only. It is treated as bypassed. metric.SetEvents([EVENT_IMAGE_DIRECT]) metric.AddResultsForBypass(None, results) results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForBypass(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForBlockOnce(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([EVENT_HTML_DIRECT, EVENT_IMAGE_PROXY_VIA]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForBlockOnce(None, results) results.AssertHasPageSpecificScalarValue('eligible_responses', 'count', 2) results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) metric.SetEvents([EVENT_HTML_DIRECT, EVENT_IMAGE_DIRECT]) exception_occurred = False try: metric.AddResultsForBlockOnce(None, results) except common_metrics.ChromeProxyMetricException: exception_occurred = True # The second response was over direct, but was expected via proxy. self.assertTrue(exception_occurred) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForBlockOnce(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForHeaderValidation(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([ EVENT_HTML_DIRECT, EVENT_HTML_PROXY_VIA, EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_DIRECT ]) results = test_page_test_results.TestPageTestResults(self) missing_via_exception = False try: metric.AddResultsForHeaderValidation(None, results) except common_metrics.ChromeProxyMetricException: missing_via_exception = True # Only the HTTP image response does not have a valid Via header. self.assertTrue(missing_via_exception) # Two events with valid Via headers. metric.SetEvents([EVENT_HTML_PROXY_VIA, EVENT_IMAGE_PROXY_CACHED]) metric.AddResultsForHeaderValidation(None, results) results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForHeaderValidation(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForHTTPToDirectFallback(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([ EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, EVENT_HTML_DIRECT, EVENT_IMAGE_DIRECT ]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1) results.AssertHasPageSpecificScalarValue('bypass', 'count', 2) metric.SetEvents([EVENT_HTML_PROXY_VIA, EVENT_HTML_DIRECT]) exception_occurred = False try: metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') except common_metrics.ChromeProxyMetricException: exception_occurred = True # The first response was expected through the HTTP fallback proxy. self.assertTrue(exception_occurred) metric.SetEvents([ EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, EVENT_HTML_PROXY_VIA_HTTP_FALLBACK, EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK ]) exception_occurred = False try: metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') except common_metrics.ChromeProxyMetricException: exception_occurred = True # All but the first response were expected to be over direct. self.assertTrue(exception_occurred) metric.SetEvents( [EVENT_HTML_DIRECT, EVENT_HTML_DIRECT, EVENT_IMAGE_DIRECT]) exception_occurred = False try: metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') except common_metrics.ChromeProxyMetricException: exception_occurred = True # The first response was expected through the HTTP fallback proxy. self.assertTrue(exception_occurred) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2') except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testNetworkMetricResults(self): events = [ # A plain text HTML. self.MakeNetworkTimelineEvent(url='http://test.html1', response_headers={ 'Content-Type': 'text/html', 'Content-Length': str(len(HTML_BODY)), }, body=HTML_BODY), # A compressed HTML. self.MakeNetworkTimelineEvent(url='http://test.html2', response_headers={ 'Content-Type': 'text/html', 'Content-Encoding': 'gzip', 'X-Original-Content-Length': str(len(HTML_BODY)), }, body=HTML_BODY), # A base64 encoded image. self.MakeNetworkTimelineEvent(url='http://test.image', response_headers={ 'Content-Type': 'image/jpeg', 'Content-Encoding': 'gzip', 'X-Original-Content-Length': str(IMAGE_OCL), }, body=base64.b64encode(IMAGE_BODY), base64_encoded_body=True), ] metric = network_metrics.NetworkMetric() metric._events = events metric.compute_data_saving = True self.assertTrue(len(events), len(list(metric.IterResponses(None)))) results = test_page_test_results.TestPageTestResults(self) metric.AddResults(None, results) cl = len(HTML_BODY) + GZIPPED_HTML_LEN + len(IMAGE_BODY) results.AssertHasPageSpecificScalarValue('content_length', 'bytes', cl) ocl = len(HTML_BODY) + len(HTML_BODY) + IMAGE_OCL results.AssertHasPageSpecificScalarValue('original_content_length', 'bytes', ocl) saving_percent = float(ocl - cl) * 100 / ocl results.AssertHasPageSpecificScalarValue('data_saving', 'percent', saving_percent)
def testChromeProxyMetricForCorsBypass(self): metric = metrics.ChromeProxyMetric() metric.SetEvents( [EVENT_HTML_PROXY_VIA, EVENT_IMAGE_BYPASS, EVENT_IMAGE_DIRECT]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForCorsBypass(None, results) results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1) # Passing in zero responses should cause a failure. metric.SetEvents([]) no_responses_exception = False try: metric.AddResultsForCorsBypass(None, results) except common_metrics.ChromeProxyMetricException: no_responses_exception = True self.assertTrue(no_responses_exception)
def testChromeProxyMetricForExtraViaHeader(self): metric = metrics.ChromeProxyMetric() metric.SetEvents([EVENT_HTML_DIRECT, EVENT_HTML_PROXY_EXTRA_VIA]) results = test_page_test_results.TestPageTestResults(self) metric.AddResultsForExtraViaHeader(None, results, TEST_EXTRA_VIA_HEADER) # The direct page should not count an extra via header, but should also not # throw an exception. results.AssertHasPageSpecificScalarValue('extra_via_header', 'count', 1) metric.SetEvents([EVENT_HTML_PROXY_VIA]) exception_occurred = False try: metric.AddResultsForExtraViaHeader(None, results, TEST_EXTRA_VIA_HEADER) except common_metrics.ChromeProxyMetricException: exception_occurred = True # The response had the chrome proxy via header, but not the extra expected # via header. self.assertTrue(exception_occurred)
def ComputeStartupMetrics(self): results = test_page_test_results.TestPageTestResults(self) # Create a mock model usable by # StartupTimelineMetric.AddWholeTraceResults(). def IterateEvents(event_predicate): for event in self.events: if event_predicate(event): yield event class MockClass(object): pass model = MockClass() model.browser_process = MockClass() model.browser_process.parent = MockClass() model.browser_process.parent.IterAllEvents = IterateEvents startup.StartupTimelineMetric().AddWholeTraceResults(model, results) return results
def AddResults(self): self.results = test_page_test_results.TestPageTestResults(self) self.metric.AddResults(self.model, self.renderer_thread, INTERACTION_RECORDS, self.results)
def GetResults(self, metric, model, renderer_thread, interaction_records): results = test_page_test_results.TestPageTestResults(self) metric.AddResults(model, renderer_thread, interaction_records, results) return results
class GPUTimelineTest(unittest.TestCase): def GetResults(self, metric, domain.model, renderer_thread, interaction_records): results = test_page_test_results.TestPageTestResults(self) metric.AddResults(domain.model, renderer_thread, interaction_records, results) return results
def RunAggregator(self, aggregator, interactions): results = test_page_test_results.TestPageTestResults(self) aggregator.AddResults(self.domain.model, self.renderer_process, interactions, results) return results