|
| 1 | +/* eslint-disable node-core/require-common-first, node-core/required-modules */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const common = require('./'); |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const assert = require('assert'); |
| 9 | + |
| 10 | +function getCpuProfiles(dir) { |
| 11 | + const list = fs.readdirSync(dir); |
| 12 | + return list |
| 13 | + .filter((file) => file.endsWith('.cpuprofile')) |
| 14 | + .map((file) => path.join(dir, file)); |
| 15 | +} |
| 16 | + |
| 17 | +function getFrames(output, file, suffix) { |
| 18 | + const data = fs.readFileSync(file, 'utf8'); |
| 19 | + const profile = JSON.parse(data); |
| 20 | + const frames = profile.nodes.filter((i) => { |
| 21 | + const frame = i.callFrame; |
| 22 | + return frame.url.endsWith(suffix); |
| 23 | + }); |
| 24 | + return { frames, nodes: profile.nodes }; |
| 25 | +} |
| 26 | + |
| 27 | +function verifyFrames(output, file, suffix) { |
| 28 | + const { frames, nodes } = getFrames(output, file, suffix); |
| 29 | + if (frames.length === 0) { |
| 30 | + // Show native debug output and the profile for debugging. |
| 31 | + console.log(output.stderr.toString()); |
| 32 | + console.log(nodes); |
| 33 | + } |
| 34 | + assert.notDeepStrictEqual(frames, []); |
| 35 | +} |
| 36 | + |
| 37 | +let FIB = 30; |
| 38 | +// This is based on emperial values - in the CI, on Windows the program |
| 39 | +// tend to finish too fast then we won't be able to see the profiled script |
| 40 | +// in the samples, so we need to bump the values a bit. On slower platforms |
| 41 | +// like the Pis it could take more time to complete, we need to use a |
| 42 | +// smaller value so the test would not time out. |
| 43 | +if (common.isWindows) { |
| 44 | + FIB = 40; |
| 45 | +} |
| 46 | + |
| 47 | +// We need to set --cpu-interval to a smaller value to make sure we can |
| 48 | +// find our workload in the samples. 50us should be a small enough sampling |
| 49 | +// interval for this. |
| 50 | +const kCpuProfInterval = 50; |
| 51 | +const env = { |
| 52 | + ...process.env, |
| 53 | + FIB, |
| 54 | + NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER' |
| 55 | +}; |
| 56 | + |
| 57 | +module.exports = { |
| 58 | + getCpuProfiles, |
| 59 | + kCpuProfInterval, |
| 60 | + env, |
| 61 | + getFrames, |
| 62 | + verifyFrames |
| 63 | +}; |
0 commit comments