Skip to content

Commit c52bba4

Browse files
committed
chore: 动态监控埋点改周期 console 输出
性能埋点 entries 写入扩展隔离 world 的 Performance buffer,主页面 console 直接查询拿不到、需切换 execution context。改为开启 debug 后每 3 秒在页面 console 自动输出各热点的 count/avg/max/lastDetail,无需切上下文。将版本号提升到 1.2.81。
1 parent 0d2c2a5 commit c52bba4

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "stackprism",
33
"private": true,
4-
"version": "1.2.80",
4+
"version": "1.2.81",
55
"type": "module",
66
"description": "StackPrism 用于检测网页前端、后端、CDN、SaaS、广告营销、统计、登录、支付、网站程序和主题模板线索。",
77
"scripts": {

src/content/content-observer.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,43 @@
8686
}
8787
: noop
8888

89+
const PERF_DUMP_INTERVAL_MS = 3000
90+
const PERF_MEASURE_NAMES = ['sp:mutation-callback', 'sp:mutation-flush', 'sp:perf-observer', 'sp:send-snapshot']
91+
let perfDumpTimer = 0
92+
93+
const dumpPerfSnapshot = () => {
94+
const summary = {}
95+
let hasAny = false
96+
for (const name of PERF_MEASURE_NAMES) {
97+
const entries = performance.getEntriesByName(name)
98+
if (!entries.length) continue
99+
hasAny = true
100+
let total = 0
101+
let max = 0
102+
let lastDetail = null
103+
for (const entry of entries) {
104+
total += entry.duration
105+
if (entry.duration > max) max = entry.duration
106+
if (entry.detail !== undefined) lastDetail = entry.detail
107+
}
108+
summary[name] = {
109+
count: entries.length,
110+
totalMs: Number(total.toFixed(1)),
111+
avgMs: Number((total / entries.length).toFixed(2)),
112+
maxMs: Number(max.toFixed(1)),
113+
lastDetail
114+
}
115+
try {
116+
performance.clearMeasures(name)
117+
} catch {
118+
// ignore
119+
}
120+
}
121+
if (hasAny) {
122+
console.log('[StackPrism observer]', new Date().toISOString().slice(11, 19), summary)
123+
}
124+
}
125+
89126
// ----- 底层 helper -----
90127

91128
const trimList = (list, max) => {
@@ -374,6 +411,10 @@
374411
pendingMutationFrame = 0
375412
}
376413
pendingMutationNodes = []
414+
if (perfDumpTimer) {
415+
window.clearInterval(perfDumpTimer)
416+
perfDumpTimer = 0
417+
}
377418
if (navigationInterval) {
378419
window.clearInterval(navigationInterval)
379420
navigationInterval = 0
@@ -577,6 +618,10 @@
577618
installMutationObserver()
578619
installNavigationObserver()
579620
scheduleSend()
621+
if (PERF_DEBUG) {
622+
console.log('[StackPrism observer] 性能埋点已启用,每 ' + PERF_DUMP_INTERVAL_MS + 'ms 输出一次摘要')
623+
perfDumpTimer = window.setInterval(dumpPerfSnapshot, PERF_DUMP_INTERVAL_MS)
624+
}
580625
} catch (error) {
581626
if (!isExtensionContextInvalidated(error)) {
582627
throw error

0 commit comments

Comments
 (0)