test: split test-cpu-prof.js · nodejs/node@2353c63 · GitHub
Skip to content

Commit 2353c63

Browse files
joyeecheungBridgeAR
authored andcommitted
test: split test-cpu-prof.js
Split test-cpu-prof.js into multiple files for different test cases so it's easier to find the problematic one if it flakes. Also move the split tests into parallel. PR-URL: #28170 Refs: #27611 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 9382b3b commit 2353c63

13 files changed

Lines changed: 572 additions & 367 deletions

test/common/cpu-prof.js

Lines changed: 63 additions & 0 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// Test --cpu-prof without --cpu-prof-interval. Here we just verify that
4+
// we manage to generate a profile since it's hard to tell whether we
5+
// can sample our target function with the default sampling rate across
6+
// different platforms and machine configurations.
7+
8+
const common = require('../common');
9+
const fixtures = require('../common/fixtures');
10+
common.skipIfInspectorDisabled();
11+
12+
const assert = require('assert');
13+
const { spawnSync } = require('child_process');
14+
15+
const tmpdir = require('../common/tmpdir');
16+
const {
17+
getCpuProfiles,
18+
env
19+
} = require('../common/cpu-prof');
20+
21+
{
22+
tmpdir.refresh();
23+
const output = spawnSync(process.execPath, [
24+
'--cpu-prof',
25+
fixtures.path('workload', 'fibonacci.js'),
26+
], {
27+
cwd: tmpdir.path,
28+
env
29+
});
30+
if (output.status !== 0) {
31+
console.log(output.stderr.toString());
32+
}
33+
assert.strictEqual(output.status, 0);
34+
const profiles = getCpuProfiles(tmpdir.path);
35+
assert.strictEqual(profiles.length, 1);
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
// This tests that relative --cpu-prof-dir works.
4+
5+
const common = require('../common');
6+
const fixtures = require('../common/fixtures');
7+
common.skipIfInspectorDisabled();
8+
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { spawnSync } = require('child_process');
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const {
16+
getCpuProfiles,
17+
kCpuProfInterval,
18+
env,
19+
verifyFrames
20+
} = require('../common/cpu-prof');
21+
22+
// relative --cpu-prof-dir
23+
{
24+
tmpdir.refresh();
25+
const dir = path.join(tmpdir.path, 'prof');
26+
const output = spawnSync(process.execPath, [
27+
'--cpu-prof',
28+
'--cpu-prof-interval',
29+
kCpuProfInterval,
30+
'--cpu-prof-dir',
31+
dir,
32+
fixtures.path('workload', 'fibonacci.js'),
33+
], {
34+
cwd: tmpdir.path,
35+
env
36+
});
37+
if (output.status !== 0) {
38+
console.log(output.stderr.toString());
39+
}
40+
assert.strictEqual(output.status, 0);
41+
assert(fs.existsSync(dir));
42+
const profiles = getCpuProfiles(dir);
43+
assert.strictEqual(profiles.length, 1);
44+
verifyFrames(output, profiles[0], 'fibonacci.js');
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
// This tests that --cpu-prof-dir and --cpu-prof-name works together.
4+
5+
const common = require('../common');
6+
const fixtures = require('../common/fixtures');
7+
common.skipIfInspectorDisabled();
8+
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { spawnSync } = require('child_process');
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const {
16+
getCpuProfiles,
17+
kCpuProfInterval,
18+
env,
19+
verifyFrames
20+
} = require('../common/cpu-prof');
21+
22+
{
23+
tmpdir.refresh();
24+
const dir = path.join(tmpdir.path, 'prof');
25+
const file = path.join(dir, 'test.cpuprofile');
26+
const output = spawnSync(process.execPath, [
27+
'--cpu-prof',
28+
'--cpu-prof-interval',
29+
kCpuProfInterval,
30+
'--cpu-prof-name',
31+
'test.cpuprofile',
32+
'--cpu-prof-dir',
33+
dir,
34+
fixtures.path('workload', 'fibonacci.js'),
35+
], {
36+
cwd: tmpdir.path,
37+
env
38+
});
39+
if (output.status !== 0) {
40+
console.log(output.stderr.toString());
41+
}
42+
assert.strictEqual(output.status, 0);
43+
assert(fs.existsSync(dir));
44+
const profiles = getCpuProfiles(dir);
45+
assert.deepStrictEqual(profiles, [file]);
46+
verifyFrames(output, file, 'fibonacci.js');
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
// This tests that relative --cpu-prof-dir works.
4+
5+
const common = require('../common');
6+
const fixtures = require('../common/fixtures');
7+
common.skipIfInspectorDisabled();
8+
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { spawnSync } = require('child_process');
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const {
16+
getCpuProfiles,
17+
kCpuProfInterval,
18+
env,
19+
verifyFrames
20+
} = require('../common/cpu-prof');
21+
22+
// relative --cpu-prof-dir
23+
{
24+
tmpdir.refresh();
25+
const output = spawnSync(process.execPath, [
26+
'--cpu-prof',
27+
'--cpu-prof-interval',
28+
kCpuProfInterval,
29+
'--cpu-prof-dir',
30+
'prof',
31+
fixtures.path('workload', 'fibonacci.js'),
32+
], {
33+
cwd: tmpdir.path,
34+
env
35+
});
36+
if (output.status !== 0) {
37+
console.log(output.stderr.toString());
38+
}
39+
assert.strictEqual(output.status, 0);
40+
const dir = path.join(tmpdir.path, 'prof');
41+
assert(fs.existsSync(dir));
42+
const profiles = getCpuProfiles(dir);
43+
assert.strictEqual(profiles.length, 1);
44+
verifyFrames(output, profiles[0], 'fibonacci.js');
45+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
// This tests that --cpu-prof-dir works for workers.
4+
5+
const common = require('../common');
6+
const fixtures = require('../common/fixtures');
7+
common.skipIfInspectorDisabled();
8+
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { spawnSync } = require('child_process');
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const {
16+
getCpuProfiles,
17+
kCpuProfInterval,
18+
env,
19+
getFrames
20+
} = require('../common/cpu-prof');
21+
22+
// --cpu-prof-dir with worker
23+
{
24+
tmpdir.refresh();
25+
const output = spawnSync(process.execPath, [
26+
'--cpu-prof-interval',
27+
kCpuProfInterval,
28+
'--cpu-prof-dir',
29+
'prof',
30+
'--cpu-prof',
31+
fixtures.path('workload', 'fibonacci-worker.js'),
32+
], {
33+
cwd: tmpdir.path,
34+
env
35+
});
36+
if (output.status !== 0) {
37+
console.log(output.stderr.toString());
38+
}
39+
assert.strictEqual(output.status, 0);
40+
const dir = path.join(tmpdir.path, 'prof');
41+
assert(fs.existsSync(dir));
42+
const profiles = getCpuProfiles(dir);
43+
assert.strictEqual(profiles.length, 2);
44+
const profile1 = getFrames(output, profiles[0], 'fibonacci.js');
45+
const profile2 = getFrames(output, profiles[1], 'fibonacci.js');
46+
if (profile1.frames.length === 0 && profile2.frames.length === 0) {
47+
// Show native debug output and the profile for debugging.
48+
console.log(output.stderr.toString());
49+
console.log('CPU path: ', profiles[0]);
50+
console.log(profile1.nodes);
51+
console.log('CPU path: ', profiles[1]);
52+
console.log(profile2.nodes);
53+
}
54+
assert(profile1.frames.length > 0 || profile2.frames.length > 0);
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
// This tests that --cpu-prof generates CPU profile when event
4+
// loop is drained.
5+
// TODO(joyeecheung): share the fixtures with v8 coverage tests
6+
7+
const common = require('../common');
8+
const fixtures = require('../common/fixtures');
9+
common.skipIfInspectorDisabled();
10+
11+
const assert = require('assert');
12+
const { spawnSync } = require('child_process');
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const {
16+
getCpuProfiles,
17+
kCpuProfInterval,
18+
env,
19+
verifyFrames
20+
} = require('../common/cpu-prof');
21+
22+
{
23+
tmpdir.refresh();
24+
const output = spawnSync(process.execPath, [
25+
'--cpu-prof',
26+
'--cpu-prof-interval',
27+
kCpuProfInterval,
28+
fixtures.path('workload', 'fibonacci.js'),
29+
], {
30+
cwd: tmpdir.path,
31+
env
32+
});
33+
if (output.status !== 0) {
34+
console.log(output.stderr.toString());
35+
}
36+
assert.strictEqual(output.status, 0);
37+
const profiles = getCpuProfiles(tmpdir.path);
38+
assert.strictEqual(profiles.length, 1);
39+
verifyFrames(output, profiles[0], 'fibonacci.js');
40+
}
Lines changed: 39 additions & 0 deletions

0 commit comments

Comments
 (0)