@@ -133,9 +133,92 @@ const { sleep } = require('internal/util');
133133 `Expected samples to be recorded, got count=${ histogram . count } ` ) ;
134134 assert ( histogram . min > 0 ) ;
135135 assert ( histogram . max > 0 ) ;
136+ assert ( histogram . mean > 0 ) ;
137+ assert ( histogram . percentiles . size > 0 ) ;
138+ for ( let n = 1 ; n < 100 ; n = n + 10 ) {
139+ assert ( histogram . percentile ( n ) >= 0 ) ;
140+ }
141+ // reset() should restore the histogram to its initial state
142+ histogram . reset ( ) ;
143+ assert . strictEqual ( histogram . count , 0 ) ;
144+ assert . strictEqual ( histogram . max , 0 ) ;
145+ assert . strictEqual ( histogram . min , 9223372036854776000 ) ;
146+ assert ( Number . isNaN ( histogram . mean ) ) ;
147+ assert ( Number . isNaN ( histogram . stddev ) ) ;
148+ assert . strictEqual ( histogram . percentiles . size , 1 ) ;
149+ } ) , common . platformTimeout ( 20 ) ) ;
150+ }
151+
152+ {
153+ // enable()/disable() return values for ELDHistogram (samplePerIteration: true)
154+ const histogram = monitorEventLoopDelay ( { samplePerIteration : true } ) ;
155+ assert . strictEqual ( histogram . enable ( ) , true ) ;
156+ assert . strictEqual ( histogram . enable ( ) , false ) ; // already enabled, no-op
157+ assert . strictEqual ( histogram . disable ( ) , true ) ;
158+ assert . strictEqual ( histogram . disable ( ) , false ) ; // already disabled, no-op
159+ // Re-enabling after disable should work
160+ assert . strictEqual ( histogram . enable ( ) , true ) ;
161+ setTimeout ( common . mustCall ( ( ) => {
162+ histogram . disable ( ) ;
163+ assert ( histogram . count > 0 ,
164+ `Expected samples after re-enable, got count=${ histogram . count } ` ) ;
136165 } ) , common . platformTimeout ( 20 ) ) ;
137166}
138167
168+ {
169+ // Verify that samplePerIteration records exactly one sample per event loop iteration.
170+ const N = 10 ;
171+ const histogram = monitorEventLoopDelay ( { samplePerIteration : true } ) ;
172+ histogram . enable ( ) ;
173+
174+ let iterations = 0 ;
175+ const verify = common . mustCall ( ( ) => {
176+ histogram . disable ( ) ;
177+ assert (
178+ histogram . count >= N - 1 ,
179+ `Expected at least ${ N - 1 } samples for ${ N } iterations, got ${ histogram . count } `
180+ ) ;
181+ } ) ;
182+
183+ function tick ( ) {
184+ if ( ++ iterations < N ) {
185+ setImmediate ( tick ) ;
186+ } else {
187+ verify ( ) ;
188+ }
189+ }
190+ setImmediate ( tick ) ;
191+ }
192+
193+ {
194+ // samplePerIteration should sample per event loop iteration, independent of
195+ // the timer resolution used by the legacy monitorEventLoopDelay path.
196+ const N = 10 ;
197+ const histogram = monitorEventLoopDelay ( {
198+ samplePerIteration : true ,
199+ resolution : 60 * 1000 ,
200+ } ) ;
201+ histogram . enable ( ) ;
202+
203+ let iterations = 0 ;
204+ const verify = common . mustCall ( ( ) => {
205+ histogram . disable ( ) ;
206+ assert (
207+ histogram . count >= N - 1 ,
208+ `Expected samples despite large resolution, got count=${ histogram . count } `
209+ ) ;
210+ } ) ;
211+
212+ function tick ( ) {
213+ if ( ++ iterations < N ) {
214+ setImmediate ( tick ) ;
215+ } else {
216+ verify ( ) ;
217+ }
218+ }
219+ setImmediate ( tick ) ;
220+ }
221+
139222// Make sure that the histogram instances can be garbage-collected without
140223// and not just implicitly destroyed when the Environment is torn down.
141224process . on ( 'exit' , global . gc ) ;
0 commit comments