Chromium Code Reviews| Index: tools/perf/contrib/cros_benchmarks/cros_utils.py |
| diff --git a/tools/perf/contrib/cros_benchmarks/cros_utils.py b/tools/perf/contrib/cros_benchmarks/cros_utils.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1788bfecf2def64f8cf5c221fbd5a3583fbc6bbf |
| --- /dev/null |
| +++ b/tools/perf/contrib/cros_benchmarks/cros_utils.py |
| @@ -0,0 +1,111 @@ |
| +# Copyright 2017 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 json |
| +import logging |
| +import os |
| +import subprocess |
| +import time |
| + |
| +import py_utils |
| + |
| +from telemetry.core import exceptions |
| +from telemetry.value import histogram_util |
| + |
| +def FindKeyboardDevice(dut_ip): |
|
cywang
2017/05/22 09:04:04
Try to use an emulated keyboard instead(e.g. use e
vovoy
2017/05/23 02:26:10
Done. Using keyboard emulator to send key events.
|
| + """Find the device that supports events EV=120013. |
| + Example /proc/bus/input/devices output for keyboard: |
| + I: Bus=0011 Vendor=0001 Product=0001 Version=ab83 |
| + N: Name="AT Translated Set 2 keyboard" |
| + P: Phys=isa0060/serio0/input0 |
| + S: Sysfs=/devices/platform/i8042/serio0/input/input3 |
| + U: Uniq= |
| + H: Handlers=sysrq event3 |
| + B: PROP=0 |
| + B: EV=120013 |
| + B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe |
| + B: MSC=10 |
| + B: LED=7 |
| + """ |
| + cmd = ['ssh', 'root@' + dut_ip, 'cat', '/proc/bus/input/devices'] |
| + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| + out = p.communicate()[0] |
| + |
| + for line in out.splitlines(): |
| + handler_prefix = 'H: Handlers=' |
| + kbd_event = 'B: EV=120013' |
| + if line.startswith(handler_prefix): |
| + handlers = line[len(handler_prefix):].split() |
| + elif line.startswith(kbd_event): |
| + for handler in handlers: |
| + if handler.startswith('event'): |
| + return '/dev/input/' + handler |
| + raise Exception('keyboard device not found') |
| + |
| +def SetupKeyDispatch(dut_ip): |
| + """Uploading the script to send key to switch tabs.""" |
| + cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| + send_key_filename = cur_dir + '/data/send_key_tab_switch' |
| + log_key_filename = cur_dir + '/data/log_key_tab_switch' |
| + |
| + kbd_dev = FindKeyboardDevice(dut_ip) |
| + send_key_content = '#!/bin/bash\n' |
| + send_key_content += ('evemu-play --insert-slot0 ' + kbd_dev + |
| + ' < /home/root/log_key_tab_switch') |
| + with open(send_key_filename, 'w') as f: |
| + f.write(send_key_content) |
| + os.chmod(send_key_filename, 0774) |
| + |
| + os.system('scp -q %s root@%s:/usr/local/tmp/' % (log_key_filename, dut_ip)) |
| + os.system('scp -q %s root@%s:/usr/local/tmp/' % (send_key_filename, dut_ip)) |
| + |
| +def GetTabSwitchHistogram(tab): |
| + histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' |
| + histogram_type = histogram_util.BROWSER_HISTOGRAM |
| + return histogram_util.GetHistogram( |
| + histogram_type, histogram_name, tab) |
| + |
| +def GetTabSwitchHistogramRetry(browser): |
| + """Retry GetHistogram() as it may fail when a context was discarded.""" |
| + while True: |
| + try: |
| + # Any tab can be used to get the TabSwitchPaintDuration histogram. |
| + # At each retry, try to get valid context again. |
| + return GetTabSwitchHistogram(browser.tabs[-1]) |
| + except exceptions.DevtoolsTargetCrashException: |
| + logging.info('GetHistogram: devtools context lost') |
| + except KeyError: |
| + logging.info('GetHistogram: devtools context lost') |
| + except Exception as e: |
| + logging.warning('GetHistogram: exception') |
| + logging.warning(e) |
| + time.sleep(0.5) |
| + |
| +def SwitchTab(browser): |
| + """Sending Ctrl-tab key to trigger tab switching. Checking the histogram |
| + to wait for tab switching completion.""" |
| + # pylint: disable=protected-access |
| + platform_backend = browser._platform_backend |
| + |
| + prev_histogram = GetTabSwitchHistogramRetry(browser) |
| + |
| + platform_backend.RunCommand('/usr/local/tmp/send_key_tab_switch') |
| + |
| + def _IsDone(): |
| + cur_histogram = GetTabSwitchHistogramRetry(browser) |
| + |
| + diff_histogram = histogram_util.SubtractHistogram(cur_histogram, |
| + prev_histogram) |
| + diff_histogram_count = json.loads(diff_histogram).get('count', 0) |
| + return diff_histogram_count > 0 |
| + |
| + try: |
| + py_utils.WaitFor(_IsDone, 10) |
| + except py_utils.TimeoutException: |
| + logging.warning('wait for histogram timed out') |
| + |
| +def NoScreenOff(platform_backend): |
| + """Set screen off and dim timer to 1 hour.""" |
| + platform_backend.RunCommand('set_power_policy --ac_screen_off_delay=3600') |
| + platform_backend.RunCommand('set_power_policy --ac_screen_dim_delay=3600') |