summaryrefslogtreecommitdiff
path: root/test/sequential/test-performance-eventloopdelay.js
blob: 7b9527b38639053cea2df7005c052cf3f0f1109d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Flags: --expose-gc
'use strict';

const common = require('../common');
const assert = require('assert');
const {
  monitorEventLoopDelay
} = require('perf_hooks');

{
  const histogram = monitorEventLoopDelay();
  assert(histogram);
  assert(histogram.enable());
  assert(!histogram.enable());
  histogram.reset();
  assert(histogram.disable());
  assert(!histogram.disable());
}

{
  [null, 'a', 1, false, Infinity].forEach((i) => {
    common.expectsError(
      () => monitorEventLoopDelay(i),
      {
        type: TypeError,
        code: 'ERR_INVALID_ARG_TYPE'
      }
    );
  });

  [null, 'a', false, {}, []].forEach((i) => {
    common.expectsError(
      () => monitorEventLoopDelay({ resolution: i }),
      {
        type: TypeError,
        code: 'ERR_INVALID_ARG_TYPE'
      }
    );
  });

  [-1, 0, Infinity].forEach((i) => {
    common.expectsError(
      () => monitorEventLoopDelay({ resolution: i }),
      {
        type: RangeError,
        code: 'ERR_INVALID_OPT_VALUE'
      }
    );
  });
}

{
  const histogram = monitorEventLoopDelay({ resolution: 1 });
  histogram.enable();
  let m = 5;
  function spinAWhile() {
    common.busyLoop(1000);
    if (--m > 0) {
      setTimeout(spinAWhile, common.platformTimeout(500));
    } else {
      histogram.disable();
      // The values are non-deterministic, so we just check that a value is
      // present, as opposed to a specific value.
      assert(histogram.min > 0);
      assert(histogram.max > 0);
      assert(histogram.stddev > 0);
      assert(histogram.mean > 0);
      assert(histogram.percentiles.size > 0);
      for (let n = 1; n < 100; n = n + 0.1) {
        assert(histogram.percentile(n) >= 0);
      }
      histogram.reset();
      assert.strictEqual(histogram.min, 9223372036854776000);
      assert.strictEqual(histogram.max, 0);
      assert(Number.isNaN(histogram.stddev));
      assert(Number.isNaN(histogram.mean));
      assert.strictEqual(histogram.percentiles.size, 1);

      ['a', false, {}, []].forEach((i) => {
        common.expectsError(
          () => histogram.percentile(i),
          {
            type: TypeError,
            code: 'ERR_INVALID_ARG_TYPE'
          }
        );
      });
      [-1, 0, 101].forEach((i) => {
        common.expectsError(
          () => histogram.percentile(i),
          {
            type: RangeError,
            code: 'ERR_INVALID_ARG_VALUE'
          }
        );
      });
    }
  }
  spinAWhile();
}

// Make sure that the histogram instances can be garbage-collected without
// and not just implictly destroyed when the Environment is torn down.
process.on('exit', global.gc);