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