Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / power_monitor / powermetrics_power_monitor_unittest.py
1 # Copyright 2014 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 import logging
6 import os
7 import unittest
8
9 from telemetry import test
10 from telemetry.core import util
11 from telemetry.core.platform import mac_platform_backend
12 from telemetry.core.platform.power_monitor import powermetrics_power_monitor
13
14
15 class PowerMetricsPowerMonitorTest(unittest.TestCase):
16   @test.Enabled('mac')
17   def testCanMonitorPowerUsage(self):
18     backend = mac_platform_backend.MacPlatformBackend()
19     power_monitor = powermetrics_power_monitor.PowerMetricsPowerMonitor(backend)
20     mavericks_or_later = (
21         backend.GetOSVersionName() >= mac_platform_backend.MAVERICKS)
22     # Should always be able to monitor power usage on OS Version >= 10.9 .
23     self.assertEqual(power_monitor.CanMonitorPower(), mavericks_or_later,
24         "Error checking powermetrics availability: '%s'" % '|'.join(os.uname()))
25
26   @test.Enabled('mac')
27   def testParseEmptyPowerMetricsOutput(self):
28     # Important to handle zero length powermetrics outout - crbug.com/353250 .
29     self.assertIsNone(powermetrics_power_monitor.PowerMetricsPowerMonitor.
30         ParsePowerMetricsOutput(''))
31
32   @test.Enabled('mac')
33   def testParsePowerMetricsOutput(self):
34     def getOutput(output_file):
35       test_data_path = os.path.join(util.GetUnittestDataDir(), output_file)
36       with open(test_data_path, 'r') as f:
37         process_output = f.read()
38       return (powermetrics_power_monitor.PowerMetricsPowerMonitor.
39           ParsePowerMetricsOutput(process_output))
40
41     power_monitor = powermetrics_power_monitor.PowerMetricsPowerMonitor(
42         mac_platform_backend.MacPlatformBackend())
43     if not power_monitor.CanMonitorPower():
44       logging.warning('Test not supported on this platform.')
45       return
46
47     # Supported hardware reports power samples and energy consumption.
48     result = getOutput('powermetrics_output.output')
49
50     self.assertTrue(len(result['power_samples_mw']) > 1)
51     self.assertTrue(result['energy_consumption_mwh'] > 0)
52
53     # Verify that all component entries exist in output.
54     component_utilization = result['component_utilization']
55     for k in ['whole_package', 'gpu'] + ['cpu%d' % x for x in range(8)]:
56       self.assertTrue(component_utilization[k]['average_frequency_hz'] > 0)
57       self.assertTrue(component_utilization[k]['idle_percent'] > 0)
58
59     # Unsupported hardware doesn't.
60     result = getOutput('powermetrics_output_unsupported_hardware.output')
61     self.assertNotIn('power_samples_mw', result)
62     self.assertNotIn('energy_consumption_mwh', result)