方案1 v8-profiler
1. v8-profiler 添加node性能收集埋點
2. Apachebench壓力測試
ab -t 300-c 10-p post.txt -T "application/json" http://localhost:8008/xxx
3. v8-profiler 收集得到 cpuProfile
4. 用chrome的開發者工具 More-tools -> JavaScript Profiler -> Load,讀取cpuProfile,查看火焰圖
5. 或者使用speedscope根據cupProfile生成火焰圖,支持leftHeavy模式,直接將CPU占用率最高的排在最左邊,一目了然,快速的可以定位到問題。
方案2 chrome inspect
1. inspect模式啟動node程序
node --inspect ./app.js
2. inspect模式啟動chrome
chrome://inspect
3. Apachebench壓力測試
ab -n 1000 -c 20 http://localhost:3000/
4. 生成profile文件
完成壓測之后停止服務,選擇save保存文件
5. 使用speedscope根據cupProfile生成火焰圖
npm install -g speedscope
speedscope CPU-xxxx.cpuprofile