gf框架之服務性能分析 - 可供選擇的pprof特性

文章來源:http://gf.johng.cn/592298html

ghttp包提供了很是強大和簡便的服務性能分析功能,內部完美集成了pprof性能分析工具,能夠在任什麼時候候經過EnablePprof方法啓用性能分析特性,並可自定義性能分析工具頁面路由地址,不傳遞路由地址時,默認URI地址爲/debug/pprofnode

咱們來看一個簡單的例子:linux

package main

import (
    "gitee.com/johng/gf/g/net/ghttp"
)

func main() {
    s := ghttp.GetServer()
    s.EnablePprof()
    s.BindHandler("/", func(r *ghttp.Request){
        r.Response.Writeln("哈嘍世界!")
    })
    s.SetPort(8199)
    s.Run()
}

這個例子使用了s.EnablePprof()啓用了性能分析,默認會自動註冊如下幾個路由規則:git

/debug/pprof/*action
/debug/pprof/cmdline
/debug/pprof/profile
/debug/pprof/symbol
/debug/pprof/trace

其中/debug/pprof/*action爲頁面訪問的路由,其餘幾個地址爲go tool pprof命令準備的。簡單的性能分析咱們直接訪問/debug/pprof地址便可,內容以下:golang

  1. pprof頁面
    圖片描述
  2. 堆使用量
    圖片描述
  3. 當前進程中的goroutine詳情
    圖片描述

若是想要進行詳細的性能分析,基本上離不開go tool pprof命令行工具的支持,在開啓性能分析支持後,咱們可使用如下命令執行性能採集分析:web

go tool pprof "http://127.0.0.1:8199/debug/pprof/profile"

執行後pprof工具通過約30秒左右的接口信息採集(這30秒期間Web Server應當有流量進入,咱們這裏不停地訪問hello world頁面以做測試),而後生成性能分析報告,隨後能夠經過top10/web等pprof命令查看報告結果,更多命令可以使用go tool pprof查看。關於pprof的詳細使用介紹,請查看golang官方:blog.golang.org/profiling-go-programsubuntu

本示例中的命令行性能分析結果以下:cookie

john@johnhome:~/Workspace/Go/gf$ go tool pprof "http://127.0.0.1:8199/debug/pprof/profile"
Fetching profile over HTTP from http://127.0.0.1:8199/debug/pprof/profile
Saved profile in /home/john/pprof/pprof.___go_build_pprof_go.samples.cpu.001.pb.gz
File: ___go_build_pprof_go
Type: cpu
Time: Apr 17, 2018 at 10:53pm (CST)
Duration: 30s, Total samples = 80ms ( 0.27%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top10
Showing nodes accounting for 80ms, 100% of 80ms total
Showing top 10 nodes out of 49
      flat  flat%   sum%        cum   cum%
      10ms 12.50% 12.50%       10ms 12.50%  gitee.com/johng/gf/g/net/ghttp.(*Cookie).Get /home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/g/net/ghttp/http_server_cookie.go
      10ms 12.50% 25.00%       10ms 12.50%  internal/poll.runtime_pollReset /home/john/Softs/go1.9.2/src/runtime/netpoll.go
      10ms 12.50% 37.50%       10ms 12.50%  runtime.futex /home/john/Softs/go1.9.2/src/runtime/sys_linux_amd64.s
      10ms 12.50% 50.00%       10ms 12.50%  runtime.getitab /home/john/Softs/go1.9.2/src/runtime/iface.go
      10ms 12.50% 62.50%       10ms 12.50%  runtime.newarray /home/john/Softs/go1.9.2/src/runtime/slice.go
      10ms 12.50% 75.00%       10ms 12.50%  runtime.rawstringtmp /home/john/Softs/go1.9.2/src/runtime/string.go
      10ms 12.50% 87.50%       10ms 12.50%  runtime.usleep /home/john/Softs/go1.9.2/src/runtime/sys_linux_amd64.s
      10ms 12.50%   100%       10ms 12.50%  sync.(*RWMutex).Lock /home/john/Softs/go1.9.2/src/sync/rwmutex.go
         0     0%   100%       10ms 12.50%  bufio.(*Writer).Flush /home/john/Softs/go1.9.2/src/bufio/bufio.go
         0     0%   100%       10ms 12.50%  gitee.com/johng/gf/g/container/gqueue.(*Queue).PopFront /home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/g/container/gqueue/gqueue.go
(pprof) web
Failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH
(pprof) web 
(pprof)

其中web命令用以圖形展現接口之間的調用關係以及性能狀況,可是須要安裝Graphviz圖形化工具,以我目前的系統爲ubuntu爲例,直接執行sudo apt-get install Graphviz命令便可安裝完成圖形化工具,隨後再次使用web命令,最終生成如下圖表:
圖片描述工具

相關文章
相關標籤/搜索