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.
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
15 class PowerMetricsPowerMonitorTest(unittest.TestCase):
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()))
27 def testParseEmptyPowerMetricsOutput(self):
28 # Important to handle zero length powermetrics outout - crbug.com/353250 .
29 self.assertIsNone(powermetrics_power_monitor.PowerMetricsPowerMonitor.
30 ParsePowerMetricsOutput(''))
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))
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.')
47 # Supported hardware reports power samples and energy consumption.
48 result = getOutput('powermetrics_output.output')
50 self.assertTrue(len(result['power_samples_mw']) > 1)
51 self.assertTrue(result['energy_consumption_mwh'] > 0)
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)
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)