11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst {
61cb0ef41Sopenharmony_ci  ok,
71cb0ef41Sopenharmony_ci  strictEqual,
81cb0ef41Sopenharmony_ci  throws,
91cb0ef41Sopenharmony_ci} = require('assert');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  createHistogram,
131cb0ef41Sopenharmony_ci  monitorEventLoopDelay,
141cb0ef41Sopenharmony_ci} = require('perf_hooks');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst { inspect } = require('util');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci{
191cb0ef41Sopenharmony_ci  const h = createHistogram();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  strictEqual(h.min, 9223372036854776000);
221cb0ef41Sopenharmony_ci  strictEqual(h.minBigInt, 9223372036854775807n);
231cb0ef41Sopenharmony_ci  strictEqual(h.max, 0);
241cb0ef41Sopenharmony_ci  strictEqual(h.maxBigInt, 0n);
251cb0ef41Sopenharmony_ci  strictEqual(h.exceeds, 0);
261cb0ef41Sopenharmony_ci  strictEqual(h.exceedsBigInt, 0n);
271cb0ef41Sopenharmony_ci  ok(Number.isNaN(h.mean));
281cb0ef41Sopenharmony_ci  ok(Number.isNaN(h.stddev));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  strictEqual(h.count, 0);
311cb0ef41Sopenharmony_ci  strictEqual(h.countBigInt, 0n);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  h.record(1);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  strictEqual(h.count, 1);
361cb0ef41Sopenharmony_ci  strictEqual(h.countBigInt, 1n);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  [false, '', {}, undefined, null].forEach((i) => {
391cb0ef41Sopenharmony_ci    throws(() => h.record(i), {
401cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE'
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci  throws(() => h.record(0, Number.MAX_SAFE_INTEGER + 1), {
441cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE'
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  strictEqual(h.min, 1);
481cb0ef41Sopenharmony_ci  strictEqual(h.minBigInt, 1n);
491cb0ef41Sopenharmony_ci  strictEqual(h.max, 1);
501cb0ef41Sopenharmony_ci  strictEqual(h.maxBigInt, 1n);
511cb0ef41Sopenharmony_ci  strictEqual(h.exceeds, 0);
521cb0ef41Sopenharmony_ci  strictEqual(h.mean, 1);
531cb0ef41Sopenharmony_ci  strictEqual(h.stddev, 0);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  strictEqual(h.percentile(1), 1);
561cb0ef41Sopenharmony_ci  strictEqual(h.percentile(100), 1);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  strictEqual(h.percentileBigInt(1), 1n);
591cb0ef41Sopenharmony_ci  strictEqual(h.percentileBigInt(100), 1n);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
621cb0ef41Sopenharmony_ci  mc.port1.onmessage = common.mustCall(({ data }) => {
631cb0ef41Sopenharmony_ci    strictEqual(h.min, 1);
641cb0ef41Sopenharmony_ci    strictEqual(h.max, 1);
651cb0ef41Sopenharmony_ci    strictEqual(h.exceeds, 0);
661cb0ef41Sopenharmony_ci    strictEqual(h.mean, 1);
671cb0ef41Sopenharmony_ci    strictEqual(h.stddev, 0);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    data.record(2n);
701cb0ef41Sopenharmony_ci    data.recordDelta();
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    strictEqual(h.max, 2);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    mc.port1.close();
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci  mc.port2.postMessage(h);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci{
801cb0ef41Sopenharmony_ci  const e = monitorEventLoopDelay();
811cb0ef41Sopenharmony_ci  strictEqual(e.count, 0);
821cb0ef41Sopenharmony_ci  e.enable();
831cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
841cb0ef41Sopenharmony_ci  mc.port1.onmessage = common.mustCall(({ data }) => {
851cb0ef41Sopenharmony_ci    strictEqual(typeof data.min, 'number');
861cb0ef41Sopenharmony_ci    ok(data.min > 0);
871cb0ef41Sopenharmony_ci    ok(data.count > 0);
881cb0ef41Sopenharmony_ci    strictEqual(data.disable, undefined);
891cb0ef41Sopenharmony_ci    strictEqual(data.enable, undefined);
901cb0ef41Sopenharmony_ci    mc.port1.close();
911cb0ef41Sopenharmony_ci  });
921cb0ef41Sopenharmony_ci  const interval = setInterval(() => {
931cb0ef41Sopenharmony_ci    if (e.count > 0) {
941cb0ef41Sopenharmony_ci      clearInterval(interval);
951cb0ef41Sopenharmony_ci      mc.port2.postMessage(e);
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci  }, 50);
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci{
1011cb0ef41Sopenharmony_ci  const h = createHistogram();
1021cb0ef41Sopenharmony_ci  ok(inspect(h, { depth: null }).startsWith('Histogram'));
1031cb0ef41Sopenharmony_ci  strictEqual(inspect(h, { depth: -1 }), '[RecordableHistogram]');
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci{
1071cb0ef41Sopenharmony_ci  // Tests that RecordableHistogram is impossible to construct manually
1081cb0ef41Sopenharmony_ci  const h = createHistogram();
1091cb0ef41Sopenharmony_ci  throws(() => new h.constructor(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci{
1131cb0ef41Sopenharmony_ci  [
1141cb0ef41Sopenharmony_ci    'hello',
1151cb0ef41Sopenharmony_ci    1,
1161cb0ef41Sopenharmony_ci    null,
1171cb0ef41Sopenharmony_ci  ].forEach((i) => {
1181cb0ef41Sopenharmony_ci    throws(() => createHistogram(i), { code: 'ERR_INVALID_ARG_TYPE' });
1191cb0ef41Sopenharmony_ci  });
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  [
1221cb0ef41Sopenharmony_ci    'hello',
1231cb0ef41Sopenharmony_ci    false,
1241cb0ef41Sopenharmony_ci    null,
1251cb0ef41Sopenharmony_ci    {},
1261cb0ef41Sopenharmony_ci  ].forEach((i) => {
1271cb0ef41Sopenharmony_ci    throws(() => createHistogram({ lowest: i }), {
1281cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1291cb0ef41Sopenharmony_ci    });
1301cb0ef41Sopenharmony_ci    throws(() => createHistogram({ highest: i }), {
1311cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1321cb0ef41Sopenharmony_ci    });
1331cb0ef41Sopenharmony_ci    throws(() => createHistogram({ figures: i }), {
1341cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1351cb0ef41Sopenharmony_ci    });
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  // Number greater than 5 is not allowed
1391cb0ef41Sopenharmony_ci  for (const i of [6, 10]) {
1401cb0ef41Sopenharmony_ci    throws(() => createHistogram({ figures: i }), {
1411cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
1421cb0ef41Sopenharmony_ci    });
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  createHistogram({ lowest: 1, highest: 11, figures: 1 });
1461cb0ef41Sopenharmony_ci}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci{
1491cb0ef41Sopenharmony_ci  const h1 = createHistogram();
1501cb0ef41Sopenharmony_ci  const h2 = createHistogram();
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  h1.record(1);
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  strictEqual(h2.count, 0);
1551cb0ef41Sopenharmony_ci  strictEqual(h1.count, 1);
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  h2.add(h1);
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  strictEqual(h2.count, 1);
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci  [
1621cb0ef41Sopenharmony_ci    'hello',
1631cb0ef41Sopenharmony_ci    1,
1641cb0ef41Sopenharmony_ci    false,
1651cb0ef41Sopenharmony_ci    {},
1661cb0ef41Sopenharmony_ci  ].forEach((i) => {
1671cb0ef41Sopenharmony_ci    throws(() => h1.add(i), {
1681cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1691cb0ef41Sopenharmony_ci    });
1701cb0ef41Sopenharmony_ci  });
1711cb0ef41Sopenharmony_ci}
172