|
| 1 | +import { runtimeEnv } from "./runtime-env" |
| 2 | + |
| 3 | +type PerfDetail = Record<string, unknown> | undefined |
| 4 | + |
| 5 | +export interface PerfTraceEntry { |
| 6 | + name: string |
| 7 | + time: number |
| 8 | + absoluteTime: number |
| 9 | + host: string |
| 10 | + platform: string |
| 11 | + path: string |
| 12 | + detail?: PerfDetail |
| 13 | +} |
| 14 | + |
| 15 | +interface PerfSummary { |
| 16 | + runtimeHost: string |
| 17 | + runtimePlatform: string |
| 18 | + loaderToCliReadyMs?: number |
| 19 | + loaderToNavigateMs?: number |
| 20 | + bootstrapToAppMountMs?: number |
| 21 | + bootstrapToFirstFrameMs?: number |
| 22 | + bootstrapToFirstContentMs?: number |
| 23 | +} |
| 24 | + |
| 25 | +const TRACE_STORAGE_KEY = "codenomad:perf-trace" |
| 26 | +const WINDOW_NAME_PREFIX = "__CODENOMAD_PERF__:" |
| 27 | +const TRACE_RETENTION_LIMIT = 200 |
| 28 | +const TRACE_STALE_AFTER_MS = 10 * 60 * 1000 |
| 29 | + |
| 30 | +let inMemoryTrace: PerfTraceEntry[] | null = null |
| 31 | + |
| 32 | +function nowMs() { |
| 33 | + return typeof performance !== "undefined" ? performance.now() : Date.now() |
| 34 | +} |
| 35 | + |
| 36 | +function absoluteNowMs() { |
| 37 | + if (typeof performance !== "undefined" && typeof performance.timeOrigin === "number") { |
| 38 | + return performance.timeOrigin + performance.now() |
| 39 | + } |
| 40 | + return Date.now() |
| 41 | +} |
| 42 | + |
| 43 | +function getCurrentPath() { |
| 44 | + if (typeof window === "undefined" || !window.location) { |
| 45 | + return "" |
| 46 | + } |
| 47 | + return `${window.location.pathname}${window.location.search}${window.location.hash}` |
| 48 | +} |
| 49 | + |
| 50 | +function supportsSessionStorage() { |
| 51 | + return typeof window !== "undefined" && typeof window.sessionStorage !== "undefined" |
| 52 | +} |
| 53 | + |
| 54 | +function loadTraceFromWindowName(): PerfTraceEntry[] { |
| 55 | + if (typeof window === "undefined" || !window.name.startsWith(WINDOW_NAME_PREFIX)) { |
| 56 | + return [] |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const parsed = JSON.parse(window.name.slice(WINDOW_NAME_PREFIX.length)) |
| 61 | + return Array.isArray(parsed) ? (parsed as PerfTraceEntry[]) : [] |
| 62 | + } catch { |
| 63 | + return [] |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function persistTrace(trace: PerfTraceEntry[]) { |
| 68 | + const trimmed = trace.slice(-TRACE_RETENTION_LIMIT) |
| 69 | + inMemoryTrace = trimmed |
| 70 | + |
| 71 | + if (supportsSessionStorage()) { |
| 72 | + try { |
| 73 | + window.sessionStorage.setItem(TRACE_STORAGE_KEY, JSON.stringify(trimmed)) |
| 74 | + } catch { |
| 75 | + /* noop */ |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (typeof window !== "undefined") { |
| 80 | + try { |
| 81 | + window.name = `${WINDOW_NAME_PREFIX}${JSON.stringify(trimmed)}` |
| 82 | + } catch { |
| 83 | + /* noop */ |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + publishPerfHandle(trimmed) |
| 88 | +} |
| 89 | + |
| 90 | +function readStoredTrace(): PerfTraceEntry[] { |
| 91 | + if (inMemoryTrace) { |
| 92 | + return inMemoryTrace |
| 93 | + } |
| 94 | + |
| 95 | + let parsed: PerfTraceEntry[] = [] |
| 96 | + |
| 97 | + if (supportsSessionStorage()) { |
| 98 | + try { |
| 99 | + const raw = window.sessionStorage.getItem(TRACE_STORAGE_KEY) |
| 100 | + if (raw) { |
| 101 | + const value = JSON.parse(raw) |
| 102 | + if (Array.isArray(value)) { |
| 103 | + parsed = value as PerfTraceEntry[] |
| 104 | + } |
| 105 | + } |
| 106 | + } catch { |
| 107 | + parsed = [] |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (parsed.length === 0) { |
| 112 | + parsed = loadTraceFromWindowName() |
| 113 | + } |
| 114 | + |
| 115 | + const lastEntry = parsed[parsed.length - 1] |
| 116 | + if (lastEntry && Math.abs(absoluteNowMs() - lastEntry.absoluteTime) > TRACE_STALE_AFTER_MS) { |
| 117 | + parsed = [] |
| 118 | + } |
| 119 | + |
| 120 | + inMemoryTrace = parsed |
| 121 | + publishPerfHandle(parsed) |
| 122 | + return parsed |
| 123 | +} |
| 124 | + |
| 125 | +function getFirstMarkTime(trace: PerfTraceEntry[], name: string) { |
| 126 | + const entry = trace.find((item) => item.name === name) |
| 127 | + return entry?.absoluteTime |
| 128 | +} |
| 129 | + |
| 130 | +function durationBetween(trace: PerfTraceEntry[], start: string, end: string) { |
| 131 | + const startTime = getFirstMarkTime(trace, start) |
| 132 | + const endTime = getFirstMarkTime(trace, end) |
| 133 | + if (typeof startTime !== "number" || typeof endTime !== "number" || endTime < startTime) { |
| 134 | + return undefined |
| 135 | + } |
| 136 | + return Math.round((endTime - startTime) * 100) / 100 |
| 137 | +} |
| 138 | + |
| 139 | +export function getPerfTrace() { |
| 140 | + return [...readStoredTrace()] |
| 141 | +} |
| 142 | + |
| 143 | +export function summarizePerfTrace(trace = readStoredTrace()): PerfSummary { |
| 144 | + return { |
| 145 | + runtimeHost: runtimeEnv.host, |
| 146 | + runtimePlatform: runtimeEnv.platform, |
| 147 | + loaderToCliReadyMs: durationBetween(trace, "loading.screen.mounted", "loading.tauri.cli.ready"), |
| 148 | + loaderToNavigateMs: durationBetween(trace, "loading.screen.mounted", "loading.navigate"), |
| 149 | + bootstrapToAppMountMs: durationBetween(trace, "ui.bootstrap.start", "ui.app.mounted"), |
| 150 | + bootstrapToFirstFrameMs: durationBetween(trace, "ui.bootstrap.start", "ui.app.first-frame"), |
| 151 | + bootstrapToFirstContentMs: durationBetween(trace, "ui.bootstrap.start", "ui.session.first-content"), |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function publishPerfHandle(trace = readStoredTrace()) { |
| 156 | + if (typeof window === "undefined") { |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + ;(window as typeof window & { |
| 161 | + __CODENOMAD_PERF__?: { |
| 162 | + getTrace: () => PerfTraceEntry[] |
| 163 | + getSummary: () => PerfSummary |
| 164 | + clear: () => void |
| 165 | + mark: (name: string, detail?: PerfDetail) => PerfTraceEntry |
| 166 | + } |
| 167 | + }).__CODENOMAD_PERF__ = { |
| 168 | + getTrace: () => getPerfTrace(), |
| 169 | + getSummary: () => summarizePerfTrace(trace), |
| 170 | + clear: () => clearPerfTrace(), |
| 171 | + mark: (name: string, detail?: PerfDetail) => markPerf(name, detail), |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +export function clearPerfTrace() { |
| 176 | + persistTrace([]) |
| 177 | +} |
| 178 | + |
| 179 | +export function beginPerfTrace(name: string, detail?: PerfDetail) { |
| 180 | + clearPerfTrace() |
| 181 | + return markPerf(name, detail) |
| 182 | +} |
| 183 | + |
| 184 | +export function markPerf(name: string, detail?: PerfDetail): PerfTraceEntry { |
| 185 | + const entry: PerfTraceEntry = { |
| 186 | + name, |
| 187 | + time: nowMs(), |
| 188 | + absoluteTime: absoluteNowMs(), |
| 189 | + host: runtimeEnv.host, |
| 190 | + platform: runtimeEnv.platform, |
| 191 | + path: getCurrentPath(), |
| 192 | + detail, |
| 193 | + } |
| 194 | + |
| 195 | + if (typeof performance !== "undefined" && typeof performance.mark === "function") { |
| 196 | + try { |
| 197 | + performance.mark(name) |
| 198 | + } catch { |
| 199 | + /* noop */ |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + persistTrace([...readStoredTrace(), entry]) |
| 204 | + return entry |
| 205 | +} |
| 206 | + |
| 207 | +export function measurePerf(name: string, startMark: string, endMark: string) { |
| 208 | + if (typeof performance === "undefined" || typeof performance.measure !== "function") { |
| 209 | + return undefined |
| 210 | + } |
| 211 | + |
| 212 | + try { |
| 213 | + return performance.measure(name, startMark, endMark) |
| 214 | + } catch { |
| 215 | + return undefined |
| 216 | + } |
| 217 | +} |
0 commit comments