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
3 # found in the LICENSE file.
9 from metrics import Metric
10 from telemetry.core import camel_case
11 from telemetry.value import list_of_scalar_values
13 INTERESTING_METRICS = {
16 'description': 'Packets received by the peer connection',
20 'description': 'Packets sent by the peer connection',
24 'description': 'Time spent decoding.',
28 'description': 'Maximum time spent decoding one frame.',
30 # TODO(phoglund): Add much more interesting metrics.
34 def GetReportKind(report):
35 if 'audioInputLevel' in report or 'audioOutputLevel' in report:
37 if 'googFrameRateSent' in report or 'googFrameRateReceived' in report:
40 logging.error('Did not recognize report batch: %s.', report.keys())
44 def DistinguishAudioAndVideo(report, stat_name):
45 return GetReportKind(report) + '_' + stat_name
48 def StripAudioVideoDistinction(stat_name):
49 return re.sub('^(audio|video)_', '', stat_name)
52 def SortStatsIntoTimeSeries(report_batches):
54 for report_batch in report_batches:
55 for report in report_batch:
56 for stat_name, value in report.iteritems():
57 if stat_name not in INTERESTING_METRICS:
59 full_stat_name = DistinguishAudioAndVideo(report, stat_name)
60 time_series.setdefault(full_stat_name, []).append(float(value))
65 class WebRtcStatisticsMetric(Metric):
66 """Makes it possible to measure stats from peer connections."""
69 super(WebRtcStatisticsMetric, self).__init__()
70 self._all_reports = None
72 def Start(self, page, tab):
75 def Stop(self, page, tab):
76 """Digs out stats from data populated by the javascript in webrtc_cases."""
77 self._all_reports = tab.EvaluateJavaScript(
78 'JSON.stringify(window.peerConnectionReports)')
80 def AddResults(self, tab, results):
81 if not self._all_reports:
84 reports = json.loads(self._all_reports)
85 for i, report in enumerate(reports):
86 time_series = SortStatsIntoTimeSeries(report)
88 for stat_name, values in time_series.iteritems():
89 stat_name_underscored = camel_case.ToUnderscore(stat_name)
90 trace_name = 'peer_connection_%d_%s' % (i, stat_name_underscored)
91 general_name = StripAudioVideoDistinction(stat_name)
92 results.AddValue(list_of_scalar_values.ListOfScalarValues(
93 results.current_page, trace_name,
94 INTERESTING_METRICS[general_name]['units'], values,
95 description=INTERESTING_METRICS[general_name]['description'],