OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 import time | |
7 | |
8 from live_tests import network_metrics | |
9 from telemetry.page import page_test | |
10 from telemetry.value import scalar | |
11 | |
12 | |
13 class ChromeProxyMetricException(page_test.MeasurementFailure): | |
14 pass | |
15 | |
16 | |
17 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' | |
18 | |
19 | |
20 class ChromeProxyResponse(network_metrics.HTTPResponse): | |
21 """ Represents an HTTP response from a timeleine event.""" | |
22 def __init__(self, event): | |
23 super(ChromeProxyResponse, self).__init__(event) | |
24 | |
25 def ShouldHaveChromeProxyViaHeader(self): | |
26 resp = self.response | |
27 # Ignore https and data url | |
28 if resp.url.startswith('https') or resp.url.startswith('data:'): | |
29 return False | |
30 # Ignore 304 Not Modified and cache hit. | |
31 if resp.status == 304 or resp.served_from_cache: | |
32 return False | |
33 # Ignore invalid responses that don't have any header. Log a warning. | |
34 if not resp.headers: | |
35 logging.warning('response for %s does not any have header ' | |
36 '(refer=%s, status=%s)', | |
37 resp.url, resp.GetHeader('Referer'), resp.status) | |
38 return False | |
39 return True | |
40 | |
41 def HasChromeProxyViaHeader(self): | |
42 via_header = self.response.GetHeader('Via') | |
43 if not via_header: | |
44 return False | |
45 vias = [v.strip(' ') for v in via_header.split(',')] | |
46 # The Via header is valid if it has a 4-character version prefix followed by | |
47 # the proxy name, for example, "1.1 Chrome-Compression-Proxy". | |
48 return any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias) | |
49 | |
50 def HasExtraViaHeader(self, extra_header): | |
sclittle
2015/04/24 18:55:22
I don't think these tests need the extra via heade
bustamante
2015/04/29 20:28:03
Yeah, Done.
| |
51 via_header = self.response.GetHeader('Via') | |
52 if not via_header: | |
53 return False | |
54 vias = [v.strip(' ') for v in via_header.split(',')] | |
55 return any(v == extra_header for v in vias) | |
56 | |
57 | |
58 class ChromeProxyMetric(network_metrics.NetworkMetric): | |
59 """A Chrome proxy timeline metric.""" | |
60 | |
61 def __init__(self): | |
62 super(ChromeProxyMetric, self).__init__() | |
63 self.compute_data_saving = True | |
64 | |
65 def SetEvents(self, events): | |
66 """Used for unittest.""" | |
67 self._events = events | |
68 | |
69 def ResponseFromEvent(self, event): | |
70 return ChromeProxyResponse(event) | |
71 | |
72 def AddResults(self, tab, results): | |
73 raise NotImplementedError | |
74 | |
75 def AddResultsForDataSaving(self, tab, results): | |
76 resources_via_proxy = 0 | |
77 resources_from_cache = 0 | |
78 resources_direct = 0 | |
79 | |
80 super(ChromeProxyMetric, self).AddResults(tab, results) | |
81 for resp in self.IterResponses(tab): | |
82 if resp.response.served_from_cache: | |
83 resources_from_cache += 1 | |
84 if resp.HasChromeProxyViaHeader(): | |
85 resources_via_proxy += 1 | |
86 else: | |
87 resources_direct += 1 | |
88 | |
89 if resources_from_cache + resources_via_proxy + resources_direct == 0: | |
90 raise ChromeProxyMetricException, ( | |
91 'Expected at least one response, but zero responses were received.') | |
92 | |
93 results.AddValue(scalar.ScalarValue( | |
94 results.current_page, 'resources_via_proxy', 'count', | |
95 resources_via_proxy)) | |
96 results.AddValue(scalar.ScalarValue( | |
97 results.current_page, 'resources_from_cache', 'count', | |
98 resources_from_cache)) | |
99 results.AddValue(scalar.ScalarValue( | |
100 results.current_page, 'resources_direct', 'count', resources_direct)) | |
101 | |
102 def AddResultsForLatency(self, tab, results): | |
103 # TODO(bustamante): This is a hack to workaround crbug.com/467174, | |
104 # once fixed just pull down window.performance.timing object and | |
105 # reference that everywhere. | |
106 load_event_start = tab.EvaluateJavaScript( | |
107 'window.performance.timing.loadEventStart') | |
108 navigation_start = tab.EvaluateJavaScript( | |
109 'window.performance.timing.navigationStart') | |
110 dom_content_loaded_event_start = tab.EvaluateJavaScript( | |
111 'window.performance.timing.domContentLoadedEventStart') | |
112 fetch_start = tab.EvaluateJavaScript( | |
113 'window.performance.timing.fetchStart') | |
114 request_start = tab.EvaluateJavaScript( | |
115 'window.performance.timing.requestStart') | |
116 domain_lookup_end = tab.EvaluateJavaScript( | |
117 'window.performance.timing.domainLookupEnd') | |
118 domain_lookup_start = tab.EvaluateJavaScript( | |
119 'window.performance.timing.domainLookupStart') | |
120 connect_end = tab.EvaluateJavaScript( | |
121 'window.performance.timing.connectEnd') | |
122 connect_start = tab.EvaluateJavaScript( | |
123 'window.performance.timing.connectStart') | |
124 response_end = tab.EvaluateJavaScript( | |
125 'window.performance.timing.responseEnd') | |
126 response_start = tab.EvaluateJavaScript( | |
127 'window.performance.timing.responseStart') | |
128 | |
129 # NavigationStart relative markers in milliseconds. | |
130 load_start = (float(load_event_start) - navigation_start) | |
131 results.AddValue(scalar.ScalarValue( | |
132 results.current_page, 'load_start', 'ms', load_start)) | |
133 | |
134 dom_content_loaded_start = ( | |
135 float(dom_content_loaded_event_start) - navigation_start) | |
136 results.AddValue(scalar.ScalarValue( | |
137 results.current_page, 'dom_content_loaded_start', 'ms', | |
138 dom_content_loaded_start)) | |
139 | |
140 fetch_start = (float(fetch_start) - navigation_start) | |
141 results.AddValue(scalar.ScalarValue( | |
142 results.current_page, 'fetch_start', 'ms', fetch_start, | |
143 important=False)) | |
144 | |
145 request_start = (float(request_start) - navigation_start) | |
146 results.AddValue(scalar.ScalarValue( | |
147 results.current_page, 'request_start', 'ms', request_start, | |
148 important=False)) | |
149 | |
150 # Phase measurements in milliseconds. | |
151 domain_lookup_duration = (float(domain_lookup_end) - domain_lookup_start) | |
152 results.AddValue(scalar.ScalarValue( | |
153 results.current_page, 'domain_lookup_duration', 'ms', | |
154 domain_lookup_duration, important=False)) | |
155 | |
156 connect_duration = (float(connect_end) - connect_start) | |
157 results.AddValue(scalar.ScalarValue( | |
158 results.current_page, 'connect_duration', 'ms', connect_duration, | |
159 important=False)) | |
160 | |
161 request_duration = (float(response_start) - request_start) | |
162 results.AddValue(scalar.ScalarValue( | |
163 results.current_page, 'request_duration', 'ms', request_duration, | |
164 important=False)) | |
165 | |
166 response_duration = (float(response_end) - response_start) | |
167 results.AddValue(scalar.ScalarValue( | |
168 results.current_page, 'response_duration', 'ms', response_duration, | |
169 important=False)) | |
OLD | NEW |