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] <profile source>

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时间越多。

做个对比,看起来 FlameGraphGraphviz 更精细一些。
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值