go?tool?pprof?參數(shù)?'-base'?和?'-diff_base'之間的區(qū)別解析
go tool pprof 工具是用于分析由 runtime/pprof
包 或 net/http/pprof
包產(chǎn)生的profile數(shù)據(jù),完整的幫助文檔在 https://github.com/google/pprof/blob/main/doc/README.md,pprof
工具支持的參數(shù)很多,可以用命令 go tool pprof --help
來查看全部參數(shù)列表,今天主要來說下 -base
和 -diff_base
這兩個(gè)參數(shù), 因?yàn)槲臋n對這兩個(gè)參數(shù)的描述比較晦澀,難以理解這兩個(gè)參數(shù)之間的區(qū)別。
-diff_base source Source of base profile for comparison -base source Source of base profile for profile subtraction
pprof can subtract one profile from another, provided the profiles are of compatible types (i.e. two heap profiles). pprof has two options which can be used to specify the filename or URL for a profile to be subtracted from the source profile:
-diff_base= profile: useful for comparing two profiles. Percentages in the output are relative to the total of samples in the diff base profile.
-base= profile: useful for subtracting a cumulative profile, like a golang block profile, from another cumulative profile collected from the same program at a later time. When comparing cumulative profiles collected on the same program, percentages in the output are relative to the difference between the total for the source profile and the total for the base profile.
The -normalize flag can be used when a base profile is specified with either the -diff_base or the -base option. This flag scales the source profile so that the total of samples in the source profile is equal to the total of samples in the base profile prior to subtracting the base profile from the source profile. Useful for determining the relative differences between profiles, for example, which profile has a larger percentage of CPU time used in a particular function.
When using the -diff_base option, some report entries may have negative values. If the merged profile is output as a protocol buffer, all samples in the diff base profile will have a label with the key “pprof::base” and a value of “true”. If pprof is then used to look at the merged profile, it will behave as if separate source and base profiles were passed in.
When using the -base option to subtract one cumulative profile from another collected on the same program at a later time, percentages will be relative to the difference between the total for the source profile and the total for the base profile, and all values will be positive. In the general case, some report entries may have negative values and percentages will be relative to the total of the absolute value of all samples when aggregated at the address level.
這兩個(gè)參數(shù)都是用于多個(gè)profile文件之間的對比,文檔中關(guān)于這兩個(gè)參數(shù)的描述翻譯過來:
比較profile文件
pprof
工具可以從一個(gè)profile文件中減去另一個(gè)profile文件,前提是這兩個(gè)profile文件類型是兼容的(比如兩個(gè)都是heap profile文件)。pprof有兩個(gè)選項(xiàng),用于指定需要減去的這個(gè)profile文件的文件名或URL:-diff_base=profile
:用于比較兩個(gè)profile文件。計(jì)算出的百分比是基于diff_base 參數(shù)指定的這個(gè)profile文件中的采樣指標(biāo)之和。-base=profile
:用于累計(jì)指標(biāo)類型的profile文件(比如golang的block profile),適用于將同一個(gè)程序后采集的profile文件減去先采集的profile文件的場景。當(dāng)對比在同一個(gè)程序上收集的累計(jì)指標(biāo)類型的profile文件時(shí),展示的百分比是基于當(dāng)前采集的profile文件中的采樣指標(biāo)之和與之前采集的profile文件中采樣指標(biāo)之和之間的差值。
當(dāng)用-diff_base
或-base
參數(shù)指定基準(zhǔn)profile文件時(shí),可以加上-normalize
選項(xiàng),加上該選項(xiàng)后,在計(jì)算當(dāng)前profile文件與基準(zhǔn)profile文件的采樣和差值之前,會(huì)把當(dāng)前profile文件的采樣數(shù)據(jù)全部乘以一個(gè)固定的百分比(用基準(zhǔn)profile采樣和除以當(dāng)前profile采樣和得到),使得當(dāng)前profile的采樣數(shù)據(jù)之和與基準(zhǔn)profile采樣和相等。通常用于確定兩個(gè)profile文件之間的相對差異,例如,用于比較哪個(gè)profile在特定函數(shù)中消耗的CPU時(shí)間占比更高。
當(dāng)使用-diff_base
選項(xiàng)時(shí),某些指標(biāo)可能會(huì)存在負(fù)值。如果合并后的profile文件以protocol buffer
格式輸出,則基準(zhǔn)profile文件中的所有采樣都會(huì)加上pprof::base
的Label,Label的值為“true”。如果隨后使用pprof
工具來查看合并之后的profile文件,那么它的表現(xiàn)和同時(shí)傳入合并前的原始profile文件和基準(zhǔn)profile文件保持一致。
當(dāng)使用-base
選項(xiàng)從同一個(gè)程序上后收集的一個(gè)累計(jì)指標(biāo)類型的profile文件中減去一個(gè)之前收集的同類型的profile文件時(shí),百分比將相對于后收集的profile文件的采樣指標(biāo)總和與先收集的profile文件的采樣指標(biāo)總和之間的差值,并且所有值都將為正數(shù)(因?yàn)槔塾?jì)指標(biāo)是隨著時(shí)間在不斷增長的,后采集的值不會(huì)比先采集的值要?。?。一般來說,有些指標(biāo)可能會(huì)存在負(fù)值,這時(shí)當(dāng)在地址級(jí)別進(jìn)行聚合時(shí),百分比將相對于所有采樣指標(biāo)的絕對值之和。
總結(jié)下來就是,兩個(gè)參數(shù)都是用于計(jì)算當(dāng)前 profile文件減去基準(zhǔn)profile文件所獲得的差值,用這個(gè)差值生成一個(gè)新的profile文件,區(qū)別在于計(jì)算這個(gè)新生成的profile文件每個(gè)采樣指標(biāo)的占比時(shí),-base
是基于基準(zhǔn)profile文件的指標(biāo)和(所以百分比可能大于100%),而 -diff_base
則是基于差值profile文件的指標(biāo)和(也就是這個(gè)新生成的profile文件本身的指標(biāo)和,所以每個(gè)采樣的指標(biāo)占比不會(huì)超過100%)。
比如我這里由同一個(gè)程序前后生成了兩個(gè)profile文件: profile001.pb.gz
和 profile002.pb.gz
,現(xiàn)在分別用 -base
和 -diff_base
參數(shù)生成一個(gè)差集profile:
$ pprof -proto -output gen_by_base_opt.pb.gz -base profile001.pb.gz profile002.pb.gz Generating report in gen_by_base_opt.pb.gz $ pprof -proto -output gen_by_diff_base_opt.pb.gz -diff_base profile001.pb.gz profile002.pb.gz Generating report in gen_by_diff_base_opt.pb.gz $ ll total 48 -rw-r--r-- 1 zy staff 2839 5 18 19:42 gen_by_base_opt.pb.gz -rw-r--r-- 1 zy staff 4772 5 18 19:42 gen_by_diff_base_opt.pb.gz -rw-r--r-- 1 zy staff 3092 5 17 16:43 profile001.pb.gz -rw-r--r-- 1 zy staff 4504 5 17 16:59 profile002.pb.gz
分別計(jì)算出這兩個(gè)差集的指標(biāo)和:
package main import ( "fmt" "github.com/google/pprof/profile" "log" "os" ) func sumProfile(file string) map[string]int64 { f, err := os.Open(file) if err != nil { log.Fatal(err) } defer f.Close() pprof, err := profile.Parse(f) if err != nil { log.Fatal(err) } sums := make(map[string]int64, len(pprof.SampleType)) for _, sample := range pprof.Sample { for i, v := range sample.Value { sums[pprof.SampleType[i].Type] += v } } return sums } func main() { var sum map[string]int64 sum = sumProfile("./profile001.pb.gz") fmt.Println("sum of profile001.pb.gz: ") keys := make([]string, 0, len(sum)) for k := range sum { keys = append(keys, k) } for _, k := range keys { fmt.Println(k, " --> ", sum[k]) } fmt.Println() sum = sumProfile("./profile002.pb.gz") fmt.Println("sum of profile002.pb.gz: ") for _, k := range keys { fmt.Println(k, " --> ", sum[k]) } fmt.Println() sum = sumProfile("./gen_by_base_opt.pb.gz") fmt.Println("sum of gen_by_base_opt.pb.gz: ") for _, k := range keys { fmt.Println(k, " --> ", sum[k]) } fmt.Println() sum = sumProfile("./gen_by_diff_base_opt.pb.gz") fmt.Println("sum of gen_by_diff_base_opt.pb.gz: ") for _, k := range keys { fmt.Println(k, " --> ", sum[k]) } fmt.Println() }
執(zhí)行結(jié)果:
sum of profile001.pb.gz:
alloc_objects --> 26137
alloc_space --> 31409035
inuse_objects --> 3914
inuse_space --> 3148768sum of profile002.pb.gz:
alloc_space --> 98663649
inuse_objects --> 9397
inuse_space --> 4771620
alloc_objects --> 51635sum of gen_by_base_opt.pb.gz:
alloc_objects --> 25498
alloc_space --> 67254614
inuse_objects --> 5483
inuse_space --> 1622852sum of gen_by_diff_base_opt.pb.gz:
alloc_objects --> 25498
alloc_space --> 67254614
inuse_objects --> 5483
inuse_space --> 1622852
可以看出,gen_by_base_opt.pb.gz
和 gen_by_diff_base_opt.pb.gz
的各項(xiàng)指標(biāo)和等于 profile002.pb.gz
值減去 profile001.pb.gz
。
通過 pprof
工具來查看生成的差集profile:
$ pprof -http=: -base=profile001.pb.gz profile002.pb.gz Serving web UI on http://localhost:57739
$ pprof -http=: -diff_base=profile001.pb.gz profile002.pb.gz Serving web UI on http://localhost:57845
對比兩幅火焰圖可以看出兩幅圖除了百分比之外基本沒有差別,并且從右上角可以看出這里展示的是 alloc_space
指標(biāo)數(shù)據(jù),而 64.14MB
正好就是我們上面生成的profile文件gen_by_base_opt.pb.gz
和 gen_by_diff_base_opt.pb.gz
的 alloc_space
指標(biāo)和(67254614 / 1024 / 1024 = 64.13899803161621)。
而百分比之所以不同,是因?yàn)橛?jì)算百分比的基數(shù)不同,-base
是以計(jì)算出的差集本身作為基準(zhǔn)值計(jì)算百分比,而-diff_base
則是以指定的base profile,也就是這里的 profile001.pb.gz
指標(biāo)值作為基準(zhǔn)進(jìn)行計(jì)算 ( 67254614 / 31409035 = 2.141250566914902)。
到此這篇關(guān)于go tool pprof 參數(shù) ‘-base‘ 和 ‘-diff_base‘ 之間的區(qū)別的文章就介紹到這了,更多相關(guān)go tool pprof 參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言實(shí)戰(zhàn)之實(shí)現(xiàn)均衡器功能
這篇文章主要為大家詳細(xì)介紹了如何利用Golang?實(shí)現(xiàn)一個(gè)簡單的流浪均衡器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04golang?gorm實(shí)現(xiàn)get請求查詢案例測試
這篇文章主要為大家介紹了golang?gorm實(shí)現(xiàn)get請求查詢案例測試,2022-04-04Go語言如何使用golang-jwt/jwt/v4進(jìn)行JWT鑒權(quán)詳解
最近項(xiàng)目中需要用到鑒權(quán)機(jī)制,golang中jwt可以用,這篇文章主要給大家介紹了關(guān)于Go語言如何使用golang-jwt/jwt/v4進(jìn)行JWT鑒權(quán)的相關(guān)資料,需要的朋友可以參考下2022-09-09Go?WEB框架使用攔截器驗(yàn)證用戶登錄狀態(tài)實(shí)現(xiàn)
這篇文章主要為大家介紹了Go?WEB框架使用攔截器驗(yàn)證用戶登錄狀態(tài)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07