summaryrefslogtreecommitdiff
path: root/test/parallel/test-performance-function.js
blob: 5f774d6c2adcf538f83c3526b0d61ddba746bac3 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';

const common = require('../common');
const assert = require('assert');

const {
  createHistogram,
  performance,
  PerformanceObserver
} = require('perf_hooks');

const {
  setTimeout: sleep
} = require('timers/promises');

{
  // Intentional non-op. Do not wrap in common.mustCall();
  const n = performance.timerify(function noop() {});

  const obs = new PerformanceObserver(common.mustCall((list) => {
    const entries = list.getEntries();
    const entry = entries[0];
    assert(entry);
    assert.strictEqual(entry.name, 'noop');
    assert.strictEqual(entry.entryType, 'function');
    assert.strictEqual(typeof entry.duration, 'number');
    assert.strictEqual(typeof entry.startTime, 'number');
    obs.disconnect();
  }));
  obs.observe({ entryTypes: ['function'] });
  n();
}

{
  // If the error throws, the error should just be bubbled up and the
  // performance timeline entry will not be reported.
  const obs = new PerformanceObserver(common.mustNotCall());
  obs.observe({ entryTypes: ['function'] });
  const n = performance.timerify(() => {
    throw new Error('test');
  });
  assert.throws(() => n(), /^Error: test$/);
  obs.disconnect();
}

{
  class N {}
  const n = performance.timerify(N);

  const obs = new PerformanceObserver(common.mustCall((list) => {
    const entries = list.getEntries();
    const entry = entries[0];
    assert.strictEqual(entry[0], 1);
    assert.strictEqual(entry[1], 'abc');
    assert(entry);
    assert.strictEqual(entry.name, 'N');
    assert.strictEqual(entry.entryType, 'function');
    assert.strictEqual(typeof entry.duration, 'number');
    assert.strictEqual(typeof entry.startTime, 'number');
    obs.disconnect();
  }));
  obs.observe({ entryTypes: ['function'] });

  new n(1, 'abc');
}

{
  [1, {}, [], null, undefined, Infinity].forEach((input) => {
    assert.throws(() => performance.timerify(input),
                  {
                    code: 'ERR_INVALID_ARG_TYPE',
                    name: 'TypeError',
                    message: /The "fn" argument must be of type function/
                  });
  });
}

// Function can be wrapped many times, also check length and name
{
  const m = (a, b = 1) => {};
  const n = performance.timerify(m);
  const o = performance.timerify(m);
  const p = performance.timerify(n);
  assert.notStrictEqual(n, o);
  assert.notStrictEqual(n, p);
  assert.notStrictEqual(o, p);
  assert.strictEqual(n.length, m.length);
  assert.strictEqual(n.name, 'timerified m');
  assert.strictEqual(p.name, 'timerified timerified m');
}

(async () => {
  const histogram = createHistogram();
  const m = (a, b = 1) => {};
  const n = performance.timerify(m, { histogram });
  assert.strictEqual(histogram.max, 0);
  for (let i = 0; i < 10; i++) {
    n();
    await sleep(10);
  }
  assert.notStrictEqual(histogram.max, 0);
  [1, '', {}, [], false].forEach((histogram) => {
    assert.throws(() => performance.timerify(m, { histogram }), {
      code: 'ERR_INVALID_ARG_TYPE'
    });
  });
})().then(common.mustCall());

(async () => {
  const histogram = createHistogram();
  const m = async (a, b = 1) => {
    await sleep(10);
  };
  const n = performance.timerify(m, { histogram });
  assert.strictEqual(histogram.max, 0);
  for (let i = 0; i < 10; i++) {
    await n();
  }
  assert.notStrictEqual(histogram.max, 0);
  [1, '', {}, [], false].forEach((histogram) => {
    assert.throws(() => performance.timerify(m, { histogram }), {
      code: 'ERR_INVALID_ARG_TYPE'
    });
  });
})().then(common.mustCall());

// Regression tests for https://github.com/nodejs/node/issues/40623
{
  assert.strictEqual(performance.timerify(function func() {
    return 1;
  })(), 1);
  assert.strictEqual(performance.timerify(function() {
    return 1;
  })(), 1);
  assert.strictEqual(performance.timerify(() => {
    return 1;
  })(), 1);
  class C {}
  const wrap = performance.timerify(C);
  assert.ok(new wrap() instanceof C);
  assert.throws(() => wrap(), {
    name: 'TypeError',
  });
}