summaryrefslogtreecommitdiff
path: root/test/common/cpu-prof.js
blob: 78e746082b49907ab302f66652d8c1685ef15eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-disable node-core/require-common-first, node-core/required-modules */

'use strict';

const common = require('./');
const fs = require('fs');
const path = require('path');
const assert = require('assert');

function getCpuProfiles(dir) {
  const list = fs.readdirSync(dir);
  return list
    .filter((file) => file.endsWith('.cpuprofile'))
    .map((file) => path.join(dir, file));
}

function getFrames(output, file, suffix) {
  const data = fs.readFileSync(file, 'utf8');
  const profile = JSON.parse(data);
  const frames = profile.nodes.filter((i) => {
    const frame = i.callFrame;
    return frame.url.endsWith(suffix);
  });
  return { frames, nodes: profile.nodes };
}

function verifyFrames(output, file, suffix) {
  const { frames, nodes } = getFrames(output, file, suffix);
  if (frames.length === 0) {
    // Show native debug output and the profile for debugging.
    console.log(output.stderr.toString());
    console.log(nodes);
  }
  assert.notDeepStrictEqual(frames, []);
}

let FIB = 30;
// This is based on emperial values - in the CI, on Windows the program
// tend to finish too fast then we won't be able to see the profiled script
// in the samples, so we need to bump the values a bit. On slower platforms
// like the Pis it could take more time to complete, we need to use a
// smaller value so the test would not time out.
if (common.isWindows) {
  FIB = 40;
}

// We need to set --cpu-interval to a smaller value to make sure we can
// find our workload in the samples. 50us should be a small enough sampling
// interval for this.
const kCpuProfInterval = 50;
const env = {
  ...process.env,
  FIB,
  NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER'
};

module.exports = {
  getCpuProfiles,
  kCpuProfInterval,
  env,
  getFrames,
  verifyFrames
};