MCPcopy Create free account
hub / github.com/Noumena-Network/code / startEventLoopStallDetector

Function startEventLoopStallDetector

src/utils/eventLoopStallDetector.ts:16–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14let interval: NodeJS.Timeout | null = null
15
16export function startEventLoopStallDetector(): void {
17 if (started) {
18 return
19 }
20
21 started = true
22 let expectedAt = performance.now() + CHECK_INTERVAL_MS
23 let lastLoggedAt = 0
24
25 interval = setInterval(() => {
26 const now = performance.now()
27 const lagMs = Math.max(0, now - expectedAt)
28 expectedAt = now + CHECK_INTERVAL_MS
29
30 if (lagMs <= STALL_THRESHOLD_MS) {
31 return
32 }
33
34 if (now - lastLoggedAt < LOG_THROTTLE_MS) {
35 return
36 }
37
38 lastLoggedAt = now
39 const roundedLag = Math.round(lagMs)
40
41 logForDebugging(
42 `[Perf] Event loop stall detected: ${roundedLag}ms main-thread pause`,
43 { level: 'warn' },
44 )
45 logEvent('ncode_event_loop_stall', {
46 stall_ms:
47 roundedLag as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
48 })
49 }, CHECK_INTERVAL_MS)
50
51 interval.unref?.()
52
53 if (!cleanupRegistered) {
54 cleanupRegistered = true
55 registerCleanup(async () => {
56 if (interval) {
57 clearInterval(interval)
58 interval = null
59 }
60 started = false
61 })
62 }
63}

Callers

nothing calls this directly

Calls 4

registerCleanupFunction · 0.85
maxMethod · 0.80
logForDebuggingFunction · 0.70
logEventFunction · 0.50

Tested by

no test coverage detected