1'use strict'; 2 3const assert = require('assert'); 4const fs = require('fs'); 5const path = require('path'); 6 7function getHeapProfiles(dir) { 8 const list = fs.readdirSync(dir); 9 return list 10 .filter((file) => file.endsWith('.heapprofile')) 11 .map((file) => path.join(dir, file)); 12} 13 14function findFirstFrameInNode(root, func) { 15 const first = root.children.find( 16 (child) => child.callFrame.functionName === func, 17 ); 18 if (first) { 19 return first; 20 } 21 for (const child of root.children) { 22 const first = findFirstFrameInNode(child, func); 23 if (first) { 24 return first; 25 } 26 } 27 return undefined; 28} 29 30function findFirstFrame(file, func) { 31 const data = fs.readFileSync(file, 'utf8'); 32 const profile = JSON.parse(data); 33 const first = findFirstFrameInNode(profile.head, func); 34 return { frame: first, roots: profile.head.children }; 35} 36 37function verifyFrames(output, file, func) { 38 const { frame, roots } = findFirstFrame(file, func); 39 if (!frame) { 40 // Show native debug output and the profile for debugging. 41 console.log(output.stderr.toString()); 42 console.log(roots); 43 } 44 assert.notStrictEqual(frame, undefined); 45} 46 47// We need to set --heap-prof-interval to a small enough value to make 48// sure we can find our workload in the samples, so we need to set 49// TEST_ALLOCATION > kHeapProfInterval. 50const kHeapProfInterval = 128; 51const TEST_ALLOCATION = kHeapProfInterval * 2; 52 53const env = { 54 ...process.env, 55 TEST_ALLOCATION, 56 NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER', 57}; 58 59// TODO(joyeecheung): share the fixutres with v8 coverage tests 60module.exports = { 61 getHeapProfiles, 62 verifyFrames, 63 findFirstFrame, 64 kHeapProfInterval, 65 TEST_ALLOCATION, 66 env, 67}; 68