Files

170 lines
5.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""v1.0.47 回归:卡顿治理
现场反馈:「流程没问题,就是卡」「识别后滑动很卡」「提交也不流畅」。
两个根因(实测):
① 待办轮询在 tick 线程里同步发 HTTP:服务端读超时 10 秒 → 每 2 秒
卡 10 秒,秤读数/拿走检测/提交全被拖住。
② 待办刷新全量重建卡片:120 行要 ~1 秒(每张卡 8 个控件),
提交一单待办就变一次 → 界面反复冻。拖动每帧 30ms。
本文件验证:
A. tick 不再等网络:pending 卡住时 _tick 仍然毫秒级返回
B. 结果从后台队列应用(行变了才发 pending 事件)
C. 失败日志 5 分钟一条(不刷屏)
D. UI:同一批行只改文字、卡片对象不重建;行数减少只销毁那几张
"""
import os
import sys
import time
import types
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
ok = fail = 0
def check(name, cond, extra=''):
global ok, fail
if cond:
ok += 1
print(' ✓', name)
else:
fail += 1
print(' ✗', name, extra)
print('A. 待办拉取不再阻塞 tick')
from core.flow import FlowEngine
CFG = {'flow': {'min_scale_kg': 0.05, 'stable_ms': 400, 'settle_ms': 650,
'stable_delta_kg': 0.03},
'scale': {'simulate': True}, 'camera': {'simulate': True},
'server': {'mock': True}, 'vision': {}, 'noweigh': {}}
EV = []
eng = FlowEngine(CFG, lambda e, p=None: EV.append((e, p)))
eng.logged_in = True
eng._running = True
eng.state = 'IDLE'
class SlowApi:
"""pending 卡 2 秒(模拟现场服务端读超时)"""
def __init__(self):
self.calls = 0
def pending(self, station, timeout=None):
self.calls += 1
time.sleep(2.0)
return {'rows': [{'id': 'a', 'item_name': '小葱', 'unit': '斤',
'required_qty': 3.0}],
'config': {}, 'account_name': 't'}
eng.api = SlowApi()
eng._last_poll_at = 0.0
t0 = time.perf_counter()
eng._tick()
dt = (time.perf_counter() - t0) * 1000.0
check('pending 卡 2 秒时 _tick 仍 <100ms(实际 %.0fms)' % dt, dt < 100,
'tick 被网络拖住了')
check('后台已在拉(calls=1)', eng.api.calls == 1)
# 在途期间不重复发起
eng._last_poll_at = 0.0
eng._tick()
check('在途不重复发起', eng.api.calls == 1)
print('B. 结果从队列应用')
t0 = time.time()
while time.time() - t0 < 3.0 and not any(e == 'pending' for e, _ in EV):
eng._tick()
time.sleep(0.05)
check('pending 事件到达', any(e == 'pending' for e, _ in EV))
check('行已应用', [r['id'] for r in eng.rows] == ['a'])
before = len(EV)
eng._drain_pending() # 队列已空,不应重复发事件
check('队列空时不重复发事件', len(EV) == before)
print('C. 失败日志节流')
class FailApi:
def pending(self, station, timeout=None):
raise RuntimeError('读超时')
eng.api = FailApi()
for _ in range(5):
eng.refresh_pending_async()
t0 = time.time()
while time.time() - t0 < 0.5:
eng._tick()
time.sleep(0.02)
logs = [str(p) for e, p in EV if e == 'log' and '待办拉取失败' in str(p)]
check('5 次失败只记 1 条日志', len(logs) == 1, '实际 %d 条' % len(logs))
print('D. UI:卡片增量更新 / 局部销毁')
import json as _json
import tkinter as tk
import ui.app as A
cfg = _json.load(open('config.json', encoding='utf-8'))
cfg.setdefault('server', {})['mock'] = True
cfg.setdefault('scale', {})['simulate'] = True
cfg.setdefault('camera', {})['simulate'] = True
cfg.setdefault('ui', {})['mode'] = 'window'
cfg['ui']['todo_group'] = 'none'
A.load_config = lambda: cfg
root = tk.Tk()
app = A.App(root)
app._build()
app.ready = True
root.update()
def rows_n(n, qty=3.0):
return [{'id': 'r%d' % i, 'item_name': '商品%d' % i, 'spec': '500g',
'unit': '斤', 'required_qty': qty,
'customer_name': '客户%d' % (i % 3), 'sku_code': 'S%d' % i}
for i in range(n)]
app._fill_todo(rows_n(40))
t0 = time.time()
while time.time() - t0 < 2.0 and len(app.todo_cards) < 40:
root.update()
time.sleep(0.02)
cards_before = {k: id(v) for k, v in app.todo_cards.items()}
check('40 行已铺满', len(app.todo_cards) == 40)
# 数量变化:应当只改文字,不重建
t0 = time.perf_counter()
app._fill_todo(rows_n(40, qty=9.0))
dt = (time.perf_counter() - t0) * 1000.0
cards_after = {k: id(v) for k, v in app.todo_cards.items()}
check('数量变化不重建卡片', cards_before == cards_after)
check('增量更新耗时 <80ms(实际 %.0fms)' % dt, dt < 80)
check('数量文字已更新',
'9' in app.todo_cards['r0']._qty_lbl.cget('text'))
# 少一行:只销毁那一张,其余不动
t0 = time.perf_counter()
app._fill_todo(rows_n(40)[1:])
dt = (time.perf_counter() - t0) * 1000.0
check('删行后总数 39', len(app.todo_cards) == 39)
check('r0 卡被销毁', 'r0' not in app.todo_cards)
check('r1 卡对象未重建', id(app.todo_cards['r1']) == cards_before['r1'])
check('删行耗时 <150ms(实际 %.0fms)' % dt, dt < 150)
print()
print('通过 %d 项,失败 %d 项' % (ok, fail))
try:
app.engine.stop()
except Exception:
pass
root.destroy()
sys.exit(1 if fail else 0)