0x nodejs火焰图工具试用

昨天有大概介绍多0x 火焰图,以下是一个简单的试用

环境准备

  • 项目结构
├── README.md
├── ab.sh
├── app.js
├── package.json
└── yarn.lock
  • 代码说明
    flame script 为使用flamebearer 工具,all-in-one 为使用0x
    pacakge.json
 
{
  "name": "nodejs-flame-graph",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT",
  "scripts": {
    "start": "node --perf-basic-prof-only-functions app.js",
    "pprof": "node --prof app.js",
    "all-in-one":"0x app.js",
    "flame": "node --prof-process --preprocess -j isolate*.log | flamebearer"
  },
  "dependencies": {
    "express": "^4.14.1",
    "fast-levenshtein": "^2.0.6"
  },
  "devDependencies": {
    "0x": "^4.9.1",
    "flamebearer": "^1.1.3"
  }
}
 

app.js 一个express 代码

//app.js
const express = require('express');
const console = require('console');
const levenshtein = require('fast-levenshtein');
var arr=[];
const HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100 = 10;
const someFakeModule = (function someFakeModule () {
return {
  calculateStringDistance (a, b) {
    return levenshtein.get(a, b, {
    useCollator: true
    })
  }
 }
})()
const app = express();
app.get('/', (req, res) => {
  res.send(`
  <h2>Take a look at the network tab in devtools</h2>
  <script>
    function loops(func) {
    return func().then(_ => setTimeout(loops, 20, func))
    }
    loops(_ => fetch('api/tick'))
  </script>\
  `)
});
app.get('/api/tick', (req, res) => {
  arr.push({name:'Shubham'});
  Promise.resolve('asynchronous flow will make our stacktrace more realistic'.repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100))
  .then(text => {
    const randomText =Math.random().toString(32).repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100)
    return someFakeModule.calculateStringDistance(text, randomText)
  })
  .then(result => res.end(`result: ${result}, ${arr.length}`))
});
app.get('/api/end', () => process.exit());
app.listen(8080, () => {
  console.log(`go to http://localhost:8080/ to generate traffic`)
});
 
 

ab.sh 简单压测的

#!/bin/sh
ab -n 2000 -c 100 http://localhost:8080/api/tick

启动&&效果

  • 启动
yarn 
yarn all-in-one
 

 

 

  • 停止(ctrl+c) 查看效果
    生成内容

     

     


    火焰图

 

 

说明

0x 运行的配置选项还是挺多的,功能很强大

参考资料

https://github.com/davidmarkclements/0x
https://github.com/mapbox/flamebearer
https://github.com/rongfengliang/nodejs-flamegraph-learning

posted on   荣锋亮  阅读(848)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-01-05 gitingore && opensource license 自动生成的网站
2017-01-05 spring retry 使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示