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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With