golang pprof 簡單入門

 

目錄

 

1.離線分析

其他方面的數據

2.在線分析

直接以HTTP方式啓動服務


1.離線分析

使用benchmark或者test,生產cpu.pprof文件,然後使用go tool來查看

//range_test.go
package main

import (
	"testing"
)

var SliceSize = 1000000

func AppendSliceToSize(n int) []int {
	var slice []int
	for i := 0; i < n; i++ {
		slice = append(slice, i)
	}
	return slice
}
func AppendSliceToSizeAlloc(n int) []int {
	var slice []int = make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = i
	}
	return slice
}
func BenchmarkSlice(b *testing.B) {
	for i := 0; i < b.N; i++ {
		AppendSliceToSize(SliceSize)
	}
}
func BenchmarkAllocSlice(b *testing.B) {
	for i := 0; i < b.N; i++ {
		AppendSliceToSizeAlloc(SliceSize)
	}
}
#啓動測試
go test -v -bench=Range -cpuprofile=cpu.prof range_test.go 
goos: linux
goarch: amd64
BenchmarkRange
BenchmarkRange-8          127400              9382 ns/op
PASS
ok      command-line-arguments  1.410s

#啓動界面分析
go tool pprof -http=:8080 cpu.prof
Serving web UI on http://localhost:8080

-cpuprofile=cpu.prof就指定生成cpu的數據文件。

訪問:http://127.0.0.1:8080/ui/

得到如下圖。分析了每個函數的執行時間。可以看出,時間都花費在duffcopy上。同樣,方框越大說明耗時越久。

 

界面中由很多很細的分析

 

 

其他方面的數據

可以看出還有:blockprofile cpuprofile memprofile mutexprofile等等

-test.bench regexp
        run only benchmarks matching regexp
  -test.benchmem
        print memory allocations for benchmarks
  -test.benchtime d
        run each benchmark for duration d (default 1s)
  -test.blockprofile file
        write a goroutine blocking profile to file
  -test.blockprofilerate rate
        set blocking profile rate (see runtime.SetBlockProfileRate) (default 1)
  -test.count n
        run tests and benchmarks n times (default 1)
  -test.coverprofile file
        write a coverage profile to file
  -test.cpu list
        comma-separated list of cpu counts to run each test with
  -test.cpuprofile file
        write a cpu profile to file
  -test.failfast
        do not start new tests after the first test failure
  -test.list regexp
        list tests, examples, and benchmarks matching regexp then exit
  -test.memprofile file
        write an allocation profile to file
  -test.memprofilerate rate
        set memory allocation profiling rate (see runtime.MemProfileRate)
  -test.mutexprofile string
        write a mutex contention profile to the named file after execution
  -test.mutexprofilefraction int
        if >= 0, calls runtime.SetMutexProfileFraction() (default 1)
  -test.outputdir dir
        write profiles to dir
  -test.parallel n
        run at most n tests in parallel (default 8)
  -test.run regexp
        run only tests and examples matching regexp
  -test.short
        run smaller test suite to save time
  -test.testlogfile file
        write test action log to file (for use only by cmd/go)
  -test.timeout d
        panic test binary after duration d (default 0, timeout disabled)
  -test.trace file
        write an execution trace to file
  -test.v
        verbose: print additional output

 

還是第一節中的代碼

go test -v -bench=Range -memprofile=mem.prof  range_test.go 
goos: linux
goarch: amd64
BenchmarkRange
BenchmarkRange-8          107365              9465 ns/op
PASS
ok      command-line-arguments  1.139s



go tool pprof -http=:8080 mem.prof 
Serving web UI on http://localhost:8080

 

2.在線分析

//pprof.go
package main

import (
	"log"
	"net/http"
	_ "net/http/pprof"
	"time"
)

func run() {
	go func() {
		for {
			var slice []int
			for i := 0; i < 100000; i++ {
				slice = append(slice, i)
			}
			time.Sleep(time.Millisecond * 10)
		}
	}()
}
func main() {
	run()
	log.Println(http.ListenAndServe("localhost:8080", nil))
}
go run pprof.go 

http://127.0.0.1:8080/debug/pprof/

然而這裏沒有圖形化分析,可以使用go tool命令行工具來查看。

#cpuprof
go tool pprof http://localhost:8080/debug/pprof/profile?seconds=30
Fetching profile over HTTP from http://localhost:8080/debug/pprof/profile?seconds=30
Saved profile in /home/${usr_name}/pprof/pprof.pprof.samples.cpu.002.pb.gz
File: pprof
Type: cpu
Time: May 31, 2020 at 12:36pm (CST)
Duration: 30.14s, Total samples = 6.81s (22.59%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 4790ms, 70.34% of 6810ms total
Dropped 76 nodes (cum <= 34.05ms)
Showing top 10 nodes out of 126
      flat  flat%   sum%        cum   cum%
    1440ms 21.15% 21.15%     1440ms 21.15%  runtime.memmove
    1420ms 20.85% 42.00%     1420ms 20.85%  runtime.futex
     640ms  9.40% 51.40%      640ms  9.40%  runtime.memclrNoHeapPointers
     380ms  5.58% 56.98%      650ms  9.54%  runtime.scanobject
     190ms  2.79% 59.77%      340ms  4.99%  runtime.scanblock
     190ms  2.79% 62.56%      190ms  2.79%  runtime.usleep
     150ms  2.20% 64.76%      150ms  2.20%  runtime.epollwait
     140ms  2.06% 66.81%      140ms  2.06%  runtime.madvise
     130ms  1.91% 68.72%      150ms  2.20%  runtime.findObject
     110ms  1.62% 70.34%      110ms  1.62%  runtime.write1

 

 

 

直接以HTTP方式啓動服務

8080端口是服務端口,而8888是分析結果UI的web端口。

go tool pprof -http :8888  http://127.0.0.1:8080/debug/pprof/heap
Fetching profile over HTTP from http://127.0.0.1:8080/debug/pprof/heap
Saved profile in /home/${usr_name}/pprof/pprof.pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.006.pb.gz
Serving web UI on http://localhost:8888

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章