1// Flags: --expose-gc --expose-internals 2'use strict'; 3 4const common = require('../common'); 5const assert = require('assert'); 6const { 7 monitorEventLoopDelay 8} = require('perf_hooks'); 9const { sleep } = require('internal/util'); 10 11{ 12 const histogram = monitorEventLoopDelay(); 13 assert(histogram); 14 assert(histogram.enable()); 15 assert(!histogram.enable()); 16 histogram.reset(); 17 assert(histogram.disable()); 18 assert(!histogram.disable()); 19} 20 21{ 22 [null, 'a', 1, false, Infinity].forEach((i) => { 23 assert.throws( 24 () => monitorEventLoopDelay(i), 25 { 26 name: 'TypeError', 27 code: 'ERR_INVALID_ARG_TYPE' 28 } 29 ); 30 }); 31 32 [null, 'a', false, {}, []].forEach((i) => { 33 assert.throws( 34 () => monitorEventLoopDelay({ resolution: i }), 35 { 36 name: 'TypeError', 37 code: 'ERR_INVALID_ARG_TYPE' 38 } 39 ); 40 }); 41 42 [-1, 0, Infinity].forEach((i) => { 43 assert.throws( 44 () => monitorEventLoopDelay({ resolution: i }), 45 { 46 name: 'RangeError', 47 code: 'ERR_INVALID_OPT_VALUE' 48 } 49 ); 50 }); 51} 52 53{ 54 const histogram = monitorEventLoopDelay({ resolution: 1 }); 55 histogram.enable(); 56 let m = 5; 57 function spinAWhile() { 58 sleep(1000); 59 if (--m > 0) { 60 setTimeout(spinAWhile, common.platformTimeout(500)); 61 } else { 62 histogram.disable(); 63 // The values are non-deterministic, so we just check that a value is 64 // present, as opposed to a specific value. 65 assert(histogram.min > 0); 66 assert(histogram.max > 0); 67 assert(histogram.stddev > 0); 68 assert(histogram.mean > 0); 69 assert(histogram.percentiles.size > 0); 70 for (let n = 1; n < 100; n = n + 0.1) { 71 assert(histogram.percentile(n) >= 0); 72 } 73 histogram.reset(); 74 assert.strictEqual(histogram.min, 9223372036854776000); 75 assert.strictEqual(histogram.max, 0); 76 assert(Number.isNaN(histogram.stddev)); 77 assert(Number.isNaN(histogram.mean)); 78 assert.strictEqual(histogram.percentiles.size, 1); 79 80 ['a', false, {}, []].forEach((i) => { 81 assert.throws( 82 () => histogram.percentile(i), 83 { 84 name: 'TypeError', 85 code: 'ERR_INVALID_ARG_TYPE' 86 } 87 ); 88 }); 89 [-1, 0, 101, NaN].forEach((i) => { 90 assert.throws( 91 () => histogram.percentile(i), 92 { 93 name: 'RangeError', 94 code: 'ERR_INVALID_ARG_VALUE' 95 } 96 ); 97 }); 98 } 99 } 100 spinAWhile(); 101} 102 103// Make sure that the histogram instances can be garbage-collected without 104// and not just implictly destroyed when the Environment is torn down. 105process.on('exit', global.gc); 106