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 " ;
33
44let session ;
55let processSignals ;
@@ -10,15 +10,10 @@ export async function start() {
1010 }
1111 session = new Session ( ) ;
1212 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 ( ) ;
2217}
2318
2419async function writeProfile ( profile ) {
@@ -39,30 +34,22 @@ async function writeProfile(profile) {
3934 const fileName = `./ui5_${ dateParts . year } -${ dateParts . month } -${ dateParts . day } _` +
4035 `${ dateParts . hour } -${ dateParts . minute } -${ dateParts . second } .cpuprofile` ;
4136 console . log ( `\nSaving CPU profile to ${ fileName } ...` ) ;
42- await writeFile ( fileName , JSON . stringify ( profile ) ) ;
37+ writeFileSync ( fileName , JSON . stringify ( profile ) ) ;
4338}
4439
4540export async function stop ( ) {
4641 if ( ! session ) {
4742 return ;
4843 }
44+ const { profile} = await session . post ( "Profiler.stop" ) ;
45+ session = null ;
46+ if ( profile ) {
47+ await writeProfile ( profile ) ;
48+ }
4949 if ( processSignals ) {
5050 deregisterSigHooks ( processSignals ) ;
5151 processSignals = null ;
5252 }
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- }
6653}
6754
6855function registerSigHooks ( ) {
0 commit comments