Open onvno opened 5 years ago
查看CPU性能占用及堆占用,可以使用v8-profiler:
Node.js 性能调优之CPU篇(二)——v8-profiler
const fs = require('fs'); const crypto = require('crypto'); const express = require('express'); const profiler = require('v8-profiler'); const app = express(); app.get('/encrypt', function encryptRouter(req, res) { let password = req.query.password || 'test'; let encryptedPassword = crypto.pbkdf2Sync(password, 'salt', 10000, 512, 'sha1').toString('hex'); res.send(encryptedPassword); }); app.get('/cpuprofile', function cpuprofileRouter(req, res) { const duration = req.query.duration || 60; //Start Profiling profiler.startProfiling('CPU profile'); setTimeout(() => { //Stop Profiling after duration const profile = profiler.stopProfiling(); profile.export() .pipe(fs.createWriteStream('cpuprofile-' + Date.now() + '.cpuprofile')) .on('finish', () => profile.delete()); res.sendStatus(200); }, duration * 1000); }); app.listen(3000);
可使用:
如何查看火焰图:Node.js 性能分析之火焰图 X 轴不表示时间刻度,而表示时间长度,也就是占用的 CPU 时间; Y 轴从上到下,越接近底部表示越接近系统的底层,对于 Node.js 来说,上层是 js 代码,下层是 C 代码; 每一层上,每个条幅越宽,表示它在整个运行周期中,占用的 CPU 越多; 点击任何一个条幅,图像会自动将它置于最底部,同时只留下它之上的条幅,并且放大至全图;
Node.JS Profile 4.1 Profile实践
查看CPU性能占用及堆占用,可以使用v8-profiler:
使用例子
Node.js 性能调优之CPU篇(二)——v8-profiler
分析结果
可使用: