@@ -151,80 +151,69 @@ Daemon.prototype.innerStart = function(cb) {
151151 that . sendReady ( cb ) ;
152152 } ) ;
153153
154- var profiler ;
155-
156- try {
157- profiler = semver . satisfies ( process . version , '>= 10.0.0' ) ? require ( 'inspector' ) : null ;
158- } catch ( e ) {
159- profiler = null ;
160- }
161154
162155 /**
163156 * Memory Snapshot
164157 */
165- function snapshotPM2 ( msg , cb ) {
166- if ( profiler == null ) {
167- console . log ( 'Heap snapshot is not available (node 10+)' ) ;
168- return cb ( new Error ( 'Heap snapshot is not available (node 10+)' ) ) ;
158+ function profile ( type , msg , cb ) {
159+ if ( semver . satisfies ( process . version , '< 8' ) )
160+ return cb ( null , { error : 'Node.js is not on right version' } )
161+
162+ var cmd
163+
164+ if ( type === 'cpu' ) {
165+ cmd = {
166+ enable : 'Profiler.enable' ,
167+ start : 'Profiler.start' ,
168+ stop : 'Profiler.stop' ,
169+ disable : 'Profiler.disable'
170+ }
171+ }
172+ if ( type == 'mem' ) {
173+ cmd = {
174+ enable : 'HeapProfiler.enable' ,
175+ start : 'HeapProfiler.startSampling' ,
176+ stop : 'HeapProfiler.stopSampling' ,
177+ disable : 'HeapProfiler.disable'
178+ }
169179 }
170180
171- const session = new profiler . Session ( )
172- session . connect ( )
173- session . post ( 'HeapProfiler.enable' )
174-
175- const chunks = [ ]
176- session . on ( 'HeapProfiler.addHeapSnapshotChunk' , ( data ) => {
177- chunks . push ( data . params . chunk )
178- } )
179-
180- session . post ( 'HeapProfiler.takeHeapSnapshot' , {
181- reportProgress : false
182- } , ( err , data ) => {
183- if ( err ) return cb ( err )
184-
185- fs . writeFile ( msg . pwd , chunks . join ( '' ) , function ( ) {
186- session . post ( 'Profiler.disable' )
187- session . disconnect ( )
181+ const inspector = require ( 'inspector' )
182+ var session = new inspector . Session ( )
188183
189- return cb ( null , { file : msg . pwd } ) ;
190- } ) ;
191- } )
192- }
184+ session . connect ( )
193185
194- function startProfilingPM2 ( msg , cb ) {
195- if ( profiler == null ) {
196- console . log ( 'v8-profiler is not available' ) ;
197- return cb ( new Error ( 'v8-profiler is not available' ) ) ;
198- }
186+ var timeout = msg . timeout || 5000
199187
200- profiler . startProfiling ( 'cpu' ) ;
188+ session . post ( cmd . enable , ( err , data ) => {
189+ if ( err ) return cb ( null , { error : err . message || err } )
201190
202- process . nextTick ( function ( ) {
203- return cb ( null , { msg : 'profiling started' } ) ;
204- } ) ;
205- }
191+ console . log ( `Starting ${ cmd . start } ` )
192+ session . post ( cmd . start , ( err , data ) => {
193+ if ( err ) return cb ( null , { error : err . message || err } )
206194
207- function stopProfilingPM2 ( msg , cb ) {
208- if ( profiler == null ) {
209- console . log ( 'v8-profiler is not available' ) ;
210- return cb ( new Error ( 'v8-profiler is not available' ) ) ;
211- }
195+ setTimeout ( ( ) => {
196+ session . post ( cmd . stop , ( err , data ) => {
197+ if ( err ) return cb ( null , { error : err . message || err } )
198+ const profile = data . profile
212199
213- var profile1 = profiler . stopProfiling ( 'cpu' ) ;
200+ console . log ( `Stopping ${ cmd . stop } ` )
201+ session . post ( cmd . disable )
214202
215- profile1 . export ( )
216- . pipe ( fs . createWriteStream ( msg . pwd ) )
217- . on ( 'finish' , function ( ) {
218- profile1 . delete ( ) ;
219- return cb ( null , { file : msg . pwd } ) ;
220- } ) ;
203+ fs . writeFile ( msg . pwd , JSON . stringify ( profile ) , ( err ) => {
204+ if ( err ) return cb ( null , { error : err . message || err } )
205+ return cb ( null , { file : msg . pwd } )
206+ } )
207+ } )
208+ } , timeout )
209+ } )
210+ } )
221211 }
222212
223213 server . expose ( {
224214 killMe : that . close . bind ( this ) ,
225- snapshotPM2 : snapshotPM2 ,
226- profileStart : startProfilingPM2 ,
227- profileStop : stopProfilingPM2 ,
215+ profileCPU : profile . bind ( this , 'cpu' ) ,
216+ profileMEM : profile . bind ( this , 'mem' ) ,
228217 prepare : God . prepare ,
229218 getMonitorData : God . getMonitorData ,
230219 getSystemData : God . getSystemData ,
0 commit comments