|
| 1 | +import { useRef } from "react"; |
| 2 | +import { useMountEffect } from "../../hooks/useMountEffect"; |
| 3 | +import { |
| 4 | + trackStudioTimelinePerformance, |
| 5 | + type StudioTimelinePerformanceSample, |
| 6 | +} from "../../telemetry/events"; |
| 7 | + |
| 8 | +const SCROLL_IDLE_MS = 400; |
| 9 | +const MIN_EVENT_INTERVAL_MS = 60_000; |
| 10 | + |
| 11 | +interface TimelinePerformanceState { |
| 12 | + frameRequest: number; |
| 13 | + idleTimer: ReturnType<typeof setTimeout> | null; |
| 14 | + pendingScrollStartedAt: number; |
| 15 | + previousFrameAt: number | null; |
| 16 | + frameLatencies: number[]; |
| 17 | + frameIntervals: number[]; |
| 18 | + lastEmittedAt: number; |
| 19 | +} |
| 20 | + |
| 21 | +interface TimelinePerformanceContext { |
| 22 | + totalClipCount: number; |
| 23 | + totalRowCount: number; |
| 24 | + zoomMode: string; |
| 25 | +} |
| 26 | + |
| 27 | +function percentile(values: readonly number[], fraction: number): number | undefined { |
| 28 | + if (values.length === 0) return undefined; |
| 29 | + const sorted = [...values].sort((a, b) => a - b); |
| 30 | + const index = Math.min(sorted.length - 1, Math.ceil(sorted.length * fraction) - 1); |
| 31 | + return Number(sorted[Math.max(0, index)].toFixed(2)); |
| 32 | +} |
| 33 | + |
| 34 | +export function summarizeTimelinePerformance( |
| 35 | + scroll: HTMLElement, |
| 36 | + context: TimelinePerformanceContext, |
| 37 | + frameLatencies: readonly number[], |
| 38 | + frameIntervals: readonly number[], |
| 39 | +): StudioTimelinePerformanceSample | null { |
| 40 | + const latencyP95 = percentile(frameLatencies, 0.95); |
| 41 | + if (latencyP95 === undefined) return null; |
| 42 | + |
| 43 | + const frameIntervalP95 = percentile(frameIntervals, 0.95); |
| 44 | + return { |
| 45 | + total_clip_count: context.totalClipCount, |
| 46 | + mounted_clip_count: scroll.querySelectorAll('[data-clip="true"]').length, |
| 47 | + total_row_count: context.totalRowCount, |
| 48 | + timeline_dom_node_count: scroll.querySelectorAll("*").length, |
| 49 | + viewport_width: scroll.clientWidth, |
| 50 | + viewport_height: scroll.clientHeight, |
| 51 | + zoom_mode: context.zoomMode, |
| 52 | + scroll_sample_count: frameLatencies.length, |
| 53 | + scroll_frame_latency_p95_ms: latencyP95, |
| 54 | + scroll_frame_latency_max_ms: Number(Math.max(...frameLatencies).toFixed(2)), |
| 55 | + ...(frameIntervalP95 === undefined ? {} : { frame_interval_p95_ms: frameIntervalP95 }), |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +function resetMeasurements(state: TimelinePerformanceState): void { |
| 60 | + state.previousFrameAt = null; |
| 61 | + state.frameLatencies = []; |
| 62 | + state.frameIntervals = []; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Samples one event per timeline scroll burst, capped at one event per minute. |
| 67 | + * The event contains only aggregate counts and timings—never project content. |
| 68 | + */ |
| 69 | +export function useTimelinePerformanceTelemetry(context: TimelinePerformanceContext): { |
| 70 | + recordTimelineScroll: (scroll: HTMLDivElement) => void; |
| 71 | +} { |
| 72 | + const stateRef = useRef<TimelinePerformanceState>({ |
| 73 | + frameRequest: 0, |
| 74 | + idleTimer: null, |
| 75 | + pendingScrollStartedAt: 0, |
| 76 | + previousFrameAt: null, |
| 77 | + frameLatencies: [], |
| 78 | + frameIntervals: [], |
| 79 | + lastEmittedAt: Number.NEGATIVE_INFINITY, |
| 80 | + }); |
| 81 | + |
| 82 | + const recordTimelineScroll = (scroll: HTMLDivElement) => { |
| 83 | + const state = stateRef.current; |
| 84 | + const now = performance.now(); |
| 85 | + |
| 86 | + if (state.frameRequest === 0) { |
| 87 | + state.pendingScrollStartedAt = now; |
| 88 | + state.frameRequest = requestAnimationFrame((frameAt) => { |
| 89 | + state.frameRequest = 0; |
| 90 | + state.frameLatencies.push(frameAt - state.pendingScrollStartedAt); |
| 91 | + if (state.previousFrameAt !== null) { |
| 92 | + state.frameIntervals.push(frameAt - state.previousFrameAt); |
| 93 | + } |
| 94 | + state.previousFrameAt = frameAt; |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + if (state.idleTimer !== null) clearTimeout(state.idleTimer); |
| 99 | + state.idleTimer = setTimeout(() => { |
| 100 | + state.idleTimer = null; |
| 101 | + if (state.frameRequest !== 0) { |
| 102 | + cancelAnimationFrame(state.frameRequest); |
| 103 | + state.frameRequest = 0; |
| 104 | + resetMeasurements(state); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const emittedAt = performance.now(); |
| 109 | + if (emittedAt - state.lastEmittedAt >= MIN_EVENT_INTERVAL_MS) { |
| 110 | + const sample = summarizeTimelinePerformance( |
| 111 | + scroll, |
| 112 | + context, |
| 113 | + state.frameLatencies, |
| 114 | + state.frameIntervals, |
| 115 | + ); |
| 116 | + if (sample) { |
| 117 | + trackStudioTimelinePerformance(sample); |
| 118 | + state.lastEmittedAt = emittedAt; |
| 119 | + } |
| 120 | + } |
| 121 | + resetMeasurements(state); |
| 122 | + }, SCROLL_IDLE_MS); |
| 123 | + }; |
| 124 | + |
| 125 | + useMountEffect(() => () => { |
| 126 | + const state = stateRef.current; |
| 127 | + if (state.frameRequest !== 0) cancelAnimationFrame(state.frameRequest); |
| 128 | + if (state.idleTimer !== null) clearTimeout(state.idleTimer); |
| 129 | + }); |
| 130 | + |
| 131 | + return { recordTimelineScroll }; |
| 132 | +} |
0 commit comments