Files
image-workbench-desktop/src/runtime-frame-probe.js
T
qiqi 3707eb6f2c
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 39s
feat(perf): 性能基线纳入 App 帧探针断言(C 项)
背景:之前的 _perf-baseline.spec.js 只用页内自建的 __FRAMES__ 探针测,与 App 运行时真正
输出的 perf 行(IWBPerf,长帧 >50ms + longtask)不是同一套数据,用户日志里的数值无法回归。

- src/runtime-frame-probe.js:新增只读 snapshot()(结构化读数 + 清零),与 60s 落盘窗口解耦,
  _boot.log 的 perf 行格式不变。
- tests/_perf-baseline.spec.js:平移段前后取快照,新增 3 条断言
  · frames > 30(防「探针没启动 → 0 长帧」假绿)
  · 长帧比例 ≤ 40%
  · longtask 总时长 ≤ 2500ms
  快照同时写入 P1_BASELINE= 便于历史对比;IWB_PROBE_PERF=0 可只测不断言。
- scripts/perf-baseline.js + npm run perf:baseline:build → 动态端口起本地服 → 只跑该 spec → 关服。
- 本机实测(headless / 7000 节点 / 12 步平移,三次):长帧 13~17/57~59(22.8%~30.9%)、
  longtask 25 次 1905~1934ms、最长帧 ~400ms;阈值取实测最大值 ×1.3,换机器需重设。
- 文档:performance-lessons 增加「性能回归怎么跑」;计划 C 项标记完成。

仍不进 CI(无头环境无 GPU,数字不可比),只作真机回归。
2026-12-02 09:41:32 +08:00

