Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / tools / perf / metrics / webrtc_stats.py
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.
4
5 import json
6 import logging
7 import re
8
9 from metrics import Metric
10 from telemetry.core import camel_case
11 from telemetry.value import list_of_scalar_values
12
13 INTERESTING_METRICS = {
14     'packetsReceived': {
15         'units': 'packets',
16         'description': 'Packets received by the peer connection',
17     },
18     'packetsSent': {
19         'units': 'packets',
20         'description': 'Packets sent by the peer connection',
21     },
22     'googDecodeMs': {
23         'units': 'ms',
24         'description': 'Time spent decoding.',
25     },
26     'googMaxDecodeMs': {
27         'units': 'ms',
28         'description': 'Maximum time spent decoding one frame.',
29     },
30     # TODO(phoglund): Add much more interesting metrics.
31 }
32
33
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'
39
40   logging.error('Did not recognize report batch: %s.', report.keys())
41   return 'unknown'
42
43
44 def DistinguishAudioAndVideo(report, stat_name):
45   return GetReportKind(report) + '_' + stat_name
46
47
48 def StripAudioVideoDistinction(stat_name):
49   return re.sub('^(audio|video)_', '', stat_name)
50
51
52 def SortStatsIntoTimeSeries(report_batches):
53   time_series = {}
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:
58           continue
59         full_stat_name = DistinguishAudioAndVideo(report, stat_name)
60         time_series.setdefault(full_stat_name, []).append(float(value))
61
62   return time_series
63
64
65 class WebRtcStatisticsMetric(Metric):
66   """Makes it possible to measure stats from peer connections."""
67
68   def __init__(self):
69     super(WebRtcStatisticsMetric, self).__init__()
70     self._all_reports = None
71
72   def Start(self, page, tab):
73     pass
74
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)')
79
80   def AddResults(self, tab, results):
81     if not self._all_reports:
82       return
83
84     reports = json.loads(self._all_reports)
85     for i, report in enumerate(reports):
86       time_series = SortStatsIntoTimeSeries(report)
87
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'],
96             important=False))