Golang性能调优(go-torch, go tool pprof)

Go语言已经为开发者内置配套了很多性能调优监控的好工具和方法,这大大提升了我们profile分析的效率。此外本文还将重点介绍和推荐uber开源的go-torch,其生成的火焰图更方便更直观的帮我们进行性能调优。我也是在实际一次的性能调优中,接触到go-torch,非常棒。
摘要由CSDN通过智能技术生成

Go语言已经为开发者内置配套了很多性能调优监控的好工具和方法,这大大提升了我们profile分析的效率。此外本文还将重点介绍和推荐uber开源的go-torch,其生成的火焰图更方便更直观的帮我们进行性能调优。我也是在实际一次的性能调优中,接触到go-torch,非常棒。

go tool pprof简介

Golang内置cpu, mem, block profiler

Go强大之处是它已经在语言层面集成了profile采样工具,并且允许我们在程序的运行时使用它们,使用Go的profiler我们能获取以下的样本信息:

  • cpu profiles
  • mem profiles
  • block profile

Golang常见的profiling使用场景

  • 基准测试文件:例如使用命令go test . -bench . -cpuprofile prof.cpu生成采样文件后,再通过命令 go tool pprof [binary] prof.cpu 来进行分析。

  • import _ net/http/pprof:如果我们的应用是一个web服务,我们可以在http服务启动的代码文件(eg: main.go)添加 import _ net/http/pprof,这样我们的服务 便能自动开启profile功能,有助于我们直接分析采样结果。

  • 通过在代码里面调用runtime.StartCPUProfile或者runtime.WriteHeapProfile等内置方法,即可方便的进行数据采样。

更多Golang Profiling的使用,推荐https://blog.golang.org/profiling-go-programs

go tool pprof的使用方法

go tool pprof的参数很多,不做详细介绍,自己help看看。在这里,我主要用到的命令为:
go tool pprof --seconds 25 http://localhost:9090/debug/pprof/profile
命令中,设置了25s的采样时间,当25s采样结束后,就生成了我们想要的profile文件,然后在pprof交互命令行中输入web,从浏览器中打开,就能看到对应的整个调用链的性能树形图。

root@garnett:~/# go tool pprof -h
usage: pprof [options] [binary] <profile source> ...
Output format (only set one):
  -callgrind        Outputs a graph in callgrind format
  -disasm=p         Output annotated assembly for functions matching regexp or address
  -dot              Outputs a graph in DOT format
  -eog              Visualize graph through eog
  -evince           Visualize graph through evince
  -gif              Outputs a graph image in GIF format
  -gv               Visualize graph through gv
  -list=p           Output annotated source for functions matching regexp
  -pdf              Outputs a graph in PDF format
  -peek=p           Output callers/callees of functions matching regexp
  -png              Outputs a graph image in PNG format
  -proto            Outputs the profile in compressed protobuf format
  -ps               Outputs a graph in PS format
  -raw              Outputs a text representation of the raw profile
  -svg              Outputs a graph in SVG format
  -tags             Outputs all tags in the profile
  -text             Outputs top entries in text form
  -top              Outputs top entries in text form
  -tree             Outputs a text rendering of call graph
  -web              Visualize graph through web browser
  -weblist=p        Output annotated source in HTML for functions matching regexp or address
Output file parameters (for file-based output formats):
  -output=f         Generate output on file f (stdout by default)
Output granularity (only set one):
  -functions        Report at function level [default]
  -
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值