Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Last active October 21, 2015 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildmojo/fd107c8b731c773dbc18 to your computer and use it in GitHub Desktop.
Save mildmojo/fd107c8b731c773dbc18 to your computer and use it in GitHub Desktop.
Heartbeat.js - find event loop blocking code by looking for blown timeouts
// Heartbeat log script for node.js. Use this to sniff out code that blocks
// your event loop; heartbeats will take longer if the event loop is
// blocked.
// Just require('./heartbeat.js'); ==OR== require('./heartbeat.js')(3000);
module.exports = go;
var DEFAULT_HEARTBEAT_MS = 2000;
var startedAt, loop;
go();
function go(newInterval) {
newInterval = newInterval || DEFAULT_HEARTBEAT_MS;
clearInterval(loop);
startedAt = Date.now();
loop = setInterval(beat, newInterval);
}
function beat() {
var elapsed = Date.now() - startedAt;
console.warn('heartbeat %d: %s', process.pid, elapsed);
startedAt = Date.now();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment