栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

golang性能分析go-torch,火焰图FlameGraph

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

golang性能分析go-torch,火焰图FlameGraph

前面说过我们可以使用golang提供的go tool pprof来做性能分析,另外,今天介绍一下另一个工具 go-torch。

首先要明确的是,无论是go tool pprof还是go-torch都是对性能指标数据的分析展示,而性能数据的获取还是要通过包runtime/pprof。

go-torch是Uber公司开源的一款针对Golang程序的火焰图生成工具,能收集 stack traces,并把它们整理成火焰图,直观地呈现给开发人员。go-torch是基于使用BrendanGregg创建的火焰图工具生成直观的图像,很方便地分析Go的各个方法所占用的CPU的时间。

在 Linux 环境下

安装FlameGraph脚本

cd /mnt/d/dev/php/magook/trunk/server
git clone http://github.com/brendangregg/FlameGraph.git
cd FlameGraph
cp flamegraph.pl /usr/local/bin

flamegraph.pl -h

Option h is ambiguous (hash, height, help)
USAGE: ./flamegraph.pl [options] infile > outfile.svg

        --title TEXT     # change title text
        --subtitle TEXT  # second level title (optional)
        --width NUM      # width of image (default 1200)
        --height NUM     # height of each frame (default 16)
        --minwidth NUM   # omit smaller functions (default 0.1 pixels)
        --fonttype FONT  # font type (default "Verdana")
        --fontsize NUM   # font size (default 12)
        --countname TEXT # count type label (default "samples")
        --nametype TEXT  # name type label (default "Function:")
        --colors PALETTE # set color palette. choices are: hot (default), mem,
                         # io, wakeup, chain, java, js, perl, red, green, blue,
                         # aqua, yellow, purple, orange
        --bgcolors COLOR # set background colors. gradient choices are yellow
                         # (default), blue, green, grey; flat colors use "#rrggbb"
        --hash           # colors are keyed by function name hash
        --cp             # use consistent palette (palette.map)
        --reverse        # generate stack-reversed flame graph
        --inverted       # icicle graph
        --flamechart     # produce a flame chart (sort by time, do not merge stacks)
        --negate         # switch differential hues (blue<->red)
        --notes TEXT     # add notes comment in SVG (for debugging)
        --help           # this message

        eg,
        ./flamegraph.pl --title="Flame Graph: malloc()" trace.txt > graph.svg

安装go-torch

go get -u github.com/uber/go-torch

安装压力测试工具

go get -u github.com/adjust/go-wrk

在压力测试的同时,我们来收集性能数据才能发现问题。

测试DEMO

就在 FlameGraph目录下

package main

import (
    "log"
    "net/http"
    "fmt"
    _ "net/http/pprof"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World!n")
}

func main() {
    http.HandleFunc("/hello", myHandler) //    设置访问路由
    log.Fatal(http.ListenAndServe(":6060", nil))
}

打开三个窗口,分别执行。

1、go build test_go.go && ./test_go

2、go-wrk -c=400 -t=8 -n=10000 http://localhost:6060/hello

3、go-torch -u http://localhost:6060/ -f test_go.svg

INFO[15:33:22] Run pprof command: go tool pprof -raw -seconds 30 http://localhost:6060/debug/pprof/profile
ERROR: No stack counts found
FATAL[15:33:53] Failed: could not generate flame graph: exit status 2

按照网上说的,增大请求量,还是不行,改用 wrk来压测也不行,暂时未解决,可能是我用的WSL系统有关。

go-torch -h
Usage:
  go-torch [options] [binary] 

pprof Options:
  -u, --url=         Base URL of your Go program (default: http://localhost:8080)
      --suffix=      URL path of pprof profile (default: /debug/pprof/profile)
  -b, --binaryinput= File path of previously saved binary profile. (binary profile is anything accepted by https://golang.org/cmd/pprof)
      --binaryname=  File path of the binary that the binaryinput is for, used for pprof inputs
  -t, --seconds=     Number of seconds to profile for (default: 30)
      --pprofArgs=   Extra arguments for pprof

Output Options:
  -f, --file=        Output file name (must be .svg) (default: torch.svg)
  -p, --print        Print the generated svg to stdout instead of writing to file
  -r, --raw          Print the raw call graph output to stdout instead of creating a flame graph; use with Brendan Gregg's flame graph perl script (see https://github.com/brendangregg/FlameGraph)
      --title=       Graph title to display in the output file (default: Flame Graph)
      --width=       Generated graph width (default: 1200)
      --hash         Colors are keyed by function name hash
      --colors=      set color palette. choices are: hot (default), mem, io, wakeup, chain, java, js, perl, red, green, blue, aqua, yellow, purple, orange
      --cp           Use consistent palette (palette.map)
      --reverse      Generate stack-reversed flame graph
      --inverted     icicle graph

Help Options:
  -h, --help         Show this help message

可见,我可以直接使用 go-torch profile文件

在上篇中,在win下生成的profile文件,拷贝到当前目录。

go-torch pprof.samples.cpu.001.pb.gz
INFO[11:29:13] Run pprof command: go tool pprof -raw -seconds 30 pprof.samples.cpu.001.pb.gz
INFO[11:29:13] Writing svg to torch.svg

可见svg成功生成了。点击 torch.svg。

火焰图的y轴表示cpu调用方法的先后,x轴表示在每个采样调用时间内,方法所占的时间百分比,越宽代表占据cpu时间越多。

做个对比,看起来 FlameGraph 比 Graphviz 更精细一些。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965605.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号