安装 PProf
$ go get -u github.com/google/pprof
编写测试用例
新建 data/d_test.go,文件内容:
package data
import "testing"
const url = "https://github.com/EDDYCJY"
func TestAdd(t *testing.T) {
s := Add(url)
if s == "" {
t.Errorf("Test.Add error!")
}
}
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
Add(url)
}
}
执行测试用例
go test 命令有两个参数和 pprof 相关,它们分别指定生成的 CPU 和 Memory profiling 保存的文件:
-cpuprofile
:cpu profiling 数据要保存的文件地址-memprofile
:memory profiling 数据要报文的文件地
$ cd data
$ go test -bench=. -cpuprofile=cpu.prof
goos: darwin
goarch: amd64
pkg: GoPlayground/sw_pprof/data
BenchmarkAdd-8 10036965 128 ns/op
PASS
ok GoPlayground/sw_pprof/data 1.616s
$ go tool pprof -http=:8080 cpu.prof
top
graph
框越大,线越粗代表它占用的时间越大。
peek
焰图(Flame Graph)
它的最大优点是动态的。调用顺序由上到下(A -> B -> C -> D),每一块代表一个函数,越大代表占用 CPU 的时间更长。同时它也支持点击块深入进行分析!
Reference
- https://golang.org/pkg/net/http/pprof/
- https://blog.golang.org/pprof
- https://golang.org/pkg/runtime/pprof/
- https://github.com/google/pprof/blob/master/doc/README.md
- https://blog.wolfogre.com/posts/go-ppof-practice/
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper