OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import logging | 5 import logging |
6 import time | 6 import time |
7 | 7 |
8 from integration_tests import network_metrics | 8 from common import chrome_proxy_metrics |
9 from common import network_metrics | |
9 from telemetry.page import page_test | 10 from telemetry.page import page_test |
10 from telemetry.value import scalar | 11 from telemetry.value import scalar |
11 | 12 |
12 | 13 |
13 class ChromeProxyMetricException(page_test.MeasurementFailure): | 14 class ChromeProxyMetricException(page_test.MeasurementFailure): |
sclittle
2015/04/30 00:07:58
Maybe this exception should be moved into common?
bustamante
2015/04/30 20:30:40
Done.
| |
14 pass | 15 pass |
15 | 16 |
16 | 17 |
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): | |
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 def IsValidByViaHeader(self): | |
58 return (not self.ShouldHaveChromeProxyViaHeader() or | |
59 self.HasChromeProxyViaHeader()) | |
60 | |
61 def GetChromeProxyClientType(self): | |
62 """Get the client type directive from the Chrome-Proxy request header. | |
63 | |
64 Returns: | |
65 The client type directive from the Chrome-Proxy request header for the | |
66 request that lead to this response. For example, if the request header | |
67 "Chrome-Proxy: c=android" is present, then this method would return | |
68 "android". Returns None if no client type directive is present. | |
69 """ | |
70 if 'Chrome-Proxy' not in self.response.request_headers: | |
71 return None | |
72 | |
73 chrome_proxy_request_header = self.response.request_headers['Chrome-Proxy'] | |
74 values = [v.strip() for v in chrome_proxy_request_header.split(',')] | |
75 for value in values: | |
76 kvp = value.split('=', 1) | |
77 if len(kvp) == 2 and kvp[0].strip() == 'c': | |
78 return kvp[1].strip() | |
79 return None | |
80 | |
81 def HasChromeProxyLoFi(self): | |
82 if 'Chrome-Proxy' not in self.response.request_headers: | |
83 return False | |
84 chrome_proxy_request_header = self.response.request_headers['Chrome-Proxy'] | |
85 values = [v.strip() for v in chrome_proxy_request_header.split(',')] | |
86 for value in values: | |
87 if len(value) == 5 and value == 'q=low': | |
88 return True | |
89 return False | |
90 | |
91 class ChromeProxyMetric(network_metrics.NetworkMetric): | 18 class ChromeProxyMetric(network_metrics.NetworkMetric): |
92 """A Chrome proxy timeline metric.""" | 19 """A Chrome proxy timeline metric.""" |
93 | 20 |
94 def __init__(self): | 21 def __init__(self): |
95 super(ChromeProxyMetric, self).__init__() | 22 super(ChromeProxyMetric, self).__init__() |
96 self.compute_data_saving = True | 23 self.compute_data_saving = True |
97 | 24 |
98 def SetEvents(self, events): | 25 def SetEvents(self, events): |
99 """Used for unittest.""" | 26 """Used for unittest.""" |
100 self._events = events | 27 self._events = events |
101 | 28 |
102 def ResponseFromEvent(self, event): | 29 def ResponseFromEvent(self, event): |
103 return ChromeProxyResponse(event) | 30 return chrome_proxy_metrics.ChromeProxyResponse(event) |
104 | 31 |
105 def AddResults(self, tab, results): | 32 def AddResults(self, tab, results): |
106 raise NotImplementedError | 33 raise NotImplementedError |
107 | 34 |
108 def AddResultsForDataSaving(self, tab, results): | 35 def AddResultsForDataSaving(self, tab, results): |
109 resources_via_proxy = 0 | 36 resources_via_proxy = 0 |
110 resources_from_cache = 0 | 37 resources_from_cache = 0 |
111 resources_direct = 0 | 38 resources_direct = 0 |
112 | 39 |
113 super(ChromeProxyMetric, self).AddResults(tab, results) | 40 super(ChromeProxyMetric, self).AddResults(tab, results) |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
573 via_count += 1 | 500 via_count += 1 |
574 if via_count == 0: | 501 if via_count == 0: |
575 raise ChromeProxyMetricException, ( | 502 raise ChromeProxyMetricException, ( |
576 'Expected at least one response through the proxy after the bypass ' | 503 'Expected at least one response through the proxy after the bypass ' |
577 'expired, but zero such responses were received.') | 504 'expired, but zero such responses were received.') |
578 | 505 |
579 results.AddValue(scalar.ScalarValue( | 506 results.AddValue(scalar.ScalarValue( |
580 results.current_page, 'bypass', 'count', bypass_count)) | 507 results.current_page, 'bypass', 'count', bypass_count)) |
581 results.AddValue(scalar.ScalarValue( | 508 results.AddValue(scalar.ScalarValue( |
582 results.current_page, 'via', 'count', via_count)) | 509 results.current_page, 'via', 'count', via_count)) |
OLD | NEW |