Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js Event Loop Diagnostics

Is it possible to peek at the event loop for diagnostics?

I would like to know how many events are currently waiting for execution (excluding setTimeout/interval).

Update: I'd like to do this from inside the running node process.

like image 932
laktak Avatar asked Feb 14 '13 08:02

laktak


People also ask

How do I loop an event in Node JS?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.

What causes event loop lag?

The event loop does not stop code executing; it waits until the code stops. This can be because the code has completed, or it has requested an asynchronous activity and is waiting for the callback (or a promise to resolve). AppOptics reports the event loop lag.

Does node js have an event loop?

Node. js has two types of threads: one Event Loop and k Workers. The Event Loop is responsible for JavaScript callbacks and non-blocking I/O, and a Worker executes tasks corresponding to C++ code that completes an asynchronous request, including blocking I/O and CPU-intensive work.

What is Nodejs event loop lag?

Event loop lag measures the time span between the scheduling of a callback and its execution. Or, since a picture says more than a thousand words: An example where a timer callback is scheduled in the 'poll' phase. The blue circle indicates the time span until it is executed.


1 Answers

Updated for nodejs 0.10 with setImmediate()

While I wasn't able to find the number of waiting events in the queue I found another health metric that might be useful:

var ts=Date.now();
setImmediate(function()
{
  var delay=Date.now()-ts;
});

delay will contain the milliseconds it took from queuing the event to executing it.

This also takes cpu intensive events into account (which would not be possible by just looking at the # of events).

The measurement itself will affect the event queue as well but this should have a much lower overhead than a full profiler.

like image 200
laktak Avatar answered Sep 28 '22 00:09

laktak