1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""CPU, Memory, and FPS performance test for <video>.
7
8Calculates decoded fps, dropped fps, CPU, and memory statistics while playing
9HTML5 media element. The test compares results of playing a media file on
10different video resolutions.
11"""
12
13import logging
14import os
15import psutil
16
17import pyauto_media
18import pyauto
19import pyauto_utils
20
21# HTML test path; relative to src/chrome/test/data.
22_TEST_HTML_PATH = os.path.join('media', 'html', 'media_stat_perf.html')
23
24# Path under data path for test files.
25_TEST_MEDIA_PATH_CROWD = os.path.join('pyauto_private', 'media', 'crowd')
26
27# Path under data path for test files.
28_TEST_MEDIA_PATH_TULIP = os.path.join('media', 'avperf', 'tulip')
29# The media files used for testing.
30_TEST_VIDEOS = [os.path.join(_TEST_MEDIA_PATH_CROWD, name) for name in [
31                'crowd2160.webm', 'crowd1080.webm']]
32_TEST_VIDEOS.extend([os.path.join(_TEST_MEDIA_PATH_TULIP, name) for name in [
33    'tulip2.webm', 'tulip2.wav', 'tulip2.ogv', 'tulip2.ogg', 'tulip2.mp4',
34    'tulip2.mp3', 'tulip2.m4a']])
35
36class MediaStatsPerfTest(pyauto.PyUITest):
37  """PyAuto test container.  See file doc string for more information."""
38
39  def _GetChromeRendererProcess(self):
40    """Returns the Chrome renderer process."""
41    renderer_id = self.GetBrowserInfo()['windows'][0]['tabs'][1]['renderer_pid']
42    if not renderer_id:
43      self.fail('Can not find the tab renderer process.')
44    return psutil.Process(renderer_id)
45
46  def testMediaPerformance(self):
47    """Launches HTML test which plays each video and records statistics."""
48    for file_name in _TEST_VIDEOS:
49      # Append a tab and delete it at the end of the test to free its memory.
50      self.AppendTab(pyauto.GURL(self.GetFileURLForDataPath(_TEST_HTML_PATH)))
51
52      file_url = self.GetFileURLForDataPath(file_name)
53      logging.debug('Running perf test for %s.', file_url)
54
55      renderer_process = self._GetChromeRendererProcess()
56      # Call to set a starting time to record CPU usage by the renderer.
57      renderer_process.get_cpu_percent()
58
59      self.assertTrue(
60          self.CallJavascriptFunc('startTest', [file_url], tab_index=1))
61
62      cpu_usage = renderer_process.get_cpu_percent()
63      mem_usage_mb = renderer_process.get_memory_info()[0] / 1024
64      file_name = os.path.basename(file_name)
65      pyauto_utils.PrintPerfResult('cpu', file_name, cpu_usage, '%')
66      pyauto_utils.PrintPerfResult('memory', file_name, mem_usage_mb, 'KB')
67
68      decoded_fps = [
69          float(value) for value in
70          self.GetDOMValue("decodedFPS.join(',')", tab_index=1).split(',')]
71      dropped_frames = self.GetDOMValue('droppedFrames', tab_index=1)
72      dropped_fps = [
73          float(value) for value in
74          self.GetDOMValue("droppedFPS.join(',')", tab_index=1).split(',')]
75
76      pyauto_utils.PrintPerfResult('fps', file_name, decoded_fps, 'fps')
77      pyauto_utils.PrintPerfResult('dropped_fps', file_name, dropped_fps, 'fps')
78      pyauto_utils.PrintPerfResult('dropped_frames', file_name, dropped_frames,
79                                   'frames')
80
81      self.CloseTab(tab_index=1)
82
83
84if __name__ == '__main__':
85  pyauto_media.Main()
86