搜索
您的当前位置:首页正文

性能检测火焰图生成(JS)

来源:吉趣旅游网
import { loggerBase } from "./launcher"
/**
 * 结束检测在浏览器访问  127.0.0.1:3000/stop
 * 将在项目中生成cpuprofile文件
 * 最后通过访问https://www.speedscope.app/查看火焰图
 */
export class FlameBearer {
    static start() {
        const profiler = require('v8-profiler-node8')
        const fs = require('fs')
        const logger = loggerBase.getCatLogger('flamebearer')
        const http = require('http')
        let server = http.createServer();
        server.on("request", function (request, response) {
            if (request.url === "/stop") {
                const profile = profiler.stopProfiling()
                logger.info("=============================检测模块结束=============================")
                profile.export()
                    .pipe(fs.createWriteStream(`cpuprofile-${Date.now()}.cpuprofile`))
                    .on('finish', () => profile.delete())
            }
        })
        server.listen(3000, function () {
            console.log('服务器启动成功了')
        })
        logger.info("=============================检测模块开启=============================")
        profiler.startProfiling('CPU profile')
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top