diff --git a/gui/components/realtime_viewer.py b/gui/components/realtime_viewer.py index 5f6c9da..f6eadc5 100644 --- a/gui/components/realtime_viewer.py +++ b/gui/components/realtime_viewer.py @@ -154,20 +154,35 @@ def _tail_read(path: str, max_bytes: int = 64 * 1024) -> list[str]: def _parse_minimal(line: str) -> Optional[tuple[str, str]]: - """从一行日志中提取 (动作名, 耗时) 二元组;不含"耗时"则返回 None。""" + """从一行日志中提取 (动作名, 耗时) 二元组;不含"耗时"则返回 None。 + + 支持 ms / s 两种单位(实际 actions.log 写 `耗时=10.00ms`,旧测试桩用 `2.5s`)。 + """ if "耗时" not in line: return None - m = re.search(r"耗时[:=]?\s*([0-9]+(?:\.[0-9]+)?)\s*s", line) - dur = None - if m: - dur = m.group(1) + "s" - else: - m2 = re.search(r"([0-9]+(?:\.[0-9]+)?)\s*s", line) - if m2: - dur = m2.group(1) + "s" - if not dur: - return None idx = line.find("耗时") + tail = line[idx:] # 在"耗时"之后搜,避免把前缀里的数字当成耗时 + + # 优先 ms(必须先于 s 匹配,否则 \s*s 会先吃掉 "10ms" 的 m) + m_ms = re.search(r"耗时[:=]?\s*([0-9]+(?:\.[0-9]+)?)\s*ms", tail) + m_s = re.search(r"耗时[:=]?\s*([0-9]+(?:\.[0-9]+)?)\s*s(?![a-zA-Z])", tail) + + if m_ms: + dur = m_ms.group(1) + "ms" + elif m_s: + dur = m_s.group(1) + "s" + else: + # 兜底:在"耗时"后任意位置找 数字+ms 或 数字+s + m2 = re.search(r"([0-9]+(?:\.[0-9]+)?)\s*ms", tail) + if m2: + dur = m2.group(1) + "ms" + else: + m3 = re.search(r"([0-9]+(?:\.[0-9]+)?)\s*s(?![a-zA-Z])", tail) + if m3: + dur = m3.group(1) + "s" + else: + return None + action = line[:idx].strip() if not action: action = line.strip()[:80] diff --git a/gui/gui_app.py b/gui/gui_app.py index 2486bfd..7d408c6 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -3683,28 +3683,36 @@ def main(): return [] def parse_action_duration(line: str) -> tuple[str, str] | None: - """从一行日志提取 (action, duration),未找到返回 None。""" + """从一行日志提取 (action, duration),未找到返回 None。 + + 支持 ms / s 两种单位(实际 actions.log 写 `耗时=10.00ms`)。 + """ if "耗时" not in line: return None - # 尝试提取耗时数字 - m = re.search( - r"耗时[:=]?\s*([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*s", line - ) - dur = None - if m: - dur = m.group(1) + "s" - else: - m2 = re.search( - r"([0-9]+(?:[.\u00B7\uFF0E\uFF61·.。][0-9]+)?)\s*s", line - ) - if m2: - dur = m2.group(1) + "s" + idx = line.find("耗时") + tail = line[idx:] # 在"耗时"之后搜,避免把前缀里的数字当成耗时 - if not dur: - return None + num = r"([0-9]+(?:\.[0-9]+)?)" + # 优先 ms(必须先于 s 匹配,否则 \s*s 会先吃掉 "10ms" 的 m) + m_ms = re.search(r"耗时[:=]?\s*" + num + r"\s*ms", tail) + m_s = re.search(r"耗时[:=]?\s*" + num + r"\s*s(?![a-zA-Z])", tail) + + if m_ms: + dur = m_ms.group(1) + "ms" + elif m_s: + dur = m_s.group(1) + "s" + else: + m2 = re.search(num + r"\s*ms", tail) + if m2: + dur = m2.group(1) + "ms" + else: + m3 = re.search(num + r"\s*s(?![a-zA-Z])", tail) + if m3: + dur = m3.group(1) + "s" + else: + return None # 动作名取耗时之前的文本片段,最多保留 60 字符 - idx = line.find("耗时") action = line[:idx].strip() if not action: # 兜底:取整行前 60 字符 diff --git a/tests/test_realtime_viewer.py b/tests/test_realtime_viewer.py index 30ce510..cdc5dfc 100644 --- a/tests/test_realtime_viewer.py +++ b/tests/test_realtime_viewer.py @@ -96,3 +96,30 @@ class TestParseMinimal: def test_integer_duration(self): result = _parse_minimal("步骤A 耗时:5s") assert result == ("步骤A", "5s") + + def test_ms_unit(self): + """实际 actions.log 写的是 `耗时=10.00ms`,必须支持 ms 单位。""" + result = _parse_minimal( + "2026-05-21 14:59:00,513 - [ACTION] test step 耗时=10.00ms" + ) + assert result is not None + action, dur = result + assert dur == "10.00ms" + assert "test step" in action + + def test_ms_integer(self): + result = _parse_minimal("OCR识别 耗时=100ms") + assert result == ("OCR识别", "100ms") + + def test_ms_takes_priority_over_s(self): + """`10ms` 不能被错误识别为 `10` + `s` 的前半部分。""" + result = _parse_minimal("click 耗时=10ms") + assert result == ("click", "10ms") + + def test_number_before_耗时_not_picked_up(self): + """前缀里的 `5555` / `192.168...` 不能被当成耗时。""" + result = _parse_minimal( + "192.168.5.2:5555 [ACTION] click 耗时=10ms" + ) + assert result is not None + assert result[1] == "10ms"