nodejs 应用火焰图简单分析
2021-05-02 11:29
标签:lis 如何 listen local ISE 简单 dsc pre distance 以前有写过一个使用speedscope 的简单说明,以下是一个使用另外一个工具进行火焰图分析的简单说明 pacakge.json https://nodejs.org/en/docs/guides/diagnostics-flamegraph/ nodejs 应用火焰图简单分析 标签:lis 如何 listen local ISE 简单 dsc pre distance 原文地址:https://www.cnblogs.com/rongfengliang/p/12128876.html环境准备
├── app.js
├── package.json
└── yarn.lock
app.js//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(`
Take a look at the network tab in devtools
function loops(func) {
return func().then(_ => setTimeout(loops, 20, func))
}
loops(_ => fetch(‘api/tick‘))
\
`)
});
?
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`)
});
{
"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",
"flame":"node --prof-process --preprocess -j isolate*.log | flamebearer"
},
"dependencies": {
"express": "^4.14.1",
"fast-levenshtein": "^2.0.6"
},
"devDependencies": {
"flamebearer": "^1.1.3"
}
}
启动&&测试
yarn pprof
ab -n 2000 -c 100 http://localhost:8080/api/tick
yarn flame
可以看出calculateStringDistance
占用是比较高的
对于火焰图的生成使用了flamebearer 工具,实际上perf 也是一个很强大的工具,提供nodejs 官方也介绍了如何使用perf 进行分析参考资料
https://github.com/mapbox/flamebearer
https://nodejs.org/uk/docs/guides/simple-profiling/