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