Files

170 lines
5.5 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.56 诊断:名单滚动到底慢在哪一层
现场反馈「滑动商品的时候不流畅」。可能的位置有四个,逐个量:
A. _select_card —— 手指按下就触发,遍历所有卡片改高亮
B. _layout_cards —— 重排网格 + 重算 scrollregion
C. yview_moveto —— Canvas 滚动本身(内嵌 Frame 要整块重绘)
D. _wheel —— 每次滚轮只走 3 units,太慢会被当成「卡」
用法:python tools/_diag_v1056.py [行数]
"""
import json
import os
import sys
import time
import tkinter as tk
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
sys.path.insert(0, ROOT)
os.chdir(ROOT)
from ui import app as A # noqa: E402
A.App._poll = lambda self: None
A.App._poll_preview = lambda self: None
N = int(sys.argv[1]) if len(sys.argv) > 1 else 120
def rows(n):
out = []
names = ['大葱', '小葱', '油麦菜', '青条椒', '白萝卜', '土豆', '火龙果',
'蜜薯', '香菇', '小木耳', '软江叶', '藕', '青笋', '山药', '玉米']
for i in range(n):
out.append({
'id': 'R%04d' % i,
'item_name': '%s%d' % (names[i % len(names)], i),
'unit': '斤',
'required_qty': 10.0 + i,
'remaining_qty': 10.0 + i,
'spec': '20斤/件',
'customer_name': '南充市高坪区红运水果蔬菜批发部',
'order_code': 'ORD_WEB_20261105_BR002_%04d' % i,
})
return out
BASE = json.load(open('config.json', encoding='utf-8'))
BASE.setdefault('ui', {})
BASE['ui'].update({'mode': 'window', 'fit_screen': False,
'borderless': False, 'todo_thumb': False,
'width': 1440, 'height': 900})
A.load_config = lambda path='config.json', _c=BASE: _c
root = tk.Tk()
app = A.App(root)
root.geometry('%dx%d+0+0' % (1440, 900))
for c in root.winfo_children():
c.destroy()
app._build()
root.update_idletasks()
root.update()
rws = rows(N)
t0 = time.perf_counter()
app._fill_todo(rws)
root.update_idletasks()
root.update()
t_fill = (time.perf_counter() - t0) * 1000
print('配置:%d 行,卡片 %d 张' % (N, len(app.todo_cards)))
print('首建 _fill_todo %7.1f ms' % t_fill)
print('内容高度 %7.0f px' % (app.todo_canvas.bbox('all')[3],))
print('视口高度 %7.0f px' % app.todo_canvas.winfo_height())
# A. _select_card(模拟按下第 60 张卡)
rids = list(app.todo_cards.keys())
tgt = rids[len(rids) // 2]
t0 = time.perf_counter()
for _ in range(10):
app._select_card(tgt)
root.update_idletasks()
dt = (time.perf_counter() - t0) / 10 * 1000
print('A _select_card %7.1f ms' % dt)
# A2. 按下 + 一次完整重绘(现场真实感受)
t0 = time.perf_counter()
for _ in range(5):
app._select_card(rids[5])
app._select_card(rids[6])
root.update()
dt = (time.perf_counter() - t0) / 10 * 1000
print('A2 选中+整屏重绘 %7.1f ms' % dt)
# B. _layout_cards
t0 = time.perf_counter()
for _ in range(3):
app._layout_cards()
root.update_idletasks()
dt = (time.perf_counter() - t0) / 3 * 1000
print('B _layout_cards %7.1f ms' % dt)
# C. 滚动:模拟拖动时每帧一次 yview_moveto
cv = app.todo_canvas
total = max(1.0, cv.bbox('all')[3])
t0 = time.perf_counter()
for i in range(20):
cv.yview_moveto(min(0.9, i / 40.0))
root.update()
dt = (time.perf_counter() - t0) / 20 * 1000
print('C 滚动一帧(moveto+update) %7.1f ms → 上限 %.0f fps'
% (dt, 1000.0 / max(dt, 0.01)))
# C2. 只 moveto 不 update(TouchScroll 的真实行为:update 由 Tk 事件循环做)
t0 = time.perf_counter()
for i in range(20):
cv.yview_moveto(min(0.9, i / 40.0))
root.update_idletasks()
dt = (time.perf_counter() - t0) / 20 * 1000
print('C2 仅 moveto(实测会排队) %7.1f ms' % dt)
root.update()
# D. 滚轮一格走多少像素
t0 = time.perf_counter()
before = cv.yview()[0]
class E:
delta = -120
app._todo_scroll._wheel(E())
root.update()
after = cv.yview()[0]
print('D 滚轮一格 %7.1f px(%.1f%% 内容)'
% ((after - before) * total, (after - before) * 100))
# E. 一次 focus 全流程(日志里 ev:focus 438ms 就是它)
shot = os.path.join(ROOT, 'data', '_diag', 'shots', 'a.jpg')
r0 = rws[3]
payload = {'mode': 'weigh', 'row_id': str(r0['id']), 'row': r0,
'detected': '大葱', 'conf': 92, 'weight': 1.78,
'business': 3.56, 'unit': '斤', 'qty_editable': False,
'from_scale': False, 'candidates': [r0['id']], 'warn': '',
'snapshot': shot if os.path.exists(shot) else None,
'auto': True, 'rows': [], 'station': 'picking'}
t0 = time.perf_counter()
for i in range(5):
payload['row_id'] = str(rws[i]['id'])
app._on_focus(payload)
root.update()
dt = (time.perf_counter() - t0) / 5 * 1000
print('E focus 全流程(含跳转) %7.1f ms' % dt)
# E2. 再点一次同样的行(最坏情况:滚动 + 图 + 高亮都要重做)
t0 = time.perf_counter()
for i in range(5):
app._on_focus(payload)
root.update()
dt = (time.perf_counter() - t0) / 5 * 1000
print('E2 同一行重复 focus %7.1f ms' % dt)
# E3. v1.0.61 起确认条已撤掉,只量「跳转到那一行」这段(含翻页)
t0 = time.perf_counter()
app._reveal_row(str(payload['row_id']))
root.update()
t4 = time.perf_counter()
print('E3 跳转到该行+翻页 %7.1f ms' % ((t4 - t0) * 1000))
print('\n拖动限帧:TouchScroll 现在 0.016s/帧 = 上限 62fps')
print('若单帧 >16ms,拖动时每帧都在排队 → 手感「顿」')
root.destroy()