golang pprof simple to use

Project structure

├── go.mod
├── go.sum
├── main.go

go.mod

module github.com/rongfengliang/gopsutillearning
go 1.13
require (
 github.com/shirou/gopsutil v2.19.11+incompatible
 github. com / stretchr / testify v1 .4.0 // indirectly
 golang. org / x / sys v0 .0.0 - 20,191,224,085,550 - c709ea063b76 // indirectly
)
 

Code

package main
import (
 "fmt"
 "log"
 "net/http"
 _ "net/http/pprof"
 "github.com/shirou/gopsutil/mem"
)
Depending printinfo () {
 for {
  // fetch virtual mem info
  mem, err := mem.VirtualMemory()
  if err != nil {
   fmt.Println("some wrong", err)
  } else {
   fmt.Println((mem.String()))
  }
 }
}
func main() {
 go printinfo()
 log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}

pprof use

  • start up
go run main.go

 

 

  • Basic Commands
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
  • effect

 

 

View map flame cpu

  • Generate a profile
    addresshttp://localhost:6060/debug/pprof/profile
  • View command:
pprof -http 0.0.0.0:8080 profile
  • UI effects

 

 

  • Figure flame

 

 

makefile build

For convenience, use make to build cross-platform executable files

  • Makefile
build: clean compile
clean: 
 rm -rf pprof-demo-*
compile:
 echo "Compiling for every OS and Platform"
 GOOS=darwin GOARCH=amd64 go build -o pprof-demo-mac main.go
 GOOS=linux GOARCH=amd64 go build -o pprof-demo-linux main.go
 GOOS=windows GOARCH=amd64 go build -o pprof-demo-windows.exe main.go
 
  • 使用
make build

说明

golang 的pprof 功能强大,是把应用性能分析的利器,注意需要安装graphviz,结合自己的系统选择安装对应的版本

参考资料

https://golang.org/pkg/net/http/pprof/
https://github.com/google/pprof
https://github.com/rongfengliang/gopsutillearning
https://github.com/shirou/gopsutil

Guess you like

Origin www.cnblogs.com/rongfengliang/p/12107237.html