126 lines
5.8 KiB
JavaScript
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.
/* frame-probe.js —— 帧时长探针(2026-11-29)
*
* 目的:真机上定位「哪一下卡」。代码侧已经确认平移/拖拽/滚轮都只改 DOM,
* 浏览器无头环境也测不出卡顿(合成画布 120 节点仍是 60fps),所以必须回到真机采数据。
*
* 做法:requestAnimationFrame 连续测帧间隔,只统计 >50ms 的长帧;
* 每分钟最多写一条汇总到 _boot.log(没有长帧就不写,闲置零噪声):
* perf 60s frames=3600 long=4 max=180ms tags=zoom:3,save:1 domImgs=94
*
* 标签由各热路径主动打点:zoom(缩放提交)/ board(切画板)/ save(保存)/ preview(预览)/ thumb(生成缩略图)。
* 窗口不可见时 rAF 自动暂停,不产生开销。
*/
;(function (global) {
'use strict'
const LONG_MS = 50
const TAG_TTL = 900
// 测试可覆写:window.__IWB_PERF_WINDOW_MS__(默认 60s 一条汇总)
const WINDOW_MS = Number(global.__IWB_PERF_WINDOW_MS__) > 0 ? Number(global.__IWB_PERF_WINDOW_MS__) : 60000
let tagName = ''
let tagAt = 0
let raf = 0
let last = 0
let bucket = null
const recent = [] // 最近 20 条,便于诊断页/自动化测试直接读(不影响落盘)
const logLine = (line) => {
try {
recent.push(line)
if (recent.length > 20) recent.shift()
const S = global.IWBStorageFile
if (S && typeof S.logLine === 'function') { S.logLine(line); return }
if (global.console && global.console.warn) global.console.warn('[iwb] ' + line)
} catch (e) { /* ignore */ }
}
const blank = () => ({ at: Date.now(), n: 0, long: 0, max: 0, tags: Object.create(null), js: 0, jsMs: 0 })
// 累计器:给自动化测试用的结构化读数。与 60s 落盘窗口相互独立,
// 所以「取一次 snapshot」既不会打断 _boot.log 的汇总节奏,也不会被窗口边界截断。
const total = Object.assign(blank(), { since: 0 })
function tag (name) { tagName = String(name || ''); tagAt = Date.now() }
function flush (now) {
if (!bucket || now - bucket.at < WINDOW_MS) return
{
const tags = Object.keys(bucket.tags).map((k) => k + ':' + bucket.tags[k]).join(',')
const imgs = (global.document && global.document.images) ? global.document.images.length : -1
logLine('perf ' + Math.round((now - bucket.at) / 1000) + 's frames=' + bucket.n + ' long=' + bucket.long +
' max=' + Math.round(bucket.max) + 'ms js=' + bucket.js + '/' + Math.round(bucket.jsMs) + 'ms tags=' + (tags || '-') + ' domImgs=' + imgs)
}
bucket = blank()
}
function tick (t) {
raf = global.requestAnimationFrame(tick)
const dt = t - last
last = t
// 首帧、标签页休眠恢复、窗口最小化回来时 dt 会非常大,不算进统计
if (!(dt > 0) || dt > 3000) return
bucket.n++
total.n++
if (!total.since) total.since = Date.now()
if (dt > LONG_MS) {
bucket.long++
if (dt > bucket.max) bucket.max = dt
const name = (Date.now() - tagAt < TAG_TTL) ? tagName : 'idle'
bucket.tags[name] = (bucket.tags[name] || 0) + 1
total.long++
if (dt > total.max) total.max = dt
total.tags[name] = (total.tags[name] || 0) + 1
}
flush(Date.now())
}
function start () {
if (raf || typeof global.requestAnimationFrame !== 'function') return
bucket = blank()
total.n = 0; total.long = 0; total.max = 0; total.tags = Object.create(null); total.js = 0; total.jsMs = 0; total.since = 0
last = global.performance && global.performance.now ? global.performance.now() : Date.now()
raf = global.requestAnimationFrame(tick)
watchLongTasks()
}
let jsObserver = null
function watchLongTasks () {
if (jsObserver || typeof global.PerformanceObserver !== 'function') return
try {
jsObserver = new global.PerformanceObserver(function (list) {
for (const e of list.getEntries()) { total.js++; total.jsMs += Number(e.duration) || 0; if (!bucket) continue; bucket.js++; bucket.jsMs += Number(e.duration) || 0 }
})
jsObserver.observe({ entryTypes: ['longtask'] })
} catch (e) { jsObserver = null }
}
function stop () {
if (raf) { try { global.cancelAnimationFrame(raf) } catch (e) { /* ignore */ } }
raf = 0
bucket = null
try { if (jsObserver) jsObserver.disconnect() } catch (e) { /* ignore */ }
jsObserver = null
}
// snapshot():返回「自 start() 或上次 snapshot() 以来」的累计读数并清零(结构化,供自动化断言)。
// 字段:frames(帧数)/ long(>50ms 长帧数)/ max(最长帧 ms)/ js、jsMs(longtask 次数与总时长)/ tags(长帧归属)/ since(本段起点)
function snapshot () {
const out = {
frames: total.n,
long: total.long,
max: Math.round(total.max),
js: total.js,
jsMs: Math.round(total.jsMs),
tags: Object.assign({}, total.tags),
since: total.since || 0,
}
total.n = 0; total.long = 0; total.max = 0; total.tags = Object.create(null); total.js = 0; total.jsMs = 0; total.since = 0
return out
}
global.IWBPerf = { tag: tag, start: start, stop: stop, snapshot: snapshot, recent: recent, running: () => !!raf }
// 默认不启动:正式包(对外)不能往 _boot.log 写性能采样。
// 只有构建时 IWB_PERF_PROBE=1 注入 window.__IWB_PERF_PROBE__,
// 或显式设置 localStorage.iwb_perf_probe='1' 时才开启(排查用)。
let lsOn = false
try { const S = global.IWBStorageLocal; const st = (S && typeof S.getStore === 'function') ? S.getStore() : null; lsOn = !!(st && String(st.get('iwb_perf_probe', '')).replace(/"/g, '') === '1') } catch (e) { lsOn = false }
const wantProbe = global.__IWB_PERF_PROBE__ === 1 || global.__IWB_PERF_PROBE__ === true || lsOn
if (wantProbe && typeof global.requestAnimationFrame === 'function') start()
})(typeof window !== 'undefined' ? window : globalThis)