summaryrefslogtreecommitdiffstats
path: root/tools/perf/metrics/webrtc_stats_unittest.py
blob: 6b746cac96641832b4355a6ba0b26394219112e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from telemetry.testing import simple_mock

from metrics import webrtc_stats


SAMPLE_JSON = '''
[[
   [
      {
         "googFrameHeightInput":"480",
         "googFrameWidthInput":"640",
         "googFrameRateSent": "23",
         "packetsLost":"-1",
         "googRtt":"19",
         "packetsSent":"1",
         "bytesSent":"0"
      },
      {
         "audioInputLevel":"2048",
         "googRtt":"20",
         "googCodecName":"opus",
         "packetsSent":"4",
         "bytesSent":"0"
      }
   ],
   [
      {
         "googFrameHeightInput":"480",
         "googFrameWidthInput":"640",
         "googFrameRateSent": "21",
         "packetsLost":"-1",
         "googRtt":"18",
         "packetsSent":"8",
         "bytesSent":"6291"
      },
      {
         "audioInputLevel":"1878",
         "googRtt":"17",
         "googCodecName":"opus",
         "packetsSent":"16",
         "bytesSent":"634"
      }
   ],
   [
      {
          "googAvailableSendBandwidth":"30000",
          "googAvailableRecvBandwidth":"12345",
          "googTargetEncBitrate":"10000"
      }
  ]
],
[
   [
      {
         "googFrameRateReceived": "23",
         "googDecodeMs":"0",
         "packetsReceived":"8",
         "googRenderDelayMs":"10",
         "googMaxDecodeMs":"0",
         "googRtt":"100"
      }
   ],
   [
      {
         "googFrameRateReceived": "23",
         "googDecodeMs":"14",
         "packetsReceived":"1234",
         "googRenderDelayMs":"102",
         "googMaxDecodeMs":"150",
         "googRtt":"101"
      }
   ],
   [
      {
          "googAvailableSendBandwidth":"40000",
          "googAvailableRecvBandwidth":"22345",
          "googTargetEncBitrate":"20000"
      }
  ]
]]
'''


class FakeResults(object):
  def __init__(self, current_page):
    self._received_values = []
    self._current_page = current_page

  @property
  def received_values(self):
    return self._received_values

  @property
  def current_page(self):
    return self._current_page

  def AddValue(self, value):
    self._received_values.append(value)


class WebRtcStatsUnittest(unittest.TestCase):

  def _RunMetricOnJson(self, json_to_return):
    stats_metric = webrtc_stats.WebRtcStatisticsMetric()

    tab = simple_mock.MockObject()
    page = simple_mock.MockObject()

    stats_metric.Start(page, tab)

    tab.ExpectCall('EvaluateJavaScript',
                   simple_mock.DONT_CARE).WillReturn(json_to_return)
    stats_metric.Stop(page, tab)

    page.url = simple_mock.MockObject()
    results = FakeResults(page)
    stats_metric.AddResults(tab, results)
    return results

  def testExtractsValuesAsTimeSeries(self):
    results = self._RunMetricOnJson(SAMPLE_JSON)

    self.assertTrue(results.received_values,
                    'Expected values for googDecodeMs and others, got none.')

    # This also ensures we're clever enough to tell video packetsSent from audio
    # packetsSent.
    self.assertEqual(results.received_values[3].values,
                     [4.0, 16.0])
    self.assertEqual(results.received_values[5].values,
                     [1.0, 8.0])

  def testExtractsInterestingMetricsOnly(self):
    results = self._RunMetricOnJson(SAMPLE_JSON)

    self.assertTrue(len(results.received_values) > 0)
    self.assertIn('peer_connection_0', results.received_values[0].name,
                  'The result should be a ListOfScalarValues instance with '
                  'a name <peer connection id>_<statistic>.')
    all_names = [value.name for value in results.received_values]
    self.assertIn('peer_connection_0_video_packets_sent', all_names)
    self.assertNotIn('peer_connection_1_video_packets_sent', all_names,
                     'Peer connection 1 does not have a video packets sent in '
                     'the JSON above, unlike peer connection 0 which does.')
    self.assertIn('peer_connection_0_video_goog_rtt', all_names)
    self.assertIn('peer_connection_1_video_goog_rtt', all_names)
    # The audio_audio is intentional since the code distinguishes audio repots
    # from video reports (even though audio_input_level is quite obvious).
    self.assertNotIn('peer_connection_0_audio_audio_input_level', all_names,
                     'Input level is in the JSON for both connections but '
                     'should not be reported since it is not interesting.')
    self.assertNotIn('peer_connection_1_audio_audio_input_level', all_names)

  def testReturnsIfJsonIsEmpty(self):
    results = self._RunMetricOnJson('[]')
    self.assertFalse(results.received_values)