Line data Source code
1 : /**
2 : * Thin performance instrumentation primitives.
3 : *
4 : * The browser Performance API records the marks and measures for local
5 : * inspection. Callers retain the returned measurements for structured reports.
6 : * Sentry breadcrumbs are a secondary consumer so a startup error carries the
7 : * phase timings that preceded it.
8 : */
9 : import { addBreadcrumb } from '@sentry/vue'
10 :
11 : export interface PerfSpan {
12 : /** Wall-clock ms since navigationStart when mark() was called. */
13 : startMs: number
14 : /** Call stop() to end the span and record the measure. */
15 : stop(): number
16 : }
17 :
18 2356 : const NOOP_PERF_SPAN: PerfSpan = {
19 : startMs: 0,
20 : stop: () => 0
21 : }
22 :
23 : /**
24 : * Begin a named performance span.
25 : *
26 : * Records a `performance.mark('<name>:start')`. Calling `stop()` records
27 : * `performance.mark('<name>:end')` + `performance.measure('<name>', ...)`,
28 : * adds a Sentry breadcrumb, and returns the elapsed milliseconds. Repeat
29 : * `stop()` calls return the first measured duration without re-recording.
30 : *
31 : * @example
32 : * const span = perfMark('bootstrap/object-info')
33 : * await this.getNodeDefs()
34 : * const ms = span.stop()
35 : */
36 51810 : export function perfMark(name: string): PerfSpan {
37 51810 : const startName = `${name}:start`
38 51810 : const endName = `${name}:end`
39 :
40 51810 : try {
41 51810 : performance.mark(startName)
42 51810 : const startMs = performance.now()
43 51810 : let durationMs: number | undefined
44 :
45 51810 : return {
46 : startMs,
47 : stop(): number {
48 51810 : if (durationMs !== undefined) return durationMs
49 51810 : try {
50 51810 : performance.mark(endName)
51 51810 : durationMs = _measure(name, startName, endName, startMs)
52 51810 : _emitToSentry(name, durationMs)
53 51810 : return durationMs
54 : } catch {
55 0 : durationMs = 0
56 0 : return 0
57 : }
58 : }
59 : }
60 : } catch {
61 0 : return NOOP_PERF_SPAN
62 : }
63 : }
64 :
65 : /**
66 : * Mark a single point-in-time milestone (no duration).
67 : */
68 4710 : export function perfPoint(name: string): void {
69 4710 : try {
70 4710 : performance.mark(name)
71 4710 : _emitToSentry(name, 0)
72 : } catch {
73 0 : return
74 : }
75 : }
76 :
77 : /**
78 : * Measure from the navigation time origin to now and return the elapsed ms.
79 : */
80 2355 : export function perfMeasureSinceOrigin(name: string): number {
81 2355 : const durationMs = performance.now()
82 2355 : try {
83 2355 : performance.measure(name, { start: 0, end: durationMs })
84 : } catch {
85 0 : return durationMs
86 : }
87 2355 : _emitToSentry(name, durationMs)
88 2355 : return durationMs
89 : }
90 :
91 51810 : function _measure(
92 : name: string,
93 : startName: string,
94 : endName: string,
95 : fallbackStart: number
96 : ): number {
97 51810 : try {
98 51810 : return performance.measure(name, startName, endName).duration
99 : } catch {
100 0 : return performance.now() - fallbackStart
101 : }
102 : }
103 :
104 58875 : function _emitToSentry(name: string, durationMs: number): void {
105 58875 : try {
106 : // addBreadcrumb is a module-level function in @sentry/vue v10 — safe to
107 : // call before Sentry.init() completes (it queues internally).
108 58875 : addBreadcrumb({
109 : category: 'perf',
110 : message: name,
111 : level: 'info',
112 : data: { duration_ms: durationMs }
113 : })
114 : } catch {
115 0 : return
116 : }
117 : }
|