v8-profiler-next
Description
v8-profiler-next provides node bindings for the v8 profiler.
I. Quick Start
- Compatibility
- node version: v4.x ~ v21.x
- platform: mac, linux, windows
This module can also be used in worker_threads
.
take cpu profile
'use strict';
const fs = require('fs');
const v8Profiler = require('v8-profiler-next');
const title = 'good-name';
v8Profiler.setGenerateType(1);
v8Profiler.startProfiling(title, true);
setTimeout(() => {
const profile = v8Profiler.stopProfiling(title);
profile.export(function (error, result) {
fs.writeFileSync(`${title}.cpuprofile`, result);
profile.delete();
});
}, 5 * 60 * 1000);
Get .cpuprofile
in worker_threads
:
'use strict';
const fs = require('fs');
const path = require('path');
const v8Profiler = require('v8-profiler-next');
const workerThreads = require('worker_threads');
v8Profiler.setGenerateType(1);
if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});
v8Profiler.startProfiling('main', true);
w.once('exit', code => {
const profile = v8Profiler.stopProfiling('main');
const mainProfile = path.join(__dirname, 'main.cpuprofile');
fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
fs.writeFileSync(mainProfile, JSON.stringify(profile));
});
} else {
v8Profiler.startProfiling('worker_threads', true);
const start = Date.now();
while (Date.now() - start < 2000) { }
const profile = v8Profiler.stopProfiling('worker_threads');
const workerProfile = path.join(__dirname, 'worker_threads.cpuprofile');
fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
fs.writeFileSync(workerProfile, JSON.stringify(profile));
}
take heapsnapshot
'use strict';
const v8Profiler = require('v8-profiler-next');
const snapshot = v8Profiler.takeSnapshot();
snapshot.export(function (error, result) {
if (error){
console.error(error);
return;
}
console.log(result);
snapshot.delete();
});
const transform = snapshot.export();
transform.pipe(process.stdout);
transform.on('finish', snapshot.delete.bind(snapshot));
Get .heapsnapshot
in worker_threads
:
'use strict';
const fs = require('fs');
const path = require('path');
const v8Profiler = require('v8-profiler-next');
const workerThreads = require('worker_threads');
function createSnapshot(filename) {
const snapshot = v8Profiler.takeSnapshot();
const file = path.join(__dirname, filename);
const transform = snapshot.export();
transform.pipe(fs.createWriteStream(file));
transform.on('finish', snapshot.delete.bind(snapshot));
}
if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});
createSnapshot('main.heapsnapshot');
} else {
const start = Date.now();
const array = [];
while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }
createSnapshot('worker_threads.heapsnapshot');
}
take allocation profile
Attention: If node version < v12.x, please use sampling heap profiling alone without cpu profiling or taking snapshot.
'use strict';
const v8Profiler = require('v8-profiler-next');
const arraytest = [];
setInterval(() => {
arraytest.push(new Array(1e2).fill('*').join());
}, 20);
v8Profiler.startSamplingHeapProfiling();
setTimeout(() => {
const profile = v8Profiler.stopSamplingHeapProfiling();
require('fs').writeFileSync('./shf.heapprofile', JSON.stringify(profile));
console.log(profile);
}, 60 * 1000);
Get .heapprofile
in worker_threads
:
'use strict';
const fs = require('fs');
const path = require('path');
const v8Profiler = require('v8-profiler-next');
const workerThreads = require('worker_threads');
if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});
v8Profiler.startSamplingHeapProfiling();
w.once('exit', code => {
const profile = v8Profiler.stopSamplingHeapProfiling();
const mainProfile = path.join(__dirname, 'main.heapprofile');
fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
fs.writeFileSync(mainProfile, JSON.stringify(profile));
});
} else {
v8Profiler.startSamplingHeapProfiling();
const start = Date.now();
const array = [];
while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }
const profile = v8Profiler.stopSamplingHeapProfiling();
const workerProfile = path.join(__dirname, 'worker_threads.heapprofile');
fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
fs.writeFileSync(workerProfile, JSON.stringify(profile));
}
II. License
MIT License
Copyright (c) 2018 team of v8-profiler, hyj1991