ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / tools / perf / metrics / media.py
blob6680d8487d4738b5a83ee655096aa461d1218804
1 # Copyright 2013 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 import logging
5 import os
7 from metrics import Metric
8 from telemetry.value import list_of_scalar_values
9 from telemetry.value import scalar
12 class MediaMetric(Metric):
13 """MediaMetric class injects and calls JS responsible for recording metrics.
15 Default media metrics are collected for every media element in the page,
16 such as decoded_frame_count, dropped_frame_count, decoded_video_bytes, and
17 decoded_audio_bytes.
18 """
20 def __init__(self, tab):
21 super(MediaMetric, self).__init__()
22 with open(os.path.join(os.path.dirname(__file__), 'media.js')) as f:
23 js = f.read()
24 tab.ExecuteJavaScript(js)
25 self._results = None
26 self._skip_basic_metrics = False
28 def Start(self, page, tab):
29 """Create the media metrics for all media elements in the document."""
30 if hasattr(page, 'skip_basic_metrics'):
31 self._skip_basic_metrics = page.skip_basic_metrics
32 tab.ExecuteJavaScript('window.__createMediaMetricsForDocument()')
34 def Stop(self, page, tab):
35 self._results = tab.EvaluateJavaScript('window.__getAllMetrics()')
37 # Optional |exclude_metrics| args are not in base class Metric.
38 # pylint: disable=W0221
39 def AddResults(self, tab, results, exclude_metrics=None):
40 """Reports all recorded metrics as Telemetry perf results."""
41 exclude_metrics = exclude_metrics or []
42 trace_names = []
43 for media_metric in self._results:
44 trace_names.append(self._AddResultsForMediaElement(media_metric, results,
45 exclude_metrics))
47 return '_'.join(trace_names) or tab.url
49 def _AddResultsForMediaElement(self, media_metric, results, exclude_metrics):
50 """Reports metrics for one media element.
52 Media metrics contain an ID identifying the media element and values:
53 media_metric = {
54 'id': 'video_1',
55 'metrics': {
56 'time_to_play': 120,
57 'decoded_bytes': 13233,
58 ...
61 """
62 def AddOneResult(metric, unit):
63 if metric in exclude_metrics:
64 return
66 metrics = media_metric['metrics']
67 for m in metrics:
68 if m.startswith(metric):
69 special_label = m[len(metric):]
70 trace_name = '%s.%s%s' % (metric, trace, special_label)
71 if isinstance(metrics[m], list):
72 results.AddValue(list_of_scalar_values.ListOfScalarValues(
73 results.current_page, trace_name, unit,
74 values=[float(v) for v in metrics[m]],
75 important=True))
76 else:
77 results.AddValue(scalar.ScalarValue(
78 results.current_page, trace_name, unit, value=float(metrics[m]),
79 important=True))
81 trace = media_metric['id']
82 if not trace:
83 logging.error('Metrics ID is missing in results.')
84 return
86 if not self._skip_basic_metrics:
87 AddOneResult('buffering_time', 'ms')
88 AddOneResult('decoded_audio_bytes', 'bytes')
89 AddOneResult('decoded_video_bytes', 'bytes')
90 AddOneResult('decoded_frame_count', 'frames')
91 AddOneResult('dropped_frame_count', 'frames')
92 AddOneResult('time_to_play', 'ms')
94 AddOneResult('avg_loop_time', 'ms')
95 AddOneResult('seek', 'ms')
96 return trace