onvno / pokerface

日常技术文章阅读整理
3 stars 0 forks source link

20190715 - Node - Profiling #46

Open onvno opened 5 years ago

onvno commented 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);

分析结果

可使用:

onvno commented 5 years ago
onvno commented 5 years ago

Node.JS Profile 4.1 Profile实践