|
86 | 86 | } |
87 | 87 | : noop |
88 | 88 |
|
| 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 | + |
89 | 126 | // ----- 底层 helper ----- |
90 | 127 |
|
91 | 128 | const trimList = (list, max) => { |
|
374 | 411 | pendingMutationFrame = 0 |
375 | 412 | } |
376 | 413 | pendingMutationNodes = [] |
| 414 | + if (perfDumpTimer) { |
| 415 | + window.clearInterval(perfDumpTimer) |
| 416 | + perfDumpTimer = 0 |
| 417 | + } |
377 | 418 | if (navigationInterval) { |
378 | 419 | window.clearInterval(navigationInterval) |
379 | 420 | navigationInterval = 0 |
|
577 | 618 | installMutationObserver() |
578 | 619 | installNavigationObserver() |
579 | 620 | 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 | + } |
580 | 625 | } catch (error) { |
581 | 626 | if (!isExtensionContextInvalidated(error)) { |
582 | 627 | throw error |
|
0 commit comments