|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | +common.skipIfInspectorDisabled(); |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const inspector = require('inspector'); |
| 8 | +const stream = require('stream'); |
| 9 | +const { Worker, workerData } = require('worker_threads'); |
| 10 | + |
| 11 | +const session = new inspector.Session(); |
| 12 | +session.connect(); |
| 13 | +session.post('HeapProfiler.enable'); |
| 14 | +session.post('HeapProfiler.startTrackingHeapObjects', |
| 15 | + { trackAllocations: true }); |
| 16 | + |
| 17 | +// Perform some silly heap allocations for the next 100 ms. |
| 18 | +const interval = setInterval(() => { |
| 19 | + new stream.PassThrough().end('abc').on('data', common.mustCall()); |
| 20 | +}, 1); |
| 21 | + |
| 22 | +setTimeout(() => { |
| 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); |
0 commit comments