MCPcopy Create free account
hub / github.com/chakra-ui/panda / startProfiling

Function startProfiling

packages/node/src/cpu-profile.ts:6–91  ·  view source on GitHub ↗
(cwd: string, prefix: string, isWatching?: boolean)

Source from the content-addressed store, hash-verified

4import readline from 'node:readline'
5
6export const startProfiling = async (cwd: string, prefix: string, isWatching?: boolean) => {
7 const inspector = await import('node:inspector').then((r) => r.default)
8
9 const session = new inspector.Session()
10 session.connect()
11
12 let state: ProfileState = 'idle'
13 const setState = (s: ProfileState) => {
14 state = s
15 }
16
17 await new Promise<void>((resolve) => {
18 session.post('Profiler.enable', () => {
19 session.post('Profiler.start', () => {
20 setState('profiling')
21 resolve()
22 })
23 })
24 })
25
26 const toggleProfiler = () => {
27 if (state === 'idle') {
28 console.log('Starting CPU profiling...')
29 setState('starting')
30 session.post('Profiler.start', () => {
31 setState('profiling')
32 console.log("Press 'p' to stop profiling...")
33 })
34 } else if (state === 'profiling') {
35 console.log('Stopping CPU profiling...')
36 stopProfiling()
37 }
38 }
39
40 if (isWatching) {
41 readline.emitKeypressEvents(process.stdin)
42 if (process.stdin.isTTY) process.stdin.setRawMode(true)
43 console.log("Press 'p' to stop profiling...")
44
45 process.stdin.on('keypress', (str, key) => {
46 // Start/stop profiling on 'p'
47 if (key.name === 'p') {
48 toggleProfiler()
49 }
50
51 // Exit the process on Ctrl+C
52 if (key.ctrl && key.name === 'c') {
53 stopProfiling(() => process.exit())
54 }
55 })
56 }
57
58 const stopProfiling = (cb?: () => void) => {
59 if (state !== 'profiling') {
60 cb?.()
61 return
62 }
63

Callers 1

mainFunction · 0.90

Calls 5

setStateFunction · 0.85
toggleProfilerFunction · 0.85
stopProfilingFunction · 0.70
onMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected