• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4common.skipIfInspectorDisabled();
5
6const assert = require('assert');
7const inspector = require('inspector');
8const stream = require('stream');
9const { Worker, workerData } = require('worker_threads');
10
11const session = new inspector.Session();
12session.connect();
13session.post('HeapProfiler.enable');
14session.post('HeapProfiler.startTrackingHeapObjects',
15             { trackAllocations: true });
16
17// Perform some silly heap allocations for the next 100 ms.
18const interval = setInterval(() => {
19  new stream.PassThrough().end('abc').on('data', common.mustCall());
20}, 1);
21
22setTimeout(() => {
23  clearInterval(interval);
24
25  // Once the main test is done, we re-run it from inside a Worker thread
26  // and stop early, as that is a good way to make sure the timer handles
27  // internally created by the inspector are cleaned up properly.
28  if (workerData === 'stopEarly')
29    process.exit();
30
31  let data = '';
32  session.on('HeapProfiler.addHeapSnapshotChunk',
33             common.mustCallAtLeast((event) => {
34               data += event.params.chunk;
35             }));
36
37  // TODO(addaleax): Using `{ reportProgress: true }` crashes the process
38  // because the progress indication event would mean calling into JS while
39  // a heap snapshot is being taken, which is forbidden.
40  // What can we do about that?
41  session.post('HeapProfiler.stopTrackingHeapObjects');
42
43  assert(data.includes('PassThrough'), data);
44
45  new Worker(__filename, { workerData: 'stopEarly' });
46}, 100);
47