• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5
6const {
7  performance,
8  PerformanceObserver
9} = require('perf_hooks');
10
11{
12  // Intentional non-op. Do not wrap in common.mustCall();
13  const n = performance.timerify(function noop() {});
14
15  const obs = new PerformanceObserver(common.mustCall((list) => {
16    const entries = list.getEntries();
17    const entry = entries[0];
18    assert(entry);
19    assert.strictEqual(entry.name, 'noop');
20    assert.strictEqual(entry.entryType, 'function');
21    assert.strictEqual(typeof entry.duration, 'number');
22    assert.strictEqual(typeof entry.startTime, 'number');
23    obs.disconnect();
24  }));
25  obs.observe({ entryTypes: ['function'] });
26  n();
27}
28
29{
30  // If the error throws, the error should just be bubbled up and the
31  // performance timeline entry will not be reported.
32  const obs = new PerformanceObserver(common.mustNotCall());
33  obs.observe({ entryTypes: ['function'] });
34  const n = performance.timerify(() => {
35    throw new Error('test');
36  });
37  assert.throws(() => n(), /^Error: test$/);
38  obs.disconnect();
39}
40
41{
42  class N {}
43  const n = performance.timerify(N);
44
45  const obs = new PerformanceObserver(common.mustCall((list) => {
46    const entries = list.getEntries();
47    const entry = entries[0];
48    assert.strictEqual(entry[0], 1);
49    assert.strictEqual(entry[1], 'abc');
50    assert(entry);
51    assert.strictEqual(entry.name, 'N');
52    assert.strictEqual(entry.entryType, 'function');
53    assert.strictEqual(typeof entry.duration, 'number');
54    assert.strictEqual(typeof entry.startTime, 'number');
55    obs.disconnect();
56  }));
57  obs.observe({ entryTypes: ['function'] });
58
59  new n(1, 'abc');
60}
61
62{
63  [1, {}, [], null, undefined, Infinity].forEach((input) => {
64    assert.throws(() => performance.timerify(input),
65                  {
66                    code: 'ERR_INVALID_ARG_TYPE',
67                    name: 'TypeError',
68                    message: /The "fn" argument must be of type function/
69                  });
70  });
71}
72
73// Function can only be wrapped once, also check length and name
74{
75  const m = (a, b = 1) => {};
76  const n = performance.timerify(m);
77  const o = performance.timerify(m);
78  const p = performance.timerify(n);
79  assert.strictEqual(n, o);
80  assert.strictEqual(n, p);
81  assert.strictEqual(n.length, m.length);
82  assert.strictEqual(n.name, 'timerified m');
83}
84