Change next_proto member type.
[chromium-blink-merge.git] / tools / perf / metrics / webrtc_stats.py
blob4a787dd1afa16d87a8e5a9cda6d7a0cb689ad6ec
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.
5 import json
6 import logging
7 import re
9 from metrics import Metric
10 from telemetry.core import camel_case
11 from telemetry.value import list_of_scalar_values
13 INTERESTING_METRICS = {
14 'packetsReceived': {
15 'units': 'packets',
16 'description': 'Packets received by the peer connection',
18 'packetsSent': {
19 'units': 'packets',
20 'description': 'Packets sent by the peer connection',
22 'googDecodeMs': {
23 'units': 'ms',
24 'description': 'Time spent decoding.',
26 'googMaxDecodeMs': {
27 'units': 'ms',
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:
36 return 'audio'
37 if 'googFrameRateSent' in report or 'googFrameRateReceived' in report:
38 return 'video'
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
43 # report
44 return 'unknown'
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):
56 time_series = {}
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:
61 continue
62 if GetReportKind(report) == 'unknown':
63 continue
64 full_stat_name = DistinguishAudioAndVideo(report, stat_name)
65 time_series.setdefault(full_stat_name, []).append(float(value))
67 return time_series
70 class WebRtcStatisticsMetric(Metric):
71 """Makes it possible to measure stats from peer connections."""
73 def __init__(self):
74 super(WebRtcStatisticsMetric, self).__init__()
75 self._all_reports = None
77 def Start(self, page, tab):
78 pass
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:
87 return
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'],
101 important=False))