基本介绍
pprof 是在做性能优化前的性能分析工具。
安装: go get -u github.com/google/pprof
两个工具
服务型应用 _ “net/http/pprof” 包,专用于采集 web 服务 运行数据的分析
工具型应用 “runtime/pprof” 包,专用于采集 应用程序 运行数据的分析
另外 go tool pprof 是命令行指令,用于获取和分析 Profiling 数据,源数据可以是 http 地址,也可以是本地 Profile 文件;查看模式可以命令行交互模式,也可以是 http 模式(-http 参数)。
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"time"
)
func main() {
go func() {
for {
time.Sleep(1*time.Second)
log.Println(Add("Hello world"))
}
}()
http.ListenAndServe("0.0.0.0:8090", nil)
}
var datas []string
func Add(str string) string {
data := []byte(str)
sData := string(data)
datas = append(datas, sData)
return sData
}
http://localhost:8090/debug/pprof/
从图中我们可以看出time.Sleep消耗了很多性能