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.
8 from common
import chrome_proxy_metrics
9 from common
import network_metrics
10 from common
.chrome_proxy_metrics
import ChromeProxyMetricException
11 from telemetry
.page
import page_test
12 from telemetry
.value
import scalar
15 class ChromeProxyMetric(network_metrics
.NetworkMetric
):
16 """A Chrome proxy timeline metric."""
19 super(ChromeProxyMetric
, self
).__init
__()
20 self
.compute_data_saving
= True
22 def SetEvents(self
, events
):
23 """Used for unittest."""
26 def ResponseFromEvent(self
, event
):
27 return chrome_proxy_metrics
.ChromeProxyResponse(event
)
29 def AddResults(self
, tab
, results
):
30 raise NotImplementedError
32 def AddResultsForDataSaving(self
, tab
, results
):
33 resources_via_proxy
= 0
34 resources_from_cache
= 0
37 super(ChromeProxyMetric
, self
).AddResults(tab
, results
)
38 for resp
in self
.IterResponses(tab
):
39 if resp
.response
.served_from_cache
:
40 resources_from_cache
+= 1
41 if resp
.HasChromeProxyViaHeader():
42 resources_via_proxy
+= 1
46 if resources_from_cache
+ resources_via_proxy
+ resources_direct
== 0:
47 raise ChromeProxyMetricException
, (
48 'Expected at least one response, but zero responses were received.')
50 results
.AddValue(scalar
.ScalarValue(
51 results
.current_page
, 'resources_via_proxy', 'count',
53 results
.AddValue(scalar
.ScalarValue(
54 results
.current_page
, 'resources_from_cache', 'count',
55 resources_from_cache
))
56 results
.AddValue(scalar
.ScalarValue(
57 results
.current_page
, 'resources_direct', 'count', resources_direct
))
59 def AddResultsForLatency(self
, tab
, results
):
60 # TODO(bustamante): This is a hack to workaround crbug.com/467174,
61 # once fixed just pull down window.performance.timing object and
62 # reference that everywhere.
63 load_event_start
= tab
.EvaluateJavaScript(
64 'window.performance.timing.loadEventStart')
65 navigation_start
= tab
.EvaluateJavaScript(
66 'window.performance.timing.navigationStart')
67 dom_content_loaded_event_start
= tab
.EvaluateJavaScript(
68 'window.performance.timing.domContentLoadedEventStart')
69 fetch_start
= tab
.EvaluateJavaScript(
70 'window.performance.timing.fetchStart')
71 request_start
= tab
.EvaluateJavaScript(
72 'window.performance.timing.requestStart')
73 domain_lookup_end
= tab
.EvaluateJavaScript(
74 'window.performance.timing.domainLookupEnd')
75 domain_lookup_start
= tab
.EvaluateJavaScript(
76 'window.performance.timing.domainLookupStart')
77 connect_end
= tab
.EvaluateJavaScript(
78 'window.performance.timing.connectEnd')
79 connect_start
= tab
.EvaluateJavaScript(
80 'window.performance.timing.connectStart')
81 response_end
= tab
.EvaluateJavaScript(
82 'window.performance.timing.responseEnd')
83 response_start
= tab
.EvaluateJavaScript(
84 'window.performance.timing.responseStart')
86 # NavigationStart relative markers in milliseconds.
87 load_start
= (float(load_event_start
) - navigation_start
)
88 results
.AddValue(scalar
.ScalarValue(
89 results
.current_page
, 'load_start', 'ms', load_start
))
91 dom_content_loaded_start
= (
92 float(dom_content_loaded_event_start
) - navigation_start
)
93 results
.AddValue(scalar
.ScalarValue(
94 results
.current_page
, 'dom_content_loaded_start', 'ms',
95 dom_content_loaded_start
))
97 fetch_start
= (float(fetch_start
) - navigation_start
)
98 results
.AddValue(scalar
.ScalarValue(
99 results
.current_page
, 'fetch_start', 'ms', fetch_start
,
102 request_start
= (float(request_start
) - navigation_start
)
103 results
.AddValue(scalar
.ScalarValue(
104 results
.current_page
, 'request_start', 'ms', request_start
,
107 response_start
= (float(response_start
) - navigation_start
)
108 results
.AddValue(scalar
.ScalarValue(
109 results
.current_page
, 'response_start', 'ms', response_start
,
112 response_end
= (float(response_end
) - navigation_start
)
113 results
.AddValue(scalar
.ScalarValue(
114 results
.current_page
, 'response_end', 'ms', response_end
,
117 # Phase measurements in milliseconds.
118 domain_lookup_duration
= (float(domain_lookup_end
) - domain_lookup_start
)
119 results
.AddValue(scalar
.ScalarValue(
120 results
.current_page
, 'domain_lookup_duration', 'ms',
121 domain_lookup_duration
, important
=False))
123 connect_duration
= (float(connect_end
) - connect_start
)
124 results
.AddValue(scalar
.ScalarValue(
125 results
.current_page
, 'connect_duration', 'ms', connect_duration
,
128 request_duration
= (float(response_start
) - request_start
)
129 results
.AddValue(scalar
.ScalarValue(
130 results
.current_page
, 'request_duration', 'ms', request_duration
,
133 response_duration
= (float(response_end
) - response_start
)
134 results
.AddValue(scalar
.ScalarValue(
135 results
.current_page
, 'response_duration', 'ms', response_duration
,