Roll src/third_party/skia e66fec2:2bd5d02
[chromium-blink-merge.git] / tools / perf / metrics / network.py
blobf71795db955b211e36055a00f084863a0bec4fd7
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
23 self._browser = None
25 def Start(self, _, tab):
26 """Start the per-page preparation for this metric.
28 Here, this consists of recording the start value.
29 """
30 self._browser = tab.browser
31 if not self._platform.CanMonitorNetworkData():
32 return
34 data = self._platform.GetNetworkData(self._browser)
35 if data is not None:
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.
43 """
44 if not self._platform.CanMonitorNetworkData():
45 return
47 data = self._platform.GetNetworkData(self._browser)
48 if data is not None:
49 snd, rcv = data
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):
59 none_value_reason = (
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))
64 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))