node 应用cpu和内存占用分析

0x (推荐)

    通过封装了 --prof 对 v8 的产出做了处理,生成直接可用的火焰图。
    也可以直接生成cpuprofile

如下图:

node自带的命令

node --cpu-prof app.js
    该命令会直接生成一个.cpuprofile  可以在浏览器查看
    还有其他命令可以参考: [NODE_OPTIONS=options... | Node.js API 文档](http://nodejs.cn/api/cli/node_options_options.html)

查看应用的内存情况

const { writeHeapSnapshot } = require('node:v8');
const {
  Worker,
  isMainThread,
  parentPort
} = require('node:worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);

  worker.once('message', (filename) => {
    console.log(`worker heapdump: ${filename}`);
    // Now get a heapdump for the main thread.
    console.log(`main thread heapdump: ${writeHeapSnapshot()}`);
  });

  // Tell the worker to create a heapdump.
  worker.postMessage('heapdump');
} else {
  parentPort.once('message', (message) => {
    if (message === 'heapdump') {
      // Generate a heapdump for the worker
      // and return the filename to the parent.
      parentPort.postMessage(writeHeapSnapshot());
    }
  });
}

生成一个.heapsnapshot文件,可以在浏览器中直接打开

如何在浏览器中打开xxx.cpuprofile文件


单击加载性能分析报告,选择xxx.cpuprofile文件

如何在浏览器中打开xxx.heapsnapshot文件


单击加载,选择xxx.heapsnapshot文件。

使用浏览器自带的功能

可以参考阮一峰老师的Node 调试工具入门教程

参考文章:
● https://www.ruanyifeng.com/blog/2018/03/node-debugger.html
● https://nodejs.org/zh-cn/docs/guides/diagnostics-flamegraph/

如果你喜欢我的作品,请考虑赞助,以保持它们的可持续性。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值