aboutsummaryrefslogtreecommitdiff
path: root/catapult/devil/devil/android/perf/perf_control_test.py
blob: bde54d5a5180305a2efb4b74da47ac09de8844a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from devil import devil_env
from devil.android import device_utils
from devil.android.perf import perf_control
from devil.android.sdk import adb_wrapper

with devil_env.SysPath(devil_env.PYMOCK_PATH):
  import mock


# pylint: disable=unused-argument
def _ShellCommandHandler(cmd,
                         shell=False,
                         check_return=False,
                         cwd=None,
                         env=None,
                         run_as=None,
                         as_root=False,
                         single_line=False,
                         large_output=False,
                         raw_output=False,
                         timeout=None,
                         retries=None):
  if cmd.startswith('for CPU in '):
    if 'scaling_available_governors' in cmd:
      contents = 'interactive ondemand userspace powersave performance'
      return [contents + '\n%~%0%~%'] * 4
    if 'cat "$CPU/online"' in cmd:
      return ['1\n%~%0%~%'] * 4
  assert False, 'Should not be called with cmd: {}'.format(cmd)


class PerfControlTest(unittest.TestCase):
  @staticmethod
  def _MockOutLowLevelPerfControlMethods(perf_control_object):
    # pylint: disable=protected-access
    perf_control_object.SetScalingGovernor = mock.Mock()
    perf_control_object._ForceAllCpusOnline = mock.Mock()
    perf_control_object._SetScalingMaxFreqForCpus = mock.Mock()
    perf_control_object._SetMaxGpuClock = mock.Mock()

  # pylint: disable=no-self-use
  def testNexus5HighPerfMode(self):
    # Mock out the device state for PerfControl.
    cpu_list = ['cpu%d' % cpu for cpu in xrange(4)]
    mock_device = mock.Mock(spec=device_utils.DeviceUtils)
    mock_device.product_model = 'Nexus 5'
    mock_device.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
    mock_device.ListDirectory.return_value = cpu_list + ['cpufreq']
    mock_device.FileExists.return_value = True
    mock_device.RunShellCommand = mock.Mock(side_effect=_ShellCommandHandler)
    pc = perf_control.PerfControl(mock_device)
    self._MockOutLowLevelPerfControlMethods(pc)

    # Verify.
    # pylint: disable=protected-access
    # pylint: disable=no-member
    pc.SetHighPerfMode()
    mock_device.EnableRoot.assert_called_once_with()
    pc._ForceAllCpusOnline.assert_called_once_with(True)
    pc.SetScalingGovernor.assert_called_once_with('performance')
    pc._SetScalingMaxFreqForCpus.assert_called_once_with(
        1190400, ' '.join(cpu_list))
    pc._SetMaxGpuClock.assert_called_once_with(200000000)

  def testNexus5XHighPerfMode(self):
    # Mock out the device state for PerfControl.
    cpu_list = ['cpu%d' % cpu for cpu in xrange(6)]
    mock_device = mock.Mock(spec=device_utils.DeviceUtils)
    mock_device.product_model = 'Nexus 5X'
    mock_device.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
    mock_device.ListDirectory.return_value = cpu_list + ['cpufreq']
    mock_device.FileExists.return_value = True
    mock_device.RunShellCommand = mock.Mock(side_effect=_ShellCommandHandler)
    pc = perf_control.PerfControl(mock_device)
    self._MockOutLowLevelPerfControlMethods(pc)

    # Verify.
    # pylint: disable=protected-access
    # pylint: disable=no-member
    pc.SetHighPerfMode()
    mock_device.EnableRoot.assert_called_once_with()
    pc._ForceAllCpusOnline.assert_called_once_with(True)
    pc.SetScalingGovernor.assert_called_once_with('performance')
    pc._SetScalingMaxFreqForCpus.assert_called_once_with(
        1248000, ' '.join(cpu_list))
    pc._SetMaxGpuClock.assert_called_once_with(300000000)

  def testNexus5XDefaultPerfMode(self):
    # Mock out the device state for PerfControl.
    cpu_list = ['cpu%d' % cpu for cpu in xrange(6)]
    mock_device = mock.Mock(spec=device_utils.DeviceUtils)
    mock_device.product_model = 'Nexus 5X'
    mock_device.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
    mock_device.ListDirectory.return_value = cpu_list + ['cpufreq']
    mock_device.FileExists.return_value = True
    mock_device.RunShellCommand = mock.Mock(side_effect=_ShellCommandHandler)
    pc = perf_control.PerfControl(mock_device)
    self._MockOutLowLevelPerfControlMethods(pc)

    # Verify.
    # pylint: disable=protected-access
    # pylint: disable=no-member
    pc.SetDefaultPerfMode()
    pc.SetScalingGovernor.assert_called_once_with('ondemand')
    pc._SetScalingMaxFreqForCpus.assert_any_call(1440000, 'cpu0 cpu1 cpu2 cpu3')
    pc._SetScalingMaxFreqForCpus.assert_any_call(1824000, 'cpu4 cpu5')
    pc._SetMaxGpuClock.assert_called_once_with(600000000)
    pc._ForceAllCpusOnline.assert_called_once_with(False)