Skip to content

Instantly share code, notes, and snippets.

@zz85
Last active August 8, 2017 10:04
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 zz85/a0b365d72d664a95d0bcd20729f88192 to your computer and use it in GitHub Desktop.
Save zz85/a0b365d72d664a95d0bcd20729f88192 to your computer and use it in GitHub Desktop.
Node Event Loop Lag
// Also see https://github.com/tj/node-blocked/blob/master/index.js and https://stackoverflow.com/questions/28563354/how-to-detect-and-measure-event-loop-blocking-in-node-js
const blocked = require('blocked');
blocked(function(ms) {
console.log("Blocked", ms);
}, { threshold: 10 });
var blockDelta = 1;
var interval = 500;
var interval = setInterval(function() {
const last = process.hrtime();
const last2 = Date.now();
let x= 0;
for (let i =0; i < 100000000; i++) { x++; }
const calc_diff = (x) => {
var diff = process.hrtime(last); // with process.hrtime()
var delta = (diff[0] * 1e9 + diff[1]) * 1e-6;
console.log("node.eventloop_blocked", Math.round(delta), Date.now() - last2, x);
if (delta > blockDelta) {
}
}
setImmediate(calc_diff, 'immediate');
process.nextTick(calc_diff, 'nexttick');
}, interval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment