• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4
5common.skipIfInspectorDisabled();
6common.skipIfWorker(); // https://github.com/nodejs/node/issues/22767
7
8const assert = require('assert');
9const { Session } = require('inspector');
10
11const session = new Session();
12
13function compareIgnoringOrder(array1, array2) {
14  const set = new Set(array1);
15  const test = set.size === array2.length && array2.every((el) => set.has(el));
16  assert.ok(test, `[${array1}] differs from [${array2}]`);
17}
18
19function post(message, data) {
20  return new Promise((resolve, reject) => {
21    session.post(message, data, (err, result) => {
22      if (err)
23        reject(new Error(JSON.stringify(err)));
24      else
25        resolve(result);
26    });
27  });
28}
29
30function generateTrace() {
31  return new Promise((resolve) => setTimeout(() => {
32    for (let i = 0; i < 1000000; i++) {
33      'test' + i; // eslint-disable-line no-unused-expressions
34    }
35    resolve();
36  }, 1));
37}
38
39async function test() {
40  // This interval ensures Node does not terminate till the test is finished.
41  // Inspector session does not keep the node process running (e.g. it does not
42  // have async handles on the main event loop). It is debatable whether this
43  // should be considered a bug, and there are no plans to fix it atm.
44  const interval = setInterval(() => {}, 5000);
45  session.connect();
46  let traceNotification = null;
47  let tracingComplete = false;
48  session.on('NodeTracing.dataCollected', (n) => traceNotification = n);
49  session.on('NodeTracing.tracingComplete', () => tracingComplete = true);
50  const { categories } = await post('NodeTracing.getCategories');
51  compareIgnoringOrder(['node', 'node.async', 'node.bootstrap', 'node.fs.sync',
52                        'node.perf', 'node.perf.usertiming',
53                        'node.perf.timerify', 'v8'],
54                       categories);
55
56  const traceConfig = { includedCategories: ['v8'] };
57  await post('NodeTracing.start', { traceConfig });
58
59  for (let i = 0; i < 5; i++)
60    await generateTrace();
61  JSON.stringify(await post('NodeTracing.stop', { traceConfig }));
62  session.disconnect();
63  assert(traceNotification.params.value.length > 0);
64  assert(tracingComplete);
65  clearInterval(interval);
66  console.log('Success');
67}
68
69test().then(common.mustCall());
70