OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 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 | |
5 """Media Metrics class injects and calls JS responsible for recording metrics. | |
6 | |
7 Default media metrics are collected for every media element in the page, such as | |
8 decoded_frame_count, dropped_frame_count, decoded_video_bytes, and | |
9 decoded_audio_bytes. | |
10 """ | |
11 | |
12 import logging | |
13 import os | |
14 | |
15 | |
16 class MediaMetrics(object): | |
17 def __init__(self, tab): | |
18 # Measurement class and media actions class can both load media_metrics.js. | |
nduca
2013/07/09 23:07:19
is this comment still true?
shadi
2013/07/11 00:49:13
Oops, removed.
| |
19 deps_found = tab.EvaluateJavaScript('window.__mediaRecorders != undefined') | |
20 if not deps_found: | |
21 with open( | |
22 os.path.join(os.path.dirname(__file__), 'media_metrics.js')) as f: | |
23 js = f.read() | |
24 tab.ExecuteJavaScript(js) | |
25 self.tab = tab | |
26 | |
nduca
2013/07/09 23:07:19
you should have a Start/Stop/Add in style of memor
| |
27 def ReportCollectedMetrics(self, results): | |
nduca
2013/07/09 23:07:19
your naming shoudl conform to memory_metrics.py
shadi
2013/07/11 00:49:13
Done.
| |
28 """Reports all recorded metrics as Telemetry perf results. | |
29 | |
30 Metrics are recorded for each media element in the page. Each metric result | |
31 has media info + metric values as a map, for example: | |
32 Media metric = { | |
33 'info': { | |
34 'src': 'file.webm', | |
35 'id': 'video1', | |
36 ... | |
37 }, | |
38 'metrics': { | |
39 'ttp': [120, 'ms'], | |
40 'decoded_bytes': [13233, 'bytes'], | |
41 ... | |
42 } | |
43 } | |
44 """ | |
45 media_metrics = self.tab.EvaluateJavaScript('window.__getAllMetrics()') | |
46 for media_metric in media_metrics: | |
47 AddResult(media_metric, results) | |
48 | |
49 | |
50 def AddResult(media_metric, results): | |
51 """Adds the media metric result to global results, needed for graphing.""" | |
52 info = media_metric['info'] | |
53 metrics = media_metric['metrics'] | |
54 for metric in metrics: | |
nduca
2013/07/09 23:07:19
i'd love this to be a bit more strongly typed. Whe
shadi
2013/07/11 00:49:13
The idea is that we don't know how many metrics ha
| |
55 value, units = GetValueUnit(metrics[metric]) | |
56 if not units: | |
57 logging.error('Missing units value for media metric %s.', metric) | |
58 continue | |
59 trace = GetTraceName(info) | |
60 results.Add(trace, units, value, chart_name=metric, data_type='default') | |
61 | |
62 | |
63 def GetValueUnit(metric): | |
64 """Returns a (value, units) pair stored in a metric if available.""" | |
65 if isinstance(metric, (list, tuple)): | |
nduca
2013/07/09 23:07:19
per note above, rather this wasn't here and you ha
shadi
2013/07/11 00:49:13
See earlier comment.
| |
66 if len(metric) > 1: | |
67 return (metric[0], metric[1]) | |
68 else: | |
69 return (metric[0], None) | |
70 return (metric, None) | |
71 | |
72 | |
73 def GetTraceName(info): | |
nduca
2013/07/09 23:07:19
per note above, rather this wasn't here and you ha
shadi
2013/07/11 00:49:13
See earlier comment.
| |
74 """Returns a media trace name based on the metric info map. | |
75 | |
76 In order, returns the first available: | |
77 - 'id' key value. | |
78 - file name based on 'src' key value. | |
79 - the first key value available. | |
80 - 'no_trace'. | |
81 """ | |
82 if 'id' in info: | |
83 return info['id'] | |
84 elif 'src' in info: | |
85 # Return only file name in src. | |
86 src = info['src'] | |
87 return src[src.rfind('/') + 1:] | |
88 else: | |
89 for key in info: | |
90 return info[key] | |
91 return 'no_trace' | |
OLD | NEW |