Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Simple userland CPU profiler using v8-profiler
* Usage: require('[path_to]/CpuProfiler').init('datadir')
*
* @module CpuProfiler
* @type {exports}
*/
var fs = require('fs');
var profiler = require('v8-profiler');
var _datadir = null;
/**
* Init and schedule profiler runs
*
* @param datadir Folder to save the data to
*/
module.exports.init = function (datadir) {
_datadir = datadir;
setInterval(startProfiling, 30 * 1000);
};
/**
* Starts profiling and schedules its end
*/
function startProfiling() {
var stamp = Date.now();
var id = 'profile-' + stamp;
// Use stdout directly to bypass eventloop
fs.writeSync(1, 'Start profiler with Id [' + id + ']\n');
// Start profiling
profiler.startProfiling(id);
// Schedule stop of profiling in x seconds
setTimeout(function () {
stopProfiling(id)
}, 5000);
}
/**
* Stops the profiler and writes the data to a file
* @param id the id of the profiler process to stop
*/
function stopProfiling(id) {
var profile = profiler.stopProfiling(id);
fs.writeFile(_datadir + '/' + id + '.cpuprofile', JSON.stringify(profile), function () {
console.log('Profiler data written');
});
}
@harshitj2005

This comment has been minimized.

Copy link

harshitj2005 commented Sep 13, 2017

what is acheived by this? i dont get it. i have used this code and this file is created

profile-1505290405921.cpuprofile

in this file i dont get it which function consuming my cpu

@goktugyil

This comment has been minimized.

Copy link

goktugyil commented Nov 20, 2019

It seems this library is no longer supported

@nigimaster

This comment has been minimized.

Copy link

nigimaster commented Jan 24, 2020

Can't load file. Supported file extensions: '.heapsnapshot', '.heaptimeline', '.heapprofile'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.