新聞中心
?GOFrame?框架的?Web Server?提供了非常強(qiáng)大和簡便的服務(wù)性能分析功能,內(nèi)部完美集成了?pprof?性能分析工具,可以在任何時(shí)候通過?EnablePProf?方法啟用性能分析特性,并可自定義性能分析工具頁面路由地址,不傳遞路由地址時(shí),默認(rèn)?URI?地址為?/debug/pprof?。

10年積累的成都網(wǎng)站制作、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有科爾沁右翼中免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
PProf啟用
?PProf?特性的啟用會(huì)對(duì)程序性能產(chǎn)生一定影響,具體影響程度需要根據(jù)當(dāng)前業(yè)務(wù)場景在?PProd?啟用前后進(jìn)行對(duì)比。
EnablePProf
我們來看一個(gè)簡單的例子:
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"runtime"
)
func main() {
runtime.SetMutexProfileFraction(1) // (非必需)開啟對(duì)鎖調(diào)用的跟蹤
runtime.SetBlockProfileRate(1) // (非必需)開啟對(duì)阻塞操作的跟蹤
s := g.Server()
s.EnablePProf()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Writeln("哈嘍世界!")
})
s.SetPort(8199)
s.Run()
}
這個(gè)例子使用了?s.EnablePProf()?啟用了性能分析,默認(rèn)會(huì)自動(dòng)注冊(cè)以下幾個(gè)路由規(guī)則:
/debug/pprof/*action
/debug/pprof/cmdline
/debug/pprof/profile
/debug/pprof/symbol
/debug/pprof/trace
其中?/debug/pprof/*action?為頁面訪問的路由,其他幾個(gè)地址為?go tool pprof?命令準(zhǔn)備的。
StartPProfServer
也可以使用?StartPProfServer?方法,快速開啟一個(gè)獨(dú)立的?PProf Server?,常用于一些沒有?HTTP Server?的常駐的進(jìn)程中(例如定時(shí)任務(wù)、?GRPC?服務(wù)中),可以快速開啟一個(gè)?PProf Server?用于程序性能分析。該方法的定義如下:
func StartPProfServer(port int, pattern ...string)
一般的場景是使用異步?goroutine?運(yùn)行該?PProd Server?,即往往是這么來使用:
package main
import (
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
go ghttp.StartPProfServer(8199)
// 其他服務(wù)啟動(dòng)、運(yùn)行
// ...
}
以上示例可以改進(jìn)為:
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
go ghttp.StartPProfServer(8299)
s := g.Server()
s.EnablePProf()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Writeln("哈嘍世界!")
})
s.SetPort(8199)
s.Run()
}
PProf指標(biāo)
- ?
heap?: 報(bào)告內(nèi)存分配樣本;用于監(jiān)視當(dāng)前和歷史內(nèi)存使用情況,并檢查內(nèi)存泄漏。 - ?
threadcreate?: 報(bào)告了導(dǎo)致創(chuàng)建新?OS?線程的程序部分。 - ?
goroutine?: 報(bào)告所有當(dāng)前?goroutine?的堆棧跟蹤。 - ?
block?: 顯示?goroutine?在哪里阻塞同步原語(包括計(jì)時(shí)器通道)的等待。默認(rèn)情況下未啟用,需要手動(dòng)調(diào)用?runtime.SetBlockProfileRate?啟用。 - ?
mutex?: 報(bào)告鎖競爭。默認(rèn)情況下未啟用,需要手動(dòng)調(diào)用?runtime.SetMutexProfileFraction?啟用。
PProf頁面
簡單的性能分析我們直接訪問?/debug/pprof?地址即可,內(nèi)容如下:
1、?pprof?頁面
2、堆使用量
3、當(dāng)前進(jìn)程中的?goroutine?詳情
性能采集分析
如果想要進(jìn)行詳細(xì)的性能分析,基本上離不開?go tool pprof?命令行工具的支持,在開啟性能分析支持后,我們可以使用以下命令執(zhí)行性能采集分析:
go tool pprof "http://127.0.0.1:8199/debug/pprof/profile"
執(zhí)行后?pprof?工具經(jīng)過約30秒左右的接口信息采集(這30秒期間?WebServer?應(yīng)當(dāng)有流量進(jìn)入,我們這里不停地訪問?hello world?頁面以作測試),然后生成性能分析報(bào)告,隨后可以通過?top10/web?等?pprof?命令查看報(bào)告結(jié)果,更多命令可使用?go tool pprof?查看。關(guān)于?pprof?的詳細(xì)使用介紹,請(qǐng)查看Golang官方:blog.golang.org/profiling-go-programs
CPU性能分析
本示例中的命令行性能分析結(jié)果如下:
$ 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% github.com/gogf/gf/v2/net/ghttp.(*Cookie).Get /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/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% github.com/gogf/gf/v2/container/gqueue.(*Queue).PopFront /home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/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?命令用以圖形展示接口之間的調(diào)用關(guān)系以及性能情況,但是需要安裝?Graphviz?圖形化工具,以我目前的系統(tǒng)為?Ubuntu?為例,直接執(zhí)行?sudo apt-get install graphviz?命令即可安裝完成圖形化工具(如果是MacOS,使用?brew install Graphviz?安裝),隨后再次使用?web?命令,最終生成以下圖表:
內(nèi)存使用分析
與CPU性能分析類似,內(nèi)存使用分析同樣使用到?go tool pprof?命令:
$ go tool pprof http://127.0.0.1:8299/debug/pprof/heap
Fetching profile over HTTP from http://127.0.0.1:8299/debug/pprof/heap
Saved profile in /Users/john/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.004.pb.gz
Type: inuse_space
Time: May 24, 2021 at 8:01pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 1536.39kB, 100% of 1536.39kB total
Showing top 10 nodes out of 19
flat flat% sum% cum cum%
512.19kB 33.34% 33.34% 512.19kB 33.34% runtime.malg
512.14kB 33.33% 66.67% 512.14kB 33.33% github.com/gogf/gf/v2/container/gmap.(*StrAnyMap).doSetWithLockCheck
512.06kB 33.33% 100% 512.06kB 33.33% net.newFD (inline)
0 0% 100% 512.14kB 33.33% github.com/gogf/gf/v2/container/gmap.(*StrAnyMap).GetOrSetFuncLock
0 0% 100% 512.06kB 33.33% github.com/gogf/gf/v2/net/ghttp.(*Server).startServer.func1
0 0% 100% 512.06kB 33.33% github.com/gogf/gf/v2/net/ghttp.(*gracefulServer).ListenAndServe
0 0% 100% 512.06kB 33.33% github.com/gogf/gf/v2/net/ghttp.(*gracefulServer).doServe
0 0% 100% 512.14kB 33.33% github.com/gogf/gf/v2/os/gres.Instance
0 0% 100% 512.14kB 33.33% github.com/gogf/gf/v2/os/gres.init
0 0% 100% 512.06kB 33.33% net.(*TCPListener).Accept
(pprof) web
(pprof)
通過web圖形展示,類似這樣的:
分享名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame高級(jí)特性-PProf服務(wù)性能分析
瀏覽路徑:http://www.dlmjj.cn/article/djeeipj.html


咨詢
建站咨詢
