Files
image-workbench-desktop/src/runtime-frame-probe.js
T
qiqi e2b976413c
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 2m5s
feat: v3.26.173 帧探针可开关(rAF 帧时长 + JS 长任务写 _boot.log)
2026-12-01 20:01:31 +08:00

101 lines
4.5 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 })
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++
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
}
flush(Date.now())
}
function start () {
if (raf || typeof global.requestAnimationFrame !== 'function') return
bucket = blank()
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()) { 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
}
global.IWBPerf = { tag: tag, start: start, stop: stop, 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)