-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-inspector.js
51 lines (44 loc) · 1.25 KB
/
node-inspector.js
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
const { Worker } = require('worker_threads');
const inspector = require('inspector');
const fs = require('fs');
const session = new inspector.Session();
session.connect();
session.post('Profiler.enable', () => {
session.post('Profiler.start', () => {
});
});
function mainSpin() {
const start = Date.now();
while (Date.now() - start < 10000);
session.post('Profiler.stop', (err, { profile }) => {
// Write profile to disk, upload, etc.
if (!err) {
fs.writeFileSync('./profile-main.cpuprofile', JSON.stringify(profile));
}
session.post('Profiler.disable', () => {
});
});
}
const spin = `
function workerSpin() {
const inspector = require('inspector');
const fs = require('fs');
const session = new inspector.Session();
session.connect();
session.post('Profiler.enable', () => {
session.post('Profiler.start', () => {
});
});
const start = Date.now();
while (Date.now() - start < 10000);
session.post('Profiler.stop', (err, { profile }) => {
// Write profile to disk, upload, etc.
if (!err) {
fs.writeFileSync('./profile-worker.cpuprofile', JSON.stringify(profile));
}
});
}
workerSpin();
`;
new Worker(spin, { eval: true });
mainSpin();