目录
1.离线分析
其他方面的数据
2.在线分析
直接以HTTP方式启动服务
使用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
//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
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