1
- import { writeFile } from "node:fs/promises " ;
2
- import { Session } from "node:inspector" ;
1
+ import { writeFileSync } from "node:fs" ;
2
+ import { Session } from "node:inspector/promises " ;
3
3
4
4
let session ;
5
5
let processSignals ;
@@ -10,15 +10,10 @@ export async function start() {
10
10
}
11
11
session = new Session ( ) ;
12
12
session . connect ( ) ;
13
- await new Promise ( ( resolve ) => {
14
- session . post ( "Profiler.enable" , ( ) => {
15
- console . log ( `Recording CPU profile...` ) ;
16
- session . post ( "Profiler.start" , ( ) => {
17
- processSignals = registerSigHooks ( ) ;
18
- resolve ( ) ;
19
- } ) ;
20
- } ) ;
21
- } ) ;
13
+ await session . post ( "Profiler.enable" ) ;
14
+ await session . post ( "Profiler.start" ) ;
15
+ console . log ( `Recording CPU profile...` ) ;
16
+ processSignals = registerSigHooks ( ) ;
22
17
}
23
18
24
19
async function writeProfile ( profile ) {
@@ -39,30 +34,22 @@ async function writeProfile(profile) {
39
34
const fileName = `./ui5_${ dateParts . year } -${ dateParts . month } -${ dateParts . day } _` +
40
35
`${ dateParts . hour } -${ dateParts . minute } -${ dateParts . second } .cpuprofile` ;
41
36
console . log ( `\nSaving CPU profile to ${ fileName } ...` ) ;
42
- await writeFile ( fileName , JSON . stringify ( profile ) ) ;
37
+ writeFileSync ( fileName , JSON . stringify ( profile ) ) ;
43
38
}
44
39
45
40
export async function stop ( ) {
46
41
if ( ! session ) {
47
42
return ;
48
43
}
44
+ const { profile} = await session . post ( "Profiler.stop" ) ;
45
+ session = null ;
46
+ if ( profile ) {
47
+ await writeProfile ( profile ) ;
48
+ }
49
49
if ( processSignals ) {
50
50
deregisterSigHooks ( processSignals ) ;
51
51
processSignals = null ;
52
52
}
53
- const profile = await new Promise ( ( resolve ) => {
54
- session . post ( "Profiler.stop" , ( err , { profile} ) => {
55
- if ( err ) {
56
- resolve ( null ) ;
57
- } else {
58
- resolve ( profile ) ;
59
- }
60
- } ) ;
61
- session = null ;
62
- } ) ;
63
- if ( profile ) {
64
- await writeProfile ( profile ) ;
65
- }
66
53
}
67
54
68
55
function registerSigHooks ( ) {
0 commit comments