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
.debug('Did not recognize report batch: %s.', report
.keys())
41 # There are other kinds of reports, such as bwestats, which we don't care
42 # about here. For these cases just return 'unknown' which will ignore the
47 def DistinguishAudioAndVideo(report
, stat_name
):
48 return GetReportKind(report
) + '_' + stat_name
51 def StripAudioVideoDistinction(stat_name
):
52 return re
.sub('^(audio|video)_', '', stat_name
)
55 def SortStatsIntoTimeSeries(report_batches
):
57 for report_batch
in report_batches
:
58 for report
in report_batch
:
59 for stat_name
, value
in report
.iteritems():
60 if stat_name
not in INTERESTING_METRICS
:
62 if GetReportKind(report
) == 'unknown':
64 full_stat_name
= DistinguishAudioAndVideo(report
, stat_name
)
65 time_series
.setdefault(full_stat_name
, []).append(float(value
))
70 class WebRtcStatisticsMetric(Metric
):
71 """Makes it possible to measure stats from peer connections."""
74 super(WebRtcStatisticsMetric
, self
).__init
__()
75 self
._all
_reports
= None
77 def Start(self
, page
, tab
):
80 def Stop(self
, page
, tab
):
81 """Digs out stats from data populated by the javascript in webrtc_cases."""
82 self
._all
_reports
= tab
.EvaluateJavaScript(
83 'JSON.stringify(window.peerConnectionReports)')
85 def AddResults(self
, tab
, results
):
86 if not self
._all
_reports
:
89 reports
= json
.loads(self
._all
_reports
)
90 for i
, report
in enumerate(reports
):
91 time_series
= SortStatsIntoTimeSeries(report
)
93 for stat_name
, values
in time_series
.iteritems():
94 stat_name_underscored
= camel_case
.ToUnderscore(stat_name
)
95 trace_name
= 'peer_connection_%d_%s' % (i
, stat_name_underscored
)
96 general_name
= StripAudioVideoDistinction(stat_name
)
97 results
.AddValue(list_of_scalar_values
.ListOfScalarValues(
98 results
.current_page
, trace_name
,
99 INTERESTING_METRICS
[general_name
]['units'], values
,
100 description
=INTERESTING_METRICS
[general_name
]['description'],