1'use strict'; 2 3require('./'); 4const fs = require('fs'); 5const path = require('path'); 6const assert = require('assert'); 7 8function getCpuProfiles(dir) { 9 const list = fs.readdirSync(dir); 10 return list 11 .filter((file) => file.endsWith('.cpuprofile')) 12 .map((file) => path.join(dir, file)); 13} 14 15function getFrames(file, suffix) { 16 const data = fs.readFileSync(file, 'utf8'); 17 const profile = JSON.parse(data); 18 const frames = profile.nodes.filter((i) => { 19 const frame = i.callFrame; 20 return frame.url.endsWith(suffix); 21 }); 22 return { frames, nodes: profile.nodes }; 23} 24 25function verifyFrames(output, file, suffix) { 26 const { frames, nodes } = getFrames(file, suffix); 27 if (frames.length === 0) { 28 // Show native debug output and the profile for debugging. 29 console.log(output.stderr.toString()); 30 console.log(nodes); 31 } 32 assert.notDeepStrictEqual(frames, []); 33} 34 35// We need to set --cpu-interval to a smaller value to make sure we can 36// find our workload in the samples. 50us should be a small enough sampling 37// interval for this. 38const kCpuProfInterval = 50; 39const env = { 40 ...process.env, 41 NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER' 42}; 43 44module.exports = { 45 getCpuProfiles, 46 kCpuProfInterval, 47 env, 48 getFrames, 49 verifyFrames 50}; 51