/
v8-profiler-next.js
59 lines (54 loc) · 1.79 KB
/
v8-profiler-next.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
52
53
54
55
56
57
58
59
const { Worker } = require('worker_threads');
const v8Profiler = require('v8-profiler-next');
const fs = require('fs');
const worker = require('worker_threads');
const path = require('path');
const epoch = (new Date()).getTime();
const title = `${process.pid}-${worker.threadId}-${epoch}`;
v8Profiler.startProfiling(title, true);
setTimeout(() => {
const profile = v8Profiler.stopProfiling(title);
profile.delete();
const cpuProfilePath = path.join(`node-${title}.cpuprofile`);
fs.writeFile(cpuProfilePath, JSON.stringify(profile), (err) => {
if (err) {
console.error(`Error saving profiling file ${cpuProfilePath}, ${err}`);
} else {
console.info(`Saved profiling file ${cpuProfilePath}`);
}
});
}, 9 * 1000);
function mainSpin() {
const start = Date.now();
while (Date.now() - start < 10000);
}
const spin = `
'use strict';
const v8Profiler = require('v8-profiler-next');
const fs = require('fs');
const worker = require('worker_threads');
const path = require('path');
const epoch = (new Date()).getTime();
const title = \`\${process.pid}-\${worker.threadId}-\${epoch}\`;
// ex. 5 mins cpu profile
v8Profiler.startProfiling(title, true);
setTimeout(() => {
const profile = v8Profiler.stopProfiling(title);
profile.delete();
const cpuProfilePath = path.join(\`node-\${title}.cpuprofile\`);
fs.writeFile(cpuProfilePath, JSON.stringify(profile), (err) => {
if (err) {
console.error(\`Error saving profiling file \${cpuProfilePath}, \${err}\`);
} else {
console.info(\`Saved profiling file \${cpuProfilePath}\`);
}
});
}, 9 * 1000);
function workerSpin() {
const start = Date.now();
while (Date.now() - start < 10000);
}
workerSpin();
`;
new Worker(spin, { eval: true });
mainSpin();