diff options
author | Yan Wang <yawanng@google.com> | 2019-07-08 17:48:05 -0700 |
---|---|---|
committer | Yan Wang <yawanng@google.com> | 2019-07-10 16:57:19 -0700 |
commit | 3fb2816c59b8c99d1be7ba0f96560e4262dda8a1 (patch) | |
tree | 9c04a776ad88c10a2918daa3ed0e7646514e58ba /startop/scripts/app_startup/lib/adb_utils.py | |
parent | 34e2dabcf966102906f2b9e33b96bea2fa9728a4 (diff) |
startop: Rewrite metrics parsing using python.
Shell version metrics parser generates some strange results when called
from Python.
Test: pytest logcat_utils_test.py
Test: pytest adb_utils_test.py
Test: pytest run_app_with_prefetch_test.py
Change-Id: I44a464f7e87f35ecc283c5108577eb33ad394fc6
Diffstat (limited to 'startop/scripts/app_startup/lib/adb_utils.py')
-rw-r--r-- | startop/scripts/app_startup/lib/adb_utils.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/startop/scripts/app_startup/lib/adb_utils.py b/startop/scripts/app_startup/lib/adb_utils.py index 761dc2e82204..0e0065defd7f 100644 --- a/startop/scripts/app_startup/lib/adb_utils.py +++ b/startop/scripts/app_startup/lib/adb_utils.py @@ -16,13 +16,18 @@ """Helper util libraries for calling adb command line.""" +import datetime import os +import re import sys import time +from typing import Optional sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))) import lib.cmd_utils as cmd_utils +import lib.logcat_utils as logcat_utils + def logcat_save_timestamp() -> str: """Gets the current logcat timestamp. @@ -65,3 +70,38 @@ def pkill(procname: str): if pid: passed,_ = cmd_utils.run_adb_shell_command('kill {}'.format(pid)) time.sleep(1) + +def parse_time_to_milliseconds(time: str) -> int: + """Parses the time string to milliseconds.""" + # Example: +1s56ms, +56ms + regex = r'\+((?P<second>\d+?)s)?(?P<millisecond>\d+?)ms' + result = re.search(regex, time) + second = 0 + if result.group('second'): + second = int(result.group('second')) + ms = int(result.group('millisecond')) + return second * 1000 + ms + +def blocking_wait_for_logcat_displayed_time(timestamp: datetime.datetime, + package: str, + timeout: int) -> Optional[int]: + """Parses the displayed time in the logcat. + + Returns: + the displayed time. + """ + pattern = re.compile('.*ActivityTaskManager: Displayed {}.*'.format(package)) + # 2019-07-02 22:28:34.469453349 -> 2019-07-02 22:28:34.469453 + timestamp = datetime.datetime.strptime(timestamp[:-3], + '%Y-%m-%d %H:%M:%S.%f') + timeout_dt = timestamp + datetime.timedelta(0, timeout) + # 2019-07-01 14:54:21.946 27365 27392 I ActivityTaskManager: + # Displayed com.android.settings/.Settings: +927ms + result = logcat_utils.blocking_wait_for_logcat_pattern(timestamp, + pattern, + timeout_dt) + if not result or not '+' in result: + return None + displayed_time = result[result.rfind('+'):] + + return parse_time_to_milliseconds(displayed_time)
\ No newline at end of file |