Files
windows-fenjian/tools/_bench_scroll.py
T

143 lines
4.8 KiB
Python
Raw 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 -*-
"""UI 滚动/提交性能基准:量化各环节耗时,定位「滑动卡、提交也不流畅」
只建窗口、不连设备、不登录。测:
① 待办网格(N 张卡)拖动每帧成本
② 建卡成本(待办刷新时全量重建)
③ 摄像头预览缩放(1080p → 430x242 + PhotoImage,主线程 90ms 一次)
④ 重量事件(改多个 Label)
⑤ 网格重建 + 滚动区域重算(_layout_cards 同类工作)
用法:python tools/_bench_scroll.py [卡片数]
"""
import os
import sys
import time
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__))))
import tkinter as tk
from ui.tablet import Card, TouchScroll
from ui import tablet as T
N = int(sys.argv[1]) if len(sys.argv) > 1 else 60
FONT = 'Microsoft YaHei UI'
root = tk.Tk()
root.geometry('1440x900')
root.update_idletasks()
outer = tk.Frame(root)
outer.pack(fill='both', expand=True)
cv = tk.Canvas(outer, bg='#f2f4f7', highlightthickness=0, bd=0)
cv.pack(fill='both', expand=True)
grid = tk.Frame(cv, bg='#f2f4f7')
win = cv.create_window((0, 0), window=grid, anchor='nw')
sc = TouchScroll(cv)
sc.attach(cv)
sc.attach(grid)
def make_card(i):
c = Card(grid, radius=16, pad=12)
top = tk.Frame(c.body, bg=T.C.surface)
top.pack(fill='x')
tk.Label(top, text='商品名称示例 %d' % i, font=(FONT, 16, 'bold'),
bg=T.C.surface, fg=T.C.ink, anchor='w').pack(side='left')
th = tk.Canvas(top, width=60, height=60, bg='#dfe6ee',
highlightthickness=0)
th.pack(side='right')
tk.Label(c.body, text='规格 500g', font=(FONT, 11), bg=T.C.surface,
fg=T.C.ink3, anchor='w').pack(fill='x')
tk.Label(c.body, text='应验 %d 斤' % (i + 1), font=(FONT, 16, 'bold'),
bg=T.C.surface, fg=T.C.green, anchor='w').pack(fill='x')
tk.Label(c.body, text='客户示例名称', font=(FONT, 10), bg=T.C.surface,
fg=T.C.ink3, anchor='w').pack(fill='x')
return c
t0 = time.perf_counter()
cards = [make_card(i) for i in range(N)]
root.update_idletasks()
build_ms = (time.perf_counter() - t0) * 1000.0
cols = 6
for i, c in enumerate(cards):
c.grid(row=i // cols, column=i % cols, sticky='nsew', padx=6, pady=6)
for c in range(cols):
grid.columnconfigure(c, weight=1, uniform='b')
cv.itemconfigure(win, width=cv.winfo_width())
root.update()
cv.configure(scrollregion=cv.bbox('all'))
top_fracs = [i / 120.0 * 0.6 for i in range(120)]
t0 = time.perf_counter()
for f in top_fracs:
cv.yview_moveto(f)
root.update_idletasks()
drag_ms = (time.perf_counter() - t0) * 1000.0 / len(top_fracs)
t0 = time.perf_counter()
for f in top_fracs:
cv.yview_moveto(f)
root.update_idletasks()
drag2_ms = (time.perf_counter() - t0) * 1000.0 / len(top_fracs)
import cv2
from PIL import Image, ImageTk
frame = cv2.UMat(1080, 1920, cv2.CV_8UC3).get()
t0 = time.perf_counter()
REP = 10
for _ in range(REP):
lw, lh = 430, 242
scale = max(lw / 1920.0, lh / 1080.0)
nw, nh = max(lw, int(1920 * scale)), max(lh, int(1080 * scale))
small = cv2.resize(frame, (nw, nh), interpolation=cv2.INTER_AREA)
x0, y0 = (nw - lw) // 2, (nh - lh) // 2
small = small[y0:y0 + lh, x0:x0 + lw]
rgb = cv2.cvtColor(small, cv2.COLOR_BGR2RGB)
img = ImageTk.PhotoImage(Image.fromarray(rgb))
prev_ms = (time.perf_counter() - t0) * 1000.0 / REP
lbls = [tk.Label(root, text='0.0') for _ in range(5)]
def on_weight_like(w):
lbls[0].configure(text='%.1f' % w)
lbls[1].configure(text='%.1f 斤' % (w * 2))
lbls[2].configure(text='未去皮')
lbls[3].configure(fg='#1a7f4b')
lbls[4].configure(text='COM3 · 正常')
t0 = time.perf_counter()
for i in range(300):
on_weight_like(1.23 + i * 0.01)
lbls[0].update_idletasks()
w_ms = (time.perf_counter() - t0) * 1000.0 / 300
# ⑤ 全量重建(_fill_todo:销毁所有卡再重建 + 重新 grid + scrollregion)
t0 = time.perf_counter()
for c in cards:
c.destroy()
cards = [make_card(i) for i in range(N)]
for i, c in enumerate(cards):
c.grid(row=i // cols, column=i % cols, sticky='nsew', padx=6, pady=6)
root.update_idletasks()
cv.configure(scrollregion=cv.bbox('all'))
rebuild_ms = (time.perf_counter() - t0) * 1000.0
print('卡片数 :', N)
print('① 建 %d 张卡 : %.1f ms(每张 %.2f ms)'
% (N, build_ms, build_ms / float(N)))
print('② 拖动每帧(含 update) : %.2f ms' % drag_ms)
print('② 拖动每帧(纯 moveto) : %.2f ms' % drag2_ms)
print('③ 预览一帧缩放 : %.2f ms(90ms 一次 → 占主线程 %.0f%%)'
% (prev_ms, prev_ms / 90.0 * 100))
print('④ 重量事件一次 : %.2f ms(150ms 一次 → 占 %.0f%%)'
% (w_ms, w_ms / 150.0 * 100))
print('⑤ 全量重建(同 _fill_todo): %.1f ms' % rebuild_ms)
root.destroy()