1'use strict'; 2 3// This test is to ensure that --diagnostic-dir does not change the directory 4// for --cpu-prof when --cpu-prof-dir is specified 5 6const common = require('../common'); 7const fixtures = require('../common/fixtures'); 8common.skipIfInspectorDisabled(); 9 10const assert = require('assert'); 11const fs = require('fs'); 12const path = require('path'); 13const { spawnSync } = require('child_process'); 14 15const tmpdir = require('../common/tmpdir'); 16 17function findFirstFrameInNode(root, func) { 18 const first = root.children.find( 19 (child) => child.callFrame.functionName === func 20 ); 21 if (first) { 22 return first; 23 } 24 for (const child of root.children) { 25 const first = findFirstFrameInNode(child, func); 26 if (first) { 27 return first; 28 } 29 } 30 return undefined; 31} 32 33function findFirstFrame(file, func) { 34 const data = fs.readFileSync(file, 'utf8'); 35 const profile = JSON.parse(data); 36 const first = findFirstFrameInNode(profile.head, func); 37 return { frame: first, roots: profile.head.children }; 38} 39 40function verifyFrames(output, file, func) { 41 const { frame, roots } = findFirstFrame(file, func); 42 if (!frame) { 43 // Show native debug output and the profile for debugging. 44 console.log(output.stderr.toString()); 45 console.log(roots); 46 } 47 assert.notDeepStrictEqual(frame, undefined); 48} 49 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 59function getHeapProfiles(dir) { 60 const list = fs.readdirSync(dir); 61 return list 62 .filter((file) => file.endsWith('.heapprofile')) 63 .map((file) => path.join(dir, file)); 64} 65 66// Test --diagnostic-dir changes the default for --cpu-prof 67{ 68 tmpdir.refresh(); 69 const dir = path.join(tmpdir.path, 'prof'); 70 const output = spawnSync(process.execPath, [ 71 '--heap-prof', 72 '--diagnostic-dir', 73 dir, 74 '--heap-prof-interval', 75 kHeapProfInterval, 76 fixtures.path('workload', 'allocation.js'), 77 ], { 78 cwd: tmpdir.path, 79 env 80 }); 81 if (output.status !== 0) { 82 console.log(output.stderr.toString()); 83 } 84 assert.strictEqual(output.status, 0); 85 assert(fs.existsSync(dir)); 86 const profiles = getHeapProfiles(dir); 87 assert.strictEqual(profiles.length, 1); 88 verifyFrames(output, profiles[0], 'runAllocation'); 89} 90 91// Test --heap-prof-dir overwrites --diagnostic-dir 92{ 93 tmpdir.refresh(); 94 const dir = path.join(tmpdir.path, 'diag'); 95 const dir2 = path.join(tmpdir.path, 'prof'); 96 const output = spawnSync(process.execPath, [ 97 '--heap-prof', 98 '--heap-prof-interval', 99 kHeapProfInterval, 100 '--diagnostic-dir', 101 dir, 102 '--heap-prof-dir', 103 dir2, 104 fixtures.path('workload', 'allocation.js'), 105 ], { 106 cwd: tmpdir.path, 107 env 108 }); 109 if (output.status !== 0) { 110 console.log(output.stderr.toString()); 111 } 112 assert.strictEqual(output.status, 0); 113 assert(fs.existsSync(dir2)); 114 const profiles = getHeapProfiles(dir2); 115 assert.strictEqual(profiles.length, 1); 116 verifyFrames(output, profiles[0], 'runAllocation'); 117} 118