Golang工具鏈總結
Go語言的工具鏈對開發者非常友好,內建了很多效能調優、測試的工具,比如CPU分析器pprof、Uber開源的用於生成火焰圖的go-torch、流量複製工具goreplay等,這些工具極大的提高了開發者效率,下面分別進行總結。
1 pprof
Go的強大之處是它已經在語言層面集成了profile取樣工具,允許程式在執行時使用它們,使用Go的profiler能獲取以下樣本資訊:
cpu profile
:報告程式的CPU使用情況,按照一定頻率去採集應用程式在CPU和暫存器上的資料。
/debug/pprof/profile:訪問這個連結會自動進行 CPU profiling,持續 30s,並生成一個檔案供下載 memory profile(heap profile):報告程式記憶體的使用情況。
/debug/pprof/heap
:Memory Profiling 的路徑,訪問這個連結會得到一個記憶體 Profiling 結果的檔案。
block profile:報告goroutines不在執行狀態的情況,可以用來分析和查詢死鎖等效能瓶頸。
/debug/pprof/block
:block Profiling 的路徑。
goroutine profile:報告goroutines的使用情況,有哪些goroutine,它們的呼叫關係是怎樣的。
/debug/pprof/goroutines
:執行的 goroutines 列表,以及呼叫關係
Go中在
net/http/pprof
和
runtime/pprof
兩個地方提供效能監測的包,
但其實net/http/pprof中只是使用runtime/pprof包進行封裝了一下,並在http埠上暴露出來。
常用命令
top
top 用於顯示前個佔用最高的函式,N –cum表示前N個累積佔用CPU最高的函式。(累積,是指取樣的棧裡面存在這個函式,也就是它的整個子呼叫鏈的佔有時間都累加起來 )
list
list $function 可以展示這個function中每一行佔用的情況。
web
使用web可以開啟一個視覺化的圖。
注意:ubuntu透過
sudo apt-get install -y graphviz
安裝視覺化工具。
如何使用
web伺服器
對於web伺服器,可以直接引入
_ “net/http/pprof”
,然後在瀏覽器中使用
http://local:port/debug/pprof
即可直接檢視當前web伺服器的CPU佔用情況和記憶體使用情況。具體使用請檢視參考資料。
服務程式
對於服務程式,也可選擇使用
net/http/pprof
包,首先引入
net/http/pprof
包,然後開啟一個單獨的goroutine來開啟埠監聽。
//例子
go func() {
log。Println(http。ListenAndServe(“localhost:8080”, http。DefaultServeMux))
}
應用程式
如果你的程式只是一個應用程式,比如計算fabonacci數列,對於這種情況,你需要使用runtime/pprof 。以下例項程式碼展示cpu和記憶體的profile:
var cpuprofile = flag。String(“cpuprofile”, “”, “write cpu profile to `file`”)
var memprofile = flag。String(“memprofile”, “”, “write memory profile to `file`”)
func main() {
flag。Parse()
if *cpuprofile != “” {
f, err := os。Create(*cpuprofile)
if err != nil {
log。Fatal(“could not create CPU profile: ”, err)
}
if err := pprof。StartCPUProfile(f); err != nil {
log。Fatal(“could not start CPU profile: ”, err)
}
defer pprof。StopCPUProfile()
}
// 。。。 rest of the program 。。。
if *memprofile != “” {
f, err := os。Create(*memprofile)
if err != nil {
log。Fatal(“could not create memory profile: ”, err)
}
runtime。GC() // get up-to-date statistics
if err := pprof。WriteHeapProfile(f); err != nil {
log。Fatal(“could not write memory profile: ”, err)
}
f。Close()
}
}
注意:
程式執行時需加入–cpuprofile引數,例如
。/fabonacci ——cpuprofile=fabnacci。prof
,此時程式執行的資訊會記錄到。prof字尾的檔案中。
使用
go tool pprof fabnacci。prof
(應用程式的prof檔案),進入pprof提示介面,輸入
web命令
可以檢視在
/tmp目錄
下生產的svg檔案。(需要安裝graphviz)
獲取的 Profiling 資料是動態的,要想獲得有效的資料,
請保證應用處於較大的負載
(比如正在生成中執行的服務,或者透過其他工具模擬訪問壓力)。否則如果應用處於空閒狀態,得到的結果可能沒有任何意義。
2 go-torch
go-torch是Uber公司開源的一款針對Golang程式的火焰圖生成工具,能收集 stack traces,並把它們整理成火焰圖,直觀地程式給開發人員。go-torch是基於使用BrendanGregg建立的火焰圖工具生成直觀的影象,很方便地分析Go的各個方法所佔用的CPU的時間。
如何安裝
準備環境
git clone https://github。com/brendangregg/FlameGraph。git
cp flamegraph。pl /usr/local/bin
注意:
在終端輸入flamegraph。pl -h檢驗是否安裝FlameGraph成功
安裝go-torch
go get -v github。com/uber/go-torch
常用命令
go-torch工具的使用非常簡單,沒有任何引數時,它會嘗試從
http://localhost:8080/debug/pprof/profile
獲取profiling資料,常用的三個引數如下:
-u –url:要訪問的URL,這裡只是主機和埠部分(eg:http://localhost:9090)
-s –suffix:pprof profile的路徑,預設為/debug/pprof/profile
–seconds:要執行profiling的時間長度,預設為30s
如何使用及火焰圖介紹:
樣例——
go-torch -u http://localhost:9090 -t 30
y軸表示呼叫棧,每一層都是一個函式。呼叫棧越深,火焰就越高,頂部就是正在執行的函式,下方都是它的父函式。
x 軸表示抽樣數,如果一個函式在 x 軸佔據的寬度越寬,就表示它被抽到的次數多,即執行的時間長。注意,x 軸不代表時間,而是所有的呼叫棧合併後,按字母順序排列的。
火焰圖就是看頂層的哪個函式佔據的寬度最大。只要有”平頂”(plateaus),就表示該函式可能存在效能問題。
3 goreplay
gorepaly是一個開源工具,抓取生產環境流量,並使用真實流量持續測試系統。它使用raw-socket抓取系統流量,並根據指定的url,將流量進行轉發。
如何安裝
可以到
https://
github。com/buger/gor/re
leases
下載特定版本及平臺的二進位制包,並放在可執行目錄下。(當前支援的系統有 Windows, Linux x64 and Mac OS)
linux 64位安裝示例
wget https://github。com/buger/goreplay/releases/download/v0。16。1/gor_0。16。1_x64。tar。gz
tar -xvf gor_0。16。1_x64。tar。gz
cp goreplay /usr/local/bin
如何使用
抓取資料包
sudo goreplay ——input-raw :8080 ——output-stdout
將抓到的包列印到終端,進行檢視,原理類似於tcpdump。
重放資料包
sudo goreplay ——input-raw :8080 ——output-http=“http://localhost:8111”
使用–out-stdout引數替換–out-http,並加上要轉發的url,就可以將抓到的包轉發到目的地。
儲存以及重放
儲存資料包到requests。gor檔案
sudo gorelay ——input-raw :8080 ——output-file=requests。gor
重放requests.gor資料包
sudo ——input-file requests。gor ——output-http=“http://localhost:8111”
可以使用–output-file和–input-file來儲存、重放資料包
常用命令
簡單的HTTP流量複製
$ gor ——input-raw :80 ——output-http “http://staging。com”
HTTP流量複製頻率控制(獲取每秒超過10個請求)
$ gor ——input-tcp :28020 ——output-http “http://staging。com|10”
HTTP流量複製縮小
$ gor ——input-raw :80 ——output-tcp “replay。local:28020|10%”
HTTP流量記錄到本地檔案
$ gor ——input-raw :80 ——output-file requests。gor
HTTP流量回放和壓測
$ gor ——input-file “requests。gor|200%” ——output-http “staging。com”
HTTP流量過濾複製
$ gor ——input-raw :8080 ——output-http staging。com ——output-http-url-regexp ^www。
自定義一些流量複製的引數
$ gor ——input-raw :80 ——output-http 192。168。2。6:8000 ——http-allow-method POST ——http-set-header ‘User-Agent: Gor’ -output-http-workers=1 -http-allow-url test。php
將流量複製兩份到不同的測試服務
$ gor ——input-tcp :28020 ——output-http “http://staging。com” ——output-http “http://dev。com”
將流量像負載均衡一樣分配到不同的伺服器
$ gor ——input-tcp :28020 ——output-http “http://staging。com” ——output-http “http://dev。com” ——split-output true
本文大致對golang描述的工具鏈,進行總結,如有錯誤,歡迎指出。
原文連結:
https://
blog。csdn。net/phantom_1
11/article/details/79981579
上一篇:當空軍飛行員成績有什麼要求?