Last active
October 21, 2015 21:52
-
-
Save mildmojo/fd107c8b731c773dbc18 to your computer and use it in GitHub Desktop.
Heartbeat.js - find event loop blocking code by looking for blown timeouts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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