您的位置 首页 golang

Golang之net/http/pprof性能分析

golang 提供的 pprof 工具可以很方便的分析性能上的问题比如cpu的使用情况, 堆内存 分配,goroutine 死锁 情况等

引入pprof

新建main.go文件,贴入如下代码:

package main

 import  (
" github .com/kataras/iris/v12"

"github.com/kataras/iris/v12/ middleware /logger"
"github.com/kataras/iris/v12/middleware/recover"
 " net /http"
 _ "net/http/pprof"
)

func main() {
app := iris.New()

// Method: GET
// Resource: 
app.Handle("GET", "/", func(ctx iris.Context) {
ctx.HTML("<h1> Welcome </h1>")
})

 // 启动pprof的 web服务 
 go func() {
http.ListenAndServe("0.0.0.0:8081",  nil )
}()
// 
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
} 

执行go run main.go 启动程序,运行后访问

图形报告

MacOS 需要安装 graphviz

  • 使用 pprof 查看堆内存分配
  • go tool pprof

    • 查看30秒CPU使用

    go tool pprof

    • 收集5S执行跟踪

    wget

    执行完成后,使用”web”命令查看图形报告

    文章来源:智云一二三科技

    文章标题:Golang之net/http/pprof性能分析

    文章地址:https://www.zhihuclub.com/98268.shtml

    关于作者: 智云科技

    热门文章

    网站地图