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