forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf.ts
More file actions
48 lines (42 loc) · 1.63 KB
/
Copy pathperf.ts
File metadata and controls
48 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { appendFileSync, mkdirSync } from 'fs'
import { dirname } from 'path'
// One structured perf event. `ts` is wall-clock epoch ms (for ordering across
// runs); `mono` is performance.now() high-resolution ms (for phase deltas within
// a single process run).
export interface PerfMark {
name: string
ts: number
mono: number
meta?: Record<string, unknown>
}
// Path is resolved lazily on every call so the main process can set
// process.env.OMI_PERF_LOG AFTER this module is imported (import order would
// otherwise capture an empty value at module load).
function logPath(): string {
return process.env.OMI_PERF_LOG ?? ''
}
const buffer: string[] = []
let dirEnsured = false
// Append a perf event. Cheap and always-on: buffered in memory and flushed on
// exit (or every 50 marks) so marking itself adds negligible latency. No-op
// unless OMI_PERF_LOG is set.
export function perfMark(name: string, meta?: Record<string, unknown>): void {
if (!logPath()) return
const mark: PerfMark = { name, ts: Date.now(), mono: performance.now(), meta }
buffer.push(JSON.stringify(mark))
if (buffer.length >= 50) flushPerfMarks()
}
// Synchronously write any buffered marks to disk. Safe to call multiple times.
export function flushPerfMarks(): void {
const path = logPath()
if (!path || buffer.length === 0) return
if (!dirEnsured) {
mkdirSync(dirname(path), { recursive: true })
dirEnsured = true
}
appendFileSync(path, buffer.join('\n') + '\n')
buffer.length = 0
}
// Flush on process exit so marks survive an app.quit(). exit handlers must be
// synchronous; appendFileSync is.
process.on('exit', () => flushPerfMarks())