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.
5 from telemetry
.value
import scalar
7 from metrics
import Metric
10 NETWORK_DATA_NOT_FOUND
= 'Network data could not be found.'
13 # This is experimental. crbug.com/480512
14 # Will not be supported once network data is ported to TimelineBasedMetric.
15 class NetworkMetric(Metric
):
16 """NetworkMetrics gathers network statistics."""
18 def __init__(self
, platform
):
19 super(NetworkMetric
, self
).__init
__()
20 self
._network
_snd
= None
21 self
._network
_rcv
= None
22 self
._platform
= platform
25 def Start(self
, _
, tab
):
26 """Start the per-page preparation for this metric.
28 Here, this consists of recording the start value.
30 self
._browser
= tab
.browser
31 if not self
._platform
.CanMonitorNetworkData():
34 data
= self
._platform
.GetNetworkData(self
._browser
)
36 self
._network
_snd
, self
._network
_rcv
= data
38 def Stop(self
, _
, tab
):
39 """Prepare the results for this page.
41 The results are the differences between the current values
42 and the values when Start() was called.
44 if not self
._platform
.CanMonitorNetworkData():
47 data
= self
._platform
.GetNetworkData(self
._browser
)
50 if self
._network
_snd
is not None:
51 self
._network
_snd
= snd
- self
._network
_snd
52 if self
._network
_rcv
is not None:
53 self
._network
_rcv
= rcv
- self
._network
_rcv
54 else: # If end data cannot be found, report none.
55 self
._network
_snd
= None
56 self
._network
_rcv
= None
58 def AddResults(self
, tab
, results
):
60 None if self
._network
_snd
is not None else NETWORK_DATA_NOT_FOUND
)
61 results
.AddValue(scalar
.ScalarValue(
62 results
.current_page
, 'network_data_sent', 'kb', self
._network
_snd
,
63 important
=False, none_value_reason
=none_value_reason
))
65 None if self
._network
_rcv
is not None else NETWORK_DATA_NOT_FOUND
)
66 results
.AddValue(scalar
.ScalarValue(
67 results
.current_page
, 'network_data_received', 'kb', self
._network
_rcv
,
68 important
=False, none_value_reason
=none_value_reason
))