Chromium Code Reviews| 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 changce of discarding the last valid context is lower | |
| 33 than discarding the first valid context. | |
|
cylee1
2017/05/25 21:21:35
chance
vovoy
2017/05/26 08:19:35
Done.
| |
| 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: | |
|
cylee1
2017/05/25 21:21:35
Does except (exception1, exception2) work to merge
vovoy
2017/05/26 08:19:35
Done.
| |
| 47 logging.info('GetHistogram: devtools context lost') | |
| 48 except KeyError: | |
| 49 logging.info('GetHistogram: devtools context lost') | |
| 50 except Exception as e: | |
| 51 logging.warning('GetHistogram: exception') | |
| 52 logging.warning(e) | |
| 53 return None | |
| 54 | |
| 55 | |
| 56 def GetTabSwitchHistogramRetry(browser): | |
| 57 """Retries getting histogram as it may fail when a context was discarded. | |
| 58 | |
| 59 Args: | |
| 60 browser: Gets histogram from this browser. | |
| 61 | |
| 62 Returns: | |
| 63 A json serialization of a histogram. | |
| 64 Raises: | |
| 65 py_utils.TimeoutException: There is no valid histogram in 10 seconds. | |
| 66 """ | |
| 67 def _TryGetHistogram(): | |
| 68 return _GetTabSwitchHistogram(browser) | |
| 69 return py_utils.WaitFor(_TryGetHistogram, 10) | |
|
cylee1
2017/05/25 21:21:34
Could _TryGetHistogram be replaced by
lambda: _G
vovoy
2017/05/26 08:19:35
Done.
| |
| 70 | |
| 71 | |
| 72 def WaitTabSwitching(browser, prev_histogram): | |
| 73 """Waits for tab switching completion. | |
| 74 | |
| 75 It's done by checking browser histogram to see if | |
| 76 RWH_TabSwitchPaintDuration count increases. | |
| 77 | |
| 78 Args: | |
| 79 browser: Gets histogram from this browser. | |
| 80 prev_histogram: Checks histogram change against this histogram. | |
| 81 """ | |
| 82 def _IsDone(): | |
| 83 cur_histogram = _GetTabSwitchHistogram(browser) | |
| 84 if not cur_histogram: | |
| 85 return False | |
| 86 | |
| 87 diff_histogram = histogram_util.SubtractHistogram( | |
| 88 cur_histogram, prev_histogram) | |
| 89 diff_histogram_count = json.loads(diff_histogram).get('count', 0) | |
| 90 return diff_histogram_count > 0 | |
| 91 | |
| 92 try: | |
| 93 py_utils.WaitFor(_IsDone, 10) | |
| 94 except py_utils.TimeoutException: | |
| 95 logging.warning('Timed out waiting for histogram count increasing.') | |
| 96 | |
| 97 | |
| 98 class KeyboardEmulator(object): | |
| 99 """Sets up a remote emulated keyboard and sends key events to switch tab. | |
| 100 | |
| 101 Example usage: | |
| 102 with KeyboardEmulator(DUT_IP) as kbd: | |
| 103 for i in range(5): | |
| 104 kbd.SwitchTab() | |
| 105 """ | |
| 106 | |
| 107 def __init__(self, dut_ip): | |
| 108 """Inits KeyboardEmulator. | |
| 109 | |
| 110 Args: | |
| 111 dut_ip: DUT IP or hostname. | |
| 112 """ | |
| 113 self._dut_ip = dut_ip | |
| 114 self._root_dut_ip = 'root@' + dut_ip | |
| 115 | |
| 116 def _StartRemoteKeyboardEmulator(self): | |
| 117 """Starts keyboard emulator on the DUT. | |
| 118 | |
| 119 Returns: | |
| 120 The device name of the emulated keyboard. | |
| 121 | |
| 122 Raises: | |
| 123 RuntimeError: Keyboard emulation failed. | |
| 124 """ | |
| 125 kbd_prop_filename = '/usr/local/autotest/cros/input_playback/keyboard.prop' | |
| 126 | |
| 127 ret = _RunRemoteCommand(self._dut_ip, 'test -e %s' % kbd_prop_filename) | |
| 128 if ret != 0: | |
| 129 raise RuntimeError('Keyboard property file does not exist.') | |
| 130 | |
| 131 cmd = ['ssh', self._root_dut_ip, 'evemu-device', kbd_prop_filename] | |
| 132 ssh_process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 133 | |
| 134 # The evemu-device output format: | |
| 135 # Emulated Keyboard: /dev/input/event10 | |
| 136 output = ssh_process.stdout.readline() | |
| 137 if not output.startswith('Emulated Keyboard:'): | |
| 138 raise RuntimeError('Keyboard emulation failed.') | |
|
cylee1
2017/05/25 21:21:35
ssh_process.kill() wouldn't be executed if an erro
vovoy
2017/05/26 08:19:35
Fixed. Kills before throwing exception.
| |
| 139 key_device_name = output.split()[2] | |
| 140 | |
| 141 # The remote process would live when the ssh process was terminated. | |
| 142 ssh_process.kill() | |
| 143 | |
| 144 return key_device_name | |
| 145 | |
| 146 def _SetupKeyDispatch(self, device_name): | |
| 147 """Uploads the script to send key to switch tabs.""" | |
| 148 cur_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 149 send_key_filename = os.path.join(cur_dir, 'data', 'send_key_tab_switch') | |
| 150 log_key_filename = os.path.join(cur_dir, 'data', 'log_key_tab_switch') | |
| 151 | |
| 152 with open(send_key_filename, 'w') as f: | |
| 153 remote_log_key_filename = '/usr/local/tmp/log_key_tab_switch' | |
| 154 f.write('#!/bin/bash\n') | |
|
cylee1
2017/05/25 21:21:34
could you put the whole file content in a string f
vovoy
2017/05/26 08:19:35
Rewritten. Runs the command directly instead of up
| |
| 155 f.write('evemu-play --insert-slot0 %s < %s' % | |
| 156 (device_name, remote_log_key_filename)) | |
| 157 os.chmod(send_key_filename, 0774) | |
| 158 | |
| 159 os.system('scp -q %s %s:/usr/local/tmp/' % | |
| 160 (log_key_filename, self._root_dut_ip)) | |
| 161 os.system('scp -q %s %s:/usr/local/tmp/' % | |
| 162 (send_key_filename, self._root_dut_ip)) | |
| 163 | |
| 164 def __enter__(self): | |
| 165 key_device_name = self._StartRemoteKeyboardEmulator() | |
| 166 self._SetupKeyDispatch(key_device_name) | |
| 167 return self | |
| 168 | |
| 169 def SwitchTab(self): | |
| 170 """Sending Ctrl-tab key to trigger tab switching.""" | |
| 171 _RunRemoteCommand(self._dut_ip, '/usr/local/tmp/send_key_tab_switch') | |
| 172 | |
| 173 def __exit__(self, exc_type, exc_value, traceback): | |
| 174 # Kills the remote emulator process explicitly. | |
| 175 _RunRemoteCommand(self._dut_ip, 'pkill evemu-device') | |
| 176 | |
| 177 | |
| 178 def NoScreenOff(dut_ip): | |
| 179 """Sets screen always on for 1 hour. | |
| 180 | |
| 181 Args: | |
| 182 dut_ip: DUT IP or hostname. | |
| 183 """ | |
| 184 _RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_off_delay=3600') | |
| 185 _RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_dim_delay=3600') | |
| OLD | NEW |