(stream = getHeapSnapshot())
| 20 | const { getHeapSnapshot } = require('v8'); |
| 21 | |
| 22 | function createJSHeapSnapshot(stream = getHeapSnapshot()) { |
| 23 | stream.pause(); |
| 24 | stream.read(0); |
| 25 | const dump = JSON.parse(stream.read(stream.readableLength)); |
| 26 | const meta = dump.snapshot.meta; |
| 27 | |
| 28 | const nodes = |
| 29 | readHeapInfo(dump.nodes, meta.node_fields, meta.node_types, dump.strings); |
| 30 | const edges = |
| 31 | readHeapInfo(dump.edges, meta.edge_fields, meta.edge_types, dump.strings); |
| 32 | |
| 33 | for (const node of nodes) { |
| 34 | node.incomingEdges = []; |
| 35 | node.outgoingEdges = []; |
| 36 | } |
| 37 | |
| 38 | let fromNodeIndex = 0; |
| 39 | let edgeIndex = 0; |
| 40 | for (const { type, name_or_index, to_node } of edges) { |
| 41 | while (edgeIndex === nodes[fromNodeIndex].edge_count) { |
| 42 | edgeIndex = 0; |
| 43 | fromNodeIndex++; |
| 44 | } |
| 45 | const toNode = nodes[to_node / meta.node_fields.length]; |
| 46 | const fromNode = nodes[fromNodeIndex]; |
| 47 | const edge = { |
| 48 | type, |
| 49 | to: toNode, |
| 50 | from: fromNode, |
| 51 | name: typeof name_or_index === 'string' ? name_or_index : null, |
| 52 | }; |
| 53 | toNode.incomingEdges.push(edge); |
| 54 | fromNode.outgoingEdges.push(edge); |
| 55 | edgeIndex++; |
| 56 | } |
| 57 | |
| 58 | for (const node of nodes) { |
| 59 | assert.strictEqual(node.edge_count, node.outgoingEdges.length, |
| 60 | `${node.edge_count} !== ${node.outgoingEdges.length}`); |
| 61 | } |
| 62 | return nodes; |
| 63 | } |
| 64 | |
| 65 | function readHeapInfo(raw, fields, types, strings) { |
| 66 | const items = []; |
no test coverage detected
searching dependent graphs…