Skip to content

Commit 79663c0

Browse files
Increased performance analysis capabilities.
1 parent 15373b7 commit 79663c0

File tree

4 files changed

+2842
-0
lines changed

4 files changed

+2842
-0
lines changed

bench_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ package struc
33
import (
44
"bytes"
55
"encoding/binary"
6+
"net/http"
7+
_ "net/http/pprof"
68
"testing"
79
)
810

11+
func init() {
12+
// 启动 pprof http 服务
13+
go func() {
14+
println("Starting pprof server on :6060")
15+
println(http.ListenAndServe("localhost:6060", nil))
16+
}()
17+
}
18+
919
type BenchExample struct {
1020
Test [5]byte
1121
A int32

pprof/bench_pprof.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# 运行解码基准测试
3+
go test -bench=Decode -benchmem -cpuprofile=cpu_decode.prof -memprofile=mem_decode.prof -trace=trace_decode.out
4+
5+
# 运行编码基准测试
6+
go test -bench=Encode -benchmem -cpuprofile=cpu_encode.prof -memprofile=mem_encode.prof -trace=trace_encode.out
7+
8+
# 查看基准测试结果
9+
go tool pprof -http=:8080 cpu_decode.prof
10+
go tool pprof -http=:8080 mem_decode.prof
11+
go tool trace trace_decode.out
12+
13+
go tool pprof -http=:8080 cpu_encode.prof
14+
go tool pprof -http=:8080 mem_encode.prof
15+
go tool trace trace_encode.out
16+
17+
# 转换为svg
18+
go tool pprof -svg cpu_decode.prof > cpu_decode.svg
19+
go tool pprof -svg mem_decode.prof > mem_decode.svg
20+
go tool pprof -svg cpu_encode.prof > cpu_encode.svg
21+
go tool pprof -svg mem_encode.prof > mem_encode.svg
22+
23+
24+
# 查看trace
25+
go tool trace trace_decode.out
26+
go tool trace trace_encode.out
27+
28+
# 查看 text
29+
go tool pprof -text cpu_decode.prof
30+
go tool pprof -text mem_decode.prof
31+
go tool pprof -text cpu_encode.prof
32+
go tool pprof -text mem_encode.prof

pprof/cpu_encode.png

683 KB
Loading

0 commit comments

Comments
 (0)