简体   繁体   English

如何检测和测量node.js中的事件循环阻塞?

[英]How to detect and measure event loop blocking in node.js?

I'd like to monitor how long each run of the event loop in node.js takes. 我想监视node.js中每次运行事件循环的时间。 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 . 我基本上通过查看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. 更新:将代码更改为使用setImmediatesetImmediate所做。

"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() 我没有比检查SetImmediate的时间延迟更好的方法来测试eventloop,但是使用节点的高分辨率计时器而不是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]. 注意:delta将是一个元组数组[秒,纳秒]。

For more details on process.hrtime(): https://nodejs.org/api/all.html#all_process_hrtime 有关process.hrtime()的更多详细信息: https//nodejs.org/api/all.html#all_process_hrtime

"The primary use is for measuring performance between intervals." “主要用于测量间隔之间的性能。”

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]) process.hrtime([时间])

Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array. 返回[seconds,nanoseconds]元组数组中的当前高分辨率实时。 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. time是一个可选参数,必须是前一个process.hrtime()调用的结果(因此,在包含前一个时间的[seconds,nanoseconds]元组数组中的实时)与当前时间进行区分。 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][, ...]) 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. 这不是setTimeout(fn,0)的简单别名,它效率更高。 It runs before any additional I/O events (including timers) fire in subsequent ticks of the event loop. 它在事件循环的后续滴答中触发任何其他I / O事件(包括定时器)之前运行。

Check out this plugin https://github.com/tj/node-blocked I'm using it now and it seems to do what you want. 看看这个插件https://github.com/tj/node-blocked我现在正在使用它,它似乎做你想要的。

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 将打印出事件循环被阻止的毫秒数

You may also want to look at the profiling built into node and io.js . 您可能还想查看内置于nodeio.js中的分析。 See for example this article http://www.brendangregg.com/flamegraphs.html 例如,参见本文http://www.brendangregg.com/flamegraphs.html

And this related SO answer How to debug Node.js applications 这个相关的SO答案如何调试Node.js应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM