19

I'd like to monitor how long each run of the event loop in node.js takes. However I'm uncertain about the best way to measure this. The best way I could come up with looks like this:

var interval = 500;
var interval = setInterval(function() {
    var last = Date.now();        
    setImmediate(function() {
        var delta = Date.now() - last;
        if (delta > blockDelta) {
            report("node.eventloop_blocked", delta);
        }
    });
}, interval);

I basically infer the event loop run time by looking at the delay of a setInterval. I've seen the same approach in the blocked node module but it feels inaccurate and heavy. Is there a better way to get to this information?

Update: Changed the code to use setImmediate as done by hapi.js.

Fabian Jakobs
  • 27,222
  • 7
  • 38
  • 37
  • [The hapi.js folks do it in a similar manner](https://github.com/hapijs/good/blob/ecd705719777af5810dc224001940205cfebd2eb/lib/process.js#L14-L21), and they are monitoring this value constantly, so it looks like this would be your safest bet. – Paul Mougel Feb 17 '15 at 15:01
  • Thanks for the pointer. Their use os `setImmediate` is even better. – Fabian Jakobs Feb 18 '15 at 08:58
  • Hi I have a question here ,does the above code just detect the IO cost? but the event-loop has more than one queue, how the code detect it? – sinbar Nov 26 '18 at 03:09

4 Answers4

9

"Is there a better way to get this information?" I don't have a better way to test the eventloop than checking the time delay of SetImmediate, but you can get better precision using node's high resolution timer instead of Date.now()

var interval = 500;
var interval = setInterval(function() {
    var last = process.hrtime();          // replace Date.now()        
    setImmediate(function() {
        var delta = process.hrtime(last); // with process.hrtime()
        if (delta > blockDelta) {
            report("node.eventloop_blocked", delta);
        }
    });
}, interval);

NOTE: delta will be a tuple Array [seconds, nanoseconds].

For more details on process.hrtime(): https://nodejs.org/api/all.html#all_process_hrtime

"The primary use is for measuring performance between intervals."

YoungJohn
  • 857
  • 11
  • 14
  • Hi I have a question here ,does the above code just detect the IO cost? but the event-loop has more than one queue, how the code detect it? – sinbar Nov 26 '18 at 02:57
1

Code

this code will measure the time in nanoseconds it took for the event loop to trigger. it measures the time between the current process and the next tick.

var time = process.hrtime();
process.nextTick(function() {
   var diff = process.hrtime(time);


   console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
   // benchmark took 1000000527 nanoseconds
});

EDIT: added explanation,

process.hrtime([time])

Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array. time is an optional parameter that must be the result of a previous process.hrtime() call (and therefore, a real time in a [seconds, nanoseconds] tuple Array containing a previous time) to diff with the current time. These times are relative to an arbitrary time in the past, and not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals.

process.nextTick(callback[, arg][, ...])

Once the current event loop turn runs to completion, call the callback function.

This is not a simple alias to setTimeout(fn, 0), it's much more efficient. It runs before any additional I/O events (including timers) fire in subsequent ticks of the event loop.

Community
  • 1
  • 1
Bamieh
  • 7,907
  • 3
  • 26
  • 47
1

Check out this plugin https://github.com/tj/node-blocked I'm using it now and it seems to do what you want.

let blocked = require("blocked");

blocked(ms => {
  console.log("EVENT LOOP Blocked", ms);
});

Will print out how long in ms the event loop is blocked for

Joshua Ohana
  • 4,457
  • 12
  • 43
  • 90
0

You may also want to look at the profiling built into node and io.js. See for example this article http://www.brendangregg.com/flamegraphs.html

And this related SO answer How to debug Node.js applications

Sam Mikes
  • 9,202
  • 2
  • 36
  • 47