0%

Go Debug

1. pprof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"log"
"net/http"
// import pprof to init
_ "net/http/pprof"
)

func main() {
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}

1.1 web

http 访问http://localhost:8080/debug/pprof可以查看到如下内容
pprof

  • 内存分配
  • goroutine数量
  • 堆分配
  • 资源竞争
  • 线程创建

1.2 go tool

1
2
3
$ go tool pprof -http=:8000 http://127.0.0.1:8080/debug/pprof/profile
$ go tool pprof -http=:8000 cpu.out
$ go tool pprof -http=:8000 pprof.XXX.samples.cpu.001.pb.gz

开启另一个web服务分析获取的信息,浏览器打开http://localhost:8000即可查看到如下页面
pprof
并包含有火焰图