OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 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 json |
| 6 import logging |
| 7 import os |
| 8 import subprocess |
| 9 |
| 10 import py_utils |
| 11 |
| 12 from telemetry.core import exceptions |
| 13 from telemetry.value import histogram_util |
| 14 |
| 15 |
| 16 def _RunRemoteCommand(dut_ip, cmd): |
| 17 return os.system('ssh root@%s %s' % (dut_ip, cmd)) |
| 18 |
| 19 |
| 20 def _GetTabSwitchHistogram(browser): |
| 21 """Gets MPArch.RWH_TabSwitchPaintDuration histogram. |
| 22 |
| 23 Catches exceptions to handle devtools context lost cases. |
| 24 |
| 25 Any tab context can be used to get the TabSwitchPaintDuration histogram. |
| 26 browser.tabs[-1] is the last valid context. Ex: If the browser opens |
| 27 A, B, ..., I, J tabs, E, F, G tabs have valid contexts (not discarded), |
| 28 then browser.tab[0] is tab E, browser.tab[-1] is tab G. |
| 29 |
| 30 In the tab switching benchmark, the tabs are opened and switched to in |
| 31 1, 2, ..., n order. The tabs are discarded in roughly 1, 2, ..., n order |
| 32 (LRU order). The chance of discarding the last valid context is lower |
| 33 than discarding the first valid context. |
| 34 |
| 35 Args: |
| 36 browser: Gets histogram from this browser. |
| 37 |
| 38 Returns: |
| 39 A json serialization of a histogram or None if get histogram failed. |
| 40 """ |
| 41 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
| 42 histogram_type = histogram_util.BROWSER_HISTOGRAM |
| 43 try: |
| 44 return histogram_util.GetHistogram( |
| 45 histogram_type, histogram_name, browser.tabs[-1]) |
| 46 except (exceptions.DevtoolsTargetCrashException, KeyError): |
| 47 logging.warning('GetHistogram: Devtools context lost.') |
| 48 except exceptions.TimeoutException: |
| 49 logging.warning('GetHistogram: Timed out getting histogram.') |
| 50 return None |
| 51 |
| 52 |
| 53 def GetTabSwitchHistogramRetry(browser): |
| 54 """Retries getting histogram as it may fail when a context was discarded. |
| 55 |
| 56 Args: |
| 57 browser: Gets histogram from this browser. |
| 58 |
| 59 Returns: |
| 60 A json serialization of a histogram. |
| 61 |
| 62 Raises: |
| 63 py_utils.TimeoutException: There is no valid histogram in 10 seconds. |
| 64 """ |
| 65 return py_utils.WaitFor(lambda: _GetTabSwitchHistogram(browser), 10) |
| 66 |
| 67 |
| 68 def WaitTabSwitching(browser, prev_histogram): |
| 69 """Waits for tab switching completion. |
| 70 |
| 71 It's done by checking browser histogram to see if |
| 72 RWH_TabSwitchPaintDuration count increases. |
| 73 |
| 74 Args: |
| 75 browser: Gets histogram from this browser. |
| 76 prev_histogram: Checks histogram change against this histogram. |
| 77 """ |
| 78 def _IsDone(): |
| 79 cur_histogram = _GetTabSwitchHistogram(browser) |
| 80 if not cur_histogram: |
| 81 return False |
| 82 |
| 83 diff_histogram = histogram_util.SubtractHistogram( |
| 84 cur_histogram, prev_histogram) |
| 85 diff_histogram_count = json.loads(diff_histogram).get('count', 0) |
| 86 return diff_histogram_count > 0 |
| 87 |
| 88 try: |
| 89 py_utils.WaitFor(_IsDone, 10) |
| 90 except py_utils.TimeoutException: |
| 91 logging.warning('Timed out waiting for histogram count increasing.') |
| 92 |
| 93 |
| 94 class KeyboardEmulator(object): |
| 95 """Sets up a remote emulated keyboard and sends key events to switch tab. |
| 96 |
| 97 Example usage: |
| 98 with KeyboardEmulator(DUT_IP) as kbd: |
| 99 for i in range(5): |
| 100 kbd.SwitchTab() |
| 101 """ |
| 102 REMOTE_LOG_KEY_FILENAME = '/usr/local/tmp/log_key_tab_switch' |
| 103 |
| 104 def __init__(self, dut_ip): |
| 105 """Inits KeyboardEmulator. |
| 106 |
| 107 Args: |
| 108 dut_ip: DUT IP or hostname. |
| 109 """ |
| 110 self._dut_ip = dut_ip |
| 111 self._root_dut_ip = 'root@' + dut_ip |
| 112 self._key_device_name = None |
| 113 |
| 114 def _StartRemoteKeyboardEmulator(self): |
| 115 """Starts keyboard emulator on the DUT. |
| 116 |
| 117 Returns: |
| 118 The device name of the emulated keyboard. |
| 119 |
| 120 Raises: |
| 121 RuntimeError: Keyboard emulation failed. |
| 122 """ |
| 123 kbd_prop_filename = '/usr/local/autotest/cros/input_playback/keyboard.prop' |
| 124 |
| 125 ret = _RunRemoteCommand(self._dut_ip, 'test -e %s' % kbd_prop_filename) |
| 126 if ret != 0: |
| 127 raise RuntimeError('Keyboard property file does not exist.') |
| 128 |
| 129 cmd = ['ssh', self._root_dut_ip, 'evemu-device', kbd_prop_filename] |
| 130 ssh_process = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 131 |
| 132 # The evemu-device output format: |
| 133 # Emulated Keyboard: /dev/input/event10 |
| 134 output = ssh_process.stdout.readline() |
| 135 # The remote process would live when the ssh process was terminated. |
| 136 ssh_process.kill() |
| 137 if not output.startswith('Emulated Keyboard:'): |
| 138 raise RuntimeError('Keyboard emulation failed.') |
| 139 key_device_name = output.split()[2] |
| 140 return key_device_name |
| 141 |
| 142 def _SetupKeyDispatch(self): |
| 143 """Uploads the script to send key to switch tabs.""" |
| 144 cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| 145 log_key_filename = os.path.join(cur_dir, 'data', 'log_key_tab_switch') |
| 146 os.system('scp -q %s %s:%s' % |
| 147 (log_key_filename, self._root_dut_ip, |
| 148 KeyboardEmulator.REMOTE_LOG_KEY_FILENAME)) |
| 149 |
| 150 def __enter__(self): |
| 151 self._key_device_name = self._StartRemoteKeyboardEmulator() |
| 152 self._SetupKeyDispatch() |
| 153 return self |
| 154 |
| 155 def SwitchTab(self): |
| 156 """Sending Ctrl-tab key to trigger tab switching.""" |
| 157 cmd = ('"evemu-play --insert-slot0 %s < %s"' % |
| 158 (self._key_device_name, |
| 159 KeyboardEmulator.REMOTE_LOG_KEY_FILENAME)) |
| 160 _RunRemoteCommand(self._dut_ip, cmd) |
| 161 |
| 162 def __exit__(self, exc_type, exc_value, traceback): |
| 163 # Kills the remote emulator process explicitly. |
| 164 _RunRemoteCommand(self._dut_ip, 'pkill evemu-device') |
| 165 |
| 166 |
| 167 def NoScreenOff(dut_ip): |
| 168 """Sets screen always on for 1 hour. |
| 169 |
| 170 Args: |
| 171 dut_ip: DUT IP or hostname. |
| 172 """ |
| 173 _RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_off_delay=3600') |
| 174 _RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_dim_delay=3600') |
OLD | NEW |