1'use strict'; 2 3const common = require('../common'); 4 5const { 6 ok, 7 strictEqual, 8 throws, 9} = require('assert'); 10 11const { 12 createHistogram, 13 monitorEventLoopDelay, 14} = require('perf_hooks'); 15 16const { inspect } = require('util'); 17 18{ 19 const h = createHistogram(); 20 21 strictEqual(h.min, 9223372036854776000); 22 strictEqual(h.minBigInt, 9223372036854775807n); 23 strictEqual(h.max, 0); 24 strictEqual(h.maxBigInt, 0n); 25 strictEqual(h.exceeds, 0); 26 strictEqual(h.exceedsBigInt, 0n); 27 ok(Number.isNaN(h.mean)); 28 ok(Number.isNaN(h.stddev)); 29 30 strictEqual(h.count, 0); 31 strictEqual(h.countBigInt, 0n); 32 33 h.record(1); 34 35 strictEqual(h.count, 1); 36 strictEqual(h.countBigInt, 1n); 37 38 [false, '', {}, undefined, null].forEach((i) => { 39 throws(() => h.record(i), { 40 code: 'ERR_INVALID_ARG_TYPE' 41 }); 42 }); 43 throws(() => h.record(0, Number.MAX_SAFE_INTEGER + 1), { 44 code: 'ERR_OUT_OF_RANGE' 45 }); 46 47 strictEqual(h.min, 1); 48 strictEqual(h.minBigInt, 1n); 49 strictEqual(h.max, 1); 50 strictEqual(h.maxBigInt, 1n); 51 strictEqual(h.exceeds, 0); 52 strictEqual(h.mean, 1); 53 strictEqual(h.stddev, 0); 54 55 strictEqual(h.percentile(1), 1); 56 strictEqual(h.percentile(100), 1); 57 58 strictEqual(h.percentileBigInt(1), 1n); 59 strictEqual(h.percentileBigInt(100), 1n); 60 61 const mc = new MessageChannel(); 62 mc.port1.onmessage = common.mustCall(({ data }) => { 63 strictEqual(h.min, 1); 64 strictEqual(h.max, 1); 65 strictEqual(h.exceeds, 0); 66 strictEqual(h.mean, 1); 67 strictEqual(h.stddev, 0); 68 69 data.record(2n); 70 data.recordDelta(); 71 72 strictEqual(h.max, 2); 73 74 mc.port1.close(); 75 }); 76 mc.port2.postMessage(h); 77} 78 79{ 80 const e = monitorEventLoopDelay(); 81 strictEqual(e.count, 0); 82 e.enable(); 83 const mc = new MessageChannel(); 84 mc.port1.onmessage = common.mustCall(({ data }) => { 85 strictEqual(typeof data.min, 'number'); 86 ok(data.min > 0); 87 ok(data.count > 0); 88 strictEqual(data.disable, undefined); 89 strictEqual(data.enable, undefined); 90 mc.port1.close(); 91 }); 92 const interval = setInterval(() => { 93 if (e.count > 0) { 94 clearInterval(interval); 95 mc.port2.postMessage(e); 96 } 97 }, 50); 98} 99 100{ 101 const h = createHistogram(); 102 ok(inspect(h, { depth: null }).startsWith('Histogram')); 103 strictEqual(inspect(h, { depth: -1 }), '[RecordableHistogram]'); 104} 105 106{ 107 // Tests that RecordableHistogram is impossible to construct manually 108 const h = createHistogram(); 109 throws(() => new h.constructor(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' }); 110} 111 112{ 113 [ 114 'hello', 115 1, 116 null, 117 ].forEach((i) => { 118 throws(() => createHistogram(i), { code: 'ERR_INVALID_ARG_TYPE' }); 119 }); 120 121 [ 122 'hello', 123 false, 124 null, 125 {}, 126 ].forEach((i) => { 127 throws(() => createHistogram({ lowest: i }), { 128 code: 'ERR_INVALID_ARG_TYPE', 129 }); 130 throws(() => createHistogram({ highest: i }), { 131 code: 'ERR_INVALID_ARG_TYPE', 132 }); 133 throws(() => createHistogram({ figures: i }), { 134 code: 'ERR_INVALID_ARG_TYPE', 135 }); 136 }); 137 138 // Number greater than 5 is not allowed 139 for (const i of [6, 10]) { 140 throws(() => createHistogram({ figures: i }), { 141 code: 'ERR_OUT_OF_RANGE', 142 }); 143 } 144 145 createHistogram({ lowest: 1, highest: 11, figures: 1 }); 146} 147 148{ 149 const h1 = createHistogram(); 150 const h2 = createHistogram(); 151 152 h1.record(1); 153 154 strictEqual(h2.count, 0); 155 strictEqual(h1.count, 1); 156 157 h2.add(h1); 158 159 strictEqual(h2.count, 1); 160 161 [ 162 'hello', 163 1, 164 false, 165 {}, 166 ].forEach((i) => { 167 throws(() => h1.add(i), { 168 code: 'ERR_INVALID_ARG_TYPE', 169 }); 170 }); 171} 172