Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Unified Diff: tools/perf/contrib/cros_benchmarks/cros_utils.py

Issue 2890333002: Tab Switching Benchmark for ChromeOS (Closed)
Patch Set: Using emulated keyboard to send key events. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..637de6d7b859229be8767e10b959ccbb51afbb82
--- /dev/null
+++ b/tools/perf/contrib/cros_benchmarks/cros_utils.py
@@ -0,0 +1,106 @@
+# 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 RunRemoteCommand(dut_ip, cmd):
+ os.system('ssh root@%s %s' % (dut_ip, cmd))
+
+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 WaitTabSwitching(browser, prev_histogram):
+ """Checking the histogram to wait for tab switching completion."""
+ 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')
+
+class KeyboardEmulator(object):
+ def __init__(self, dut_ip):
+ self._dut_ip = dut_ip
+ self._process = None
+ self._key_device_name = None
+
+ def _SetupKeyDispatch(self, device_name):
+ """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'
+
+ send_key_content = '#!/bin/bash\n'
+ send_key_content += ('evemu-play --insert-slot0 ' + device_name +
+ ' < /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, self._dut_ip))
+ os.system('scp -q %s root@%s:/usr/local/tmp/' %
+ (send_key_filename, self._dut_ip))
+
+ def __enter__(self):
+ cmd = ['ssh', 'root@' + self._dut_ip, 'evemu-device',
+ '/usr/local/autotest/cros/input_playback/keyboard.prop']
cywang 2017/05/23 02:56:57 LGTM in keyboard emulation, please make sure if th
vovoy 2017/05/23 03:24:09 Done. Added checks to through exception if the pro
+ self._process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+
+ # The evemu-device output format:
+ # Emulated Keyboard: /dev/input/event10
+ output = self._process.stdout.readline()
+ self._key_device_name = output.split()[2]
+ self._SetupKeyDispatch(self._key_device_name)
+ return self
+
+ def SwitchTab(self):
+ """Sending Ctrl-tab key to trigger tab switching."""
+ RunRemoteCommand(self._dut_ip, '/usr/local/tmp/send_key_tab_switch')
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ # The remote process would live when the ssh process was terminated.
+ # Need to kill it explicitly.
+ self._process.kill()
+ RunRemoteCommand(self._dut_ip, 'pkill evemu-device')
+
+
+def NoScreenOff(dut_ip):
+ """Set screen off and dim timer to 1 hour."""
+ RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_off_delay=3600')
+ RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_dim_delay=3600')
« no previous file with comments | « tools/perf/contrib/cros_benchmarks/__init__.py ('k') | tools/perf/contrib/cros_benchmarks/data/log_key_tab_switch » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698