@@ -69,7 +69,7 @@ class Profiler extends EventEmitter {
69
69
mapper,
70
70
nearOOMCallback : this . _nearOOMExport . bind ( this )
71
71
} )
72
- this . _logger . debug ( `Started ${ profiler . type } profiler in thread ${ threadId } ` )
72
+ this . _logger . debug ( `Started ${ profiler . type } profiler in ${ threadNamePrefix } thread ` )
73
73
}
74
74
75
75
this . _capture ( this . _timeoutInterval , start )
@@ -83,7 +83,7 @@ class Profiler extends EventEmitter {
83
83
84
84
_nearOOMExport ( profileType , encodedProfile ) {
85
85
const start = this . _lastStart
86
- const end = new Date ( )
86
+ const end = Date . now ( )
87
87
this . _submit ( {
88
88
[ profileType ] : encodedProfile
89
89
} , start , end , snapshotKinds . ON_OUT_OF_MEMORY )
@@ -98,7 +98,7 @@ class Profiler extends EventEmitter {
98
98
99
99
// collect and export current profiles
100
100
// once collect returns, profilers can be safely stopped
101
- this . _collect ( snapshotKinds . ON_SHUTDOWN )
101
+ this . _collect ( snapshotKinds . ON_SHUTDOWN , false )
102
102
this . _stop ( )
103
103
}
104
104
@@ -109,13 +109,11 @@ class Profiler extends EventEmitter {
109
109
110
110
for ( const profiler of this . _config . profilers ) {
111
111
profiler . stop ( )
112
- this . _logger . debug ( `Stopped ${ profiler . type } profiler` )
112
+ this . _logger . debug ( `Stopped ${ profiler . type } profiler in ${ threadNamePrefix } thread ` )
113
113
}
114
114
115
115
clearTimeout ( this . _timer )
116
116
this . _timer = undefined
117
-
118
- return this
119
117
}
120
118
121
119
_capture ( timeout , start ) {
@@ -129,18 +127,21 @@ class Profiler extends EventEmitter {
129
127
}
130
128
}
131
129
132
- async _collect ( snapshotKind ) {
130
+ async _collect ( snapshotKind , restart = true ) {
133
131
if ( ! this . _enabled ) return
134
132
135
- const start = this . _lastStart
136
- const end = new Date ( )
133
+ const startDate = this . _lastStart
134
+ const endDate = new Date ( )
137
135
const profiles = [ ]
138
136
const encodedProfiles = { }
139
137
140
138
try {
141
139
// collect profiles synchronously so that profilers can be safely stopped asynchronously
142
140
for ( const profiler of this . _config . profilers ) {
143
- const profile = profiler . profile ( start , end )
141
+ const profile = profiler . profile ( { startDate, endDate, restart } )
142
+ if ( ! restart ) {
143
+ this . _logger . debug ( `Stopped ${ profiler . type } profiler in ${ threadNamePrefix } thread` )
144
+ }
144
145
if ( ! profile ) continue
145
146
profiles . push ( { profiler, profile } )
146
147
}
@@ -156,8 +157,10 @@ class Profiler extends EventEmitter {
156
157
} )
157
158
}
158
159
159
- this . _capture ( this . _timeoutInterval , end )
160
- await this . _submit ( encodedProfiles , start , end , snapshotKind )
160
+ if ( restart ) {
161
+ this . _capture ( this . _timeoutInterval , endDate )
162
+ }
163
+ await this . _submit ( encodedProfiles , startDate , endDate , snapshotKind )
161
164
this . _logger . debug ( 'Submitted profiles' )
162
165
} catch ( err ) {
163
166
this . _logger . error ( err )
@@ -197,10 +200,10 @@ class ServerlessProfiler extends Profiler {
197
200
this . _flushAfterIntervals = this . _config . flushInterval / 1000
198
201
}
199
202
200
- async _collect ( snapshotKind ) {
201
- if ( this . _profiledIntervals >= this . _flushAfterIntervals ) {
203
+ async _collect ( snapshotKind , restart = true ) {
204
+ if ( this . _profiledIntervals >= this . _flushAfterIntervals || ! restart ) {
202
205
this . _profiledIntervals = 0
203
- await super . _collect ( snapshotKind )
206
+ await super . _collect ( snapshotKind , restart )
204
207
} else {
205
208
this . _profiledIntervals += 1
206
209
this . _capture ( this . _timeoutInterval , new Date ( ) )
0 commit comments