summaryrefslogtreecommitdiff
path: root/test/parallel/test-trace-events-dynamic-enable.js
blob: 24a186c15a85c745e87591a169d0325c310dfc6e (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
'use strict';

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

common.skipIfInspectorDisabled();
common.skipIfWorker(); // https://github.com/nodejs/node/issues/22767

const assert = require('assert');
const { performance } = require('perf_hooks');
const { Session } = require('inspector');

const session = new Session();

function post(message, data) {
  return new Promise((resolve, reject) => {
    session.post(message, data, (err, result) => {
      if (err)
        reject(new Error(JSON.stringify(err)));
      else
        resolve(result);
    });
  });
}

async function test() {
  session.connect();

  let traceNotification = null;
  let tracingComplete = false;
  session.on('NodeTracing.dataCollected', (n) => traceNotification = n);
  session.on('NodeTracing.tracingComplete', () => tracingComplete = true);

  // Generate a node.perf event before tracing is enabled.
  performance.mark('mark1');

  const traceConfig = { includedCategories: ['node.perf'] };
  await post('NodeTracing.start', { traceConfig });

  // Generate a node.perf event after tracing is enabled. This should be the
  // mark event captured.
  performance.mark('mark2');

  await post('NodeTracing.stop', { traceConfig });

  performance.mark('mark3');

  session.disconnect();

  assert.ok(tracingComplete);
  assert.ok(traceNotification);
  assert.ok(traceNotification.data && traceNotification.data.value);

  const events = traceNotification.data.value;
  const marks = events.filter((t) => null !== /node\.perf\.usertim/.exec(t.cat));
  assert.strictEqual(marks.length, 1);
  assert.strictEqual(marks[0].name, 'mark2');
}

test();