monitorEventLoopDelay 是什么

perf_hooks.monitorEventLoopDelay([options])

  • options: Object
    resolution: The sampling rate in milliseconds. Must be greater than zero. Default: 10.
  • Returns: Histogram

    Creates a Histogram object that samples and reports the event loop delay over time. The delays will be reported in nanoseconds.
    Using a timer to detect approximate event loop delay works because the execution of timers is tied specifically to the lifecycle of the libuv event loop. That is, a delay in the loop will cause a delay in the execution of the timer, and those delays are specifically what this API is intended to detect.

监控 EventLoop 运行情况是判断系统是否健康的重要指标之一,如果有大量的延迟,说明系统存在密集计算,降低了系统的吞吐。Node.js 在 v11 版本引入了monitorEventLoopDelay,而之前需要自己去实现。

Read More

libuv 在 v1.36.0 之后移除了 gyp_uv.py (commit),没办法通过它去创建一个 libuv.a 静态链接库(v1.35 文档有详细的介绍),现在我们需要通过cmake去创建。

构建静态链接库

1
2
3
4
5
6
7
// 下载 libuv
git clone https://github.com/libuv/libuv
cd libuv
mkdir -p build
// DCMAKE_BUILD_TYPE 将其设置为 Debug 模式,不然断点没办法进入 libuv 源码中
(cd build && cmake -DCMAKE_BUILD_TYPE=Debug ..)
cmake --build build

之后 build 目录 如下,libuv_a.a 就是我们需要的 静态链接库。
build dir

新建 hello world 项目

通过 CLion 创建一个helloworld项目,在 CMakeLists.txt 里添加 libuv 相关的信息,将 libuv 的头文件和源码添加进来,最后把项目和 linuv 链接在一起

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cmake_minimum_required(VERSION 3.17)
project(helloworld)
add_executable(helloworld main.cpp)

# 前面 clone libuv 绝对路径
set(LIBUVDIR /your/libuv/path)
# 将源码导入
include_directories(${LIBUVDIR}/src)
include_directories(${LIBUVDIR}/include)

add_library(libuv STATIC IMPORTED)
set_target_properties(libuv
PROPERTIES IMPORTED_LOCATION
${LIBUVDIR}/build/libuv_a.a)

# 链接起来
target_link_libraries(helloworld libuv)

创建 main.cpp 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <uv.h>

static void cb(uv_write_t *req, int status) {
printf("Hello from Callback.\n");
}

int main() {
uv_tty_t tty;
uv_write_t req;
uv_tty_init(uv_default_loop(), &tty, 1, 0);
char str[] = "Hello UV!\n";
int len = strlen(str);
uv_buf_t bufs[] = {uv_buf_init(str, len)};
uv_write(&req, (uv_stream_t *) &tty, bufs, 1, cb);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
return 0;
}

之后就能愉快的打断点调试 libuv 了
debug libuv

学习 libuv 或者其它 C/C++相关的技术,感觉又回到了刚开始学习编程的时候,很多的不懂和挑战,不再像用 JavaScript 那样随心所欲,但是越是对底层的学习,越是能了解计算机原理,职业寿命才能变得更长。出于兴趣也好,出于无奈也好,总之新的学习让一切又变得有意思起来。

Cluster

这是一个比较熟悉的模块,早在 Node.js V0.8 版本的时候就已经被加入进来,平时在生产部署 Web 应用的时候,为了充分压榨多核 CPU,总是根据核数开启对应的数量的应用进程,来处理用户请求,这些在 PM2 工具 或者 Egg.js 框架里有相应的介绍,之前自己写的 简单梳理 Node.js 创建子进程的方法(下)—— cluster 有其原理介绍。

虽然 Cluster 已经很成熟,但是也有一些问题:

  • 进程开销较大
  • 很多第三方工具对 Cluster 不是很友好,还是以单进程为主,比如一些监控系统
  • 需要有一个管理进程,在某个 Cluster 进程出错退出后将其重启起来;单进程出错奔溃,运维可以通过健康检查未能通过的方式重启整个 Docker 等,相比而言 Cluster 模式复杂的多
  • 目前从框架上自然支持 Cluster 的只有 Egg.js,其它都需要辅助工具,比如 PM2,然而它并完全免费,所以真使用该项技术,又面临框架选择的问题

Worker Threads

在 Node.js V10.5.0 加入,但需要通过--experimental-worker开启,直到 V12 才默认支持。在密集计算任务处理上带来了新的解决方案。

我带领的团队有一个重要的业务需求,就是服务端渲染(SSR),我们使用 Egg.js 的 Cluster 模式来帮助我们提升吞吐量。虽然业务问题解决了,但是 Egg.js 比起 Express 或者 Koa 这样的框架来说复杂很多,让一个前端同事学习或者招募有经验的新同事还是有点棘手。另外就是和其它非其生态圈的工具搭配使用也遇到不少麻烦。

好在 Worker Threads 同样可以压榨 CPU,提升整体吞吐量。

Read More

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×