zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    GDC快讯,腾讯CMatrix布局云游戏B端领域
    如何测试小程序? 腾讯智慧零售保障优衣库小程序体验优化
    一分钟读懂兼容报告:测试过程视频复现,问题定位很轻松
    一到秒杀就瘫痪?压测大师保你后台稳健
    how2j-springcloud-摘抄
    问题1
    springcloud---how2java--记录零碎的信息
    how2java-springcloud-demo
    oracle 查两个日期之间数据有多少条
    非官网渠道下单导致的错误
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/12151626.html
Copyright © 2011-2022 走看看