11cb0ef41Sopenharmony_ci// Flags: --expose-gc --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  monitorEventLoopDelay
81cb0ef41Sopenharmony_ci} = require('perf_hooks');
91cb0ef41Sopenharmony_ciconst { sleep } = require('internal/util');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const histogram = monitorEventLoopDelay();
131cb0ef41Sopenharmony_ci  assert(histogram);
141cb0ef41Sopenharmony_ci  assert(histogram.enable());
151cb0ef41Sopenharmony_ci  assert(!histogram.enable());
161cb0ef41Sopenharmony_ci  histogram.reset();
171cb0ef41Sopenharmony_ci  assert(histogram.disable());
181cb0ef41Sopenharmony_ci  assert(!histogram.disable());
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  [null, 'a', 1, false, Infinity].forEach((i) => {
231cb0ef41Sopenharmony_ci    assert.throws(
241cb0ef41Sopenharmony_ci      () => monitorEventLoopDelay(i),
251cb0ef41Sopenharmony_ci      {
261cb0ef41Sopenharmony_ci        name: 'TypeError',
271cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE'
281cb0ef41Sopenharmony_ci      }
291cb0ef41Sopenharmony_ci    );
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  [null, 'a', false, {}, []].forEach((i) => {
331cb0ef41Sopenharmony_ci    assert.throws(
341cb0ef41Sopenharmony_ci      () => monitorEventLoopDelay({ resolution: i }),
351cb0ef41Sopenharmony_ci      {
361cb0ef41Sopenharmony_ci        name: 'TypeError',
371cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE'
381cb0ef41Sopenharmony_ci      }
391cb0ef41Sopenharmony_ci    );
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  [-1, 0, 2 ** 53, Infinity].forEach((i) => {
431cb0ef41Sopenharmony_ci    assert.throws(
441cb0ef41Sopenharmony_ci      () => monitorEventLoopDelay({ resolution: i }),
451cb0ef41Sopenharmony_ci      {
461cb0ef41Sopenharmony_ci        name: 'RangeError',
471cb0ef41Sopenharmony_ci        code: 'ERR_OUT_OF_RANGE'
481cb0ef41Sopenharmony_ci      }
491cb0ef41Sopenharmony_ci    );
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci  const histogram = monitorEventLoopDelay({ resolution: 1 });
551cb0ef41Sopenharmony_ci  histogram.enable();
561cb0ef41Sopenharmony_ci  let m = 5;
571cb0ef41Sopenharmony_ci  function spinAWhile() {
581cb0ef41Sopenharmony_ci    sleep(1000);
591cb0ef41Sopenharmony_ci    if (--m > 0) {
601cb0ef41Sopenharmony_ci      setTimeout(spinAWhile, common.platformTimeout(500));
611cb0ef41Sopenharmony_ci    } else {
621cb0ef41Sopenharmony_ci      histogram.disable();
631cb0ef41Sopenharmony_ci      // The values are non-deterministic, so we just check that a value is
641cb0ef41Sopenharmony_ci      // present, as opposed to a specific value.
651cb0ef41Sopenharmony_ci      assert(histogram.min > 0);
661cb0ef41Sopenharmony_ci      assert(histogram.max > 0);
671cb0ef41Sopenharmony_ci      assert(histogram.stddev > 0);
681cb0ef41Sopenharmony_ci      assert(histogram.mean > 0);
691cb0ef41Sopenharmony_ci      assert(histogram.percentiles.size > 0);
701cb0ef41Sopenharmony_ci      for (let n = 1; n < 100; n = n + 0.1) {
711cb0ef41Sopenharmony_ci        assert(histogram.percentile(n) >= 0);
721cb0ef41Sopenharmony_ci      }
731cb0ef41Sopenharmony_ci      histogram.reset();
741cb0ef41Sopenharmony_ci      assert.strictEqual(histogram.min, 9223372036854776000);
751cb0ef41Sopenharmony_ci      assert.strictEqual(histogram.max, 0);
761cb0ef41Sopenharmony_ci      assert(Number.isNaN(histogram.stddev));
771cb0ef41Sopenharmony_ci      assert(Number.isNaN(histogram.mean));
781cb0ef41Sopenharmony_ci      assert.strictEqual(histogram.percentiles.size, 1);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci      ['a', false, {}, []].forEach((i) => {
811cb0ef41Sopenharmony_ci        assert.throws(
821cb0ef41Sopenharmony_ci          () => histogram.percentile(i),
831cb0ef41Sopenharmony_ci          {
841cb0ef41Sopenharmony_ci            name: 'TypeError',
851cb0ef41Sopenharmony_ci            code: 'ERR_INVALID_ARG_TYPE'
861cb0ef41Sopenharmony_ci          }
871cb0ef41Sopenharmony_ci        );
881cb0ef41Sopenharmony_ci      });
891cb0ef41Sopenharmony_ci      [-1, 0, 101, NaN].forEach((i) => {
901cb0ef41Sopenharmony_ci        assert.throws(
911cb0ef41Sopenharmony_ci          () => histogram.percentile(i),
921cb0ef41Sopenharmony_ci          {
931cb0ef41Sopenharmony_ci            name: 'RangeError',
941cb0ef41Sopenharmony_ci            code: 'ERR_INVALID_ARG_VALUE'
951cb0ef41Sopenharmony_ci          }
961cb0ef41Sopenharmony_ci        );
971cb0ef41Sopenharmony_ci      });
981cb0ef41Sopenharmony_ci    }
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci  spinAWhile();
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// Make sure that the histogram instances can be garbage-collected without
1041cb0ef41Sopenharmony_ci// and not just implictly destroyed when the Environment is torn down.
1051cb0ef41Sopenharmony_ciprocess.on('exit', global.gc);
106