|
9 | 9 | } = require('perf_hooks'); |
10 | 10 | const { sleep } = require('internal/util'); |
11 | 11 |
|
| 12 | +function runEventLoopIterations(iterations, callback) { |
| 13 | + let remaining = iterations; |
| 14 | + function tick() { |
| 15 | + if (--remaining > 0) { |
| 16 | + setImmediate(tick); |
| 17 | + } else { |
| 18 | + callback(); |
| 19 | + } |
| 20 | + } |
| 21 | + setImmediate(tick); |
| 22 | +} |
| 23 | + |
12 | 24 | { |
13 | 25 | const histogram = monitorEventLoopDelay(); |
14 | 26 | assert(histogram); |
@@ -125,12 +137,16 @@ const { sleep } = require('internal/util'); |
125 | 137 | } |
126 | 138 |
|
127 | 139 | { |
| 140 | + const iterations = 10; |
128 | 141 | const histogram = monitorEventLoopDelay({ samplePerIteration: true }); |
129 | 142 | histogram.enable(); |
130 | | - setTimeout(common.mustCall(() => { |
| 143 | + runEventLoopIterations(iterations, common.mustCall(() => { |
131 | 144 | histogram.disable(); |
132 | | - assert(histogram.count > 0, |
133 | | - `Expected samples to be recorded, got count=${histogram.count}`); |
| 145 | + assert( |
| 146 | + histogram.count >= iterations - 1, |
| 147 | + `Expected at least ${iterations - 1} samples for ${iterations} iterations, ` + |
| 148 | + `got ${histogram.count}` |
| 149 | + ); |
134 | 150 | assert(histogram.min > 0); |
135 | 151 | assert(histogram.max > 0); |
136 | 152 | assert(histogram.mean > 0); |
@@ -146,7 +162,7 @@ const { sleep } = require('internal/util'); |
146 | 162 | assert(Number.isNaN(histogram.mean)); |
147 | 163 | assert(Number.isNaN(histogram.stddev)); |
148 | 164 | assert.strictEqual(histogram.percentiles.size, 1); |
149 | | - }), common.platformTimeout(20)); |
| 165 | + })); |
150 | 166 | } |
151 | 167 |
|
152 | 168 | { |
@@ -158,65 +174,40 @@ const { sleep } = require('internal/util'); |
158 | 174 | assert.strictEqual(histogram.disable(), false); // Already disabled, no-op |
159 | 175 | // Re-enabling after disable should work |
160 | 176 | assert.strictEqual(histogram.enable(), true); |
161 | | - setTimeout(common.mustCall(() => { |
| 177 | + runEventLoopIterations(10, common.mustCall(() => { |
162 | 178 | histogram.disable(); |
163 | 179 | assert(histogram.count > 0, |
164 | 180 | `Expected samples after re-enable, got count=${histogram.count}`); |
165 | | - }), common.platformTimeout(20)); |
| 181 | + })); |
166 | 182 | } |
167 | 183 |
|
168 | 184 | { |
169 | 185 | // Verify that samplePerIteration records exactly one sample per event loop iteration. |
170 | | - const N = 10; |
| 186 | + // It should do so independently of the timer resolution used by the legacy |
| 187 | + // monitorEventLoopDelay path. |
| 188 | + const iterations = 10; |
171 | 189 | 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({ |
| 190 | + const largeResolutionHistogram = monitorEventLoopDelay({ |
198 | 191 | samplePerIteration: true, |
199 | 192 | resolution: 60 * 1000, |
200 | 193 | }); |
201 | 194 | histogram.enable(); |
| 195 | + largeResolutionHistogram.enable(); |
202 | 196 |
|
203 | | - let iterations = 0; |
204 | | - const verify = common.mustCall(() => { |
| 197 | + runEventLoopIterations(iterations, common.mustCall(() => { |
205 | 198 | histogram.disable(); |
| 199 | + largeResolutionHistogram.disable(); |
206 | 200 | assert( |
207 | | - histogram.count >= N - 1, |
208 | | - `Expected samples despite large resolution, got count=${histogram.count}` |
| 201 | + histogram.count >= iterations - 1, |
| 202 | + `Expected at least ${iterations - 1} samples for ${iterations} iterations, ` + |
| 203 | + `got ${histogram.count}` |
209 | 204 | ); |
210 | | - }); |
211 | | - |
212 | | - function tick() { |
213 | | - if (++iterations < N) { |
214 | | - setImmediate(tick); |
215 | | - } else { |
216 | | - verify(); |
217 | | - } |
218 | | - } |
219 | | - setImmediate(tick); |
| 205 | + assert( |
| 206 | + largeResolutionHistogram.count >= iterations - 1, |
| 207 | + `Expected samples despite large resolution, ` + |
| 208 | + `got count=${largeResolutionHistogram.count}` |
| 209 | + ); |
| 210 | + })); |
220 | 211 | } |
221 | 212 |
|
222 | 213 | // Make sure that the histogram instances can be garbage-collected without |
|