Go自帶了測試框架和工具,在testing包中,以便完成單元測試(T類型)和性能測試(B類型)。
一般測試代碼放在*_test.go文件中,與被測代碼放于同一個包中。
單元測試
測試函數名稱格式是:Test[^a-z],即以Test開頭,跟上非小寫字母開頭的字符串。每個測試函數都接受一個*testing.T類型參數,用于輸出信息或中斷測試。
測試方法有:
- Fail: 標記失敗,但繼續執行當前測試函數
- FailNow: 失敗,立即終止當前測試函數執行
- Log: 輸出錯誤信息
- Error: Fail + Log
- Fatal: FailNow + Log
- Skip: 跳過當前函數,通常用于未完成的測試用例
測試代碼:
// filename: add_test.go
package test
import (
"testing"
)
func Add(a, b int) int {
return a + b
}
func TestAdd1(t *testing.T) {
if Add(2, 3) != 5 {
t.Error("result is wrong!")
} else {
t.Log("result is right")
}
}
func TestAdd2(t *testing.T) {
if Add(2, 3) != 6 {
t.Fatal("result is wrong!")
} else {
t.Log("result is right")
}
}
運行以下命令,自動搜集所有的測試文件(*_test.go),提取全部測試函數。
$ go test
輸出:
--- FAIL: TestAdd2 (0.00s)
add_test.go:20: result is wrong!
FAIL
exit status 1
FAIL _/Users/golang_learning/testTB 0.006s
輸出結果包括:出錯的測試函數名稱,執行時長和錯誤信息。
go test還有以下參數:
- -v:顯示所有測試函數運行細節
$ go test -v
輸出:
=== RUN TestAdd1
--- PASS: TestAdd1 (0.00s)
add_test.go:14: result is right
=== RUN TestAdd2
--- FAIL: TestAdd2 (0.00s)
add_test.go:20: result is wrong!
FAIL
exit status 1
FAIL _/Users/golang_learning/testTB 0.007s
- -run regex:指定要執行的測試函數
$ go test -run TestAdd2
輸出:
--- FAIL: TestAdd2 (0.00s)
add_test.go:20: result is wrong!
FAIL
exit status 1
FAIL _/Users/golang_learning/testTB 0.006s
性能測試
性能測試函數以Benchmark 開頭,參數類型是 *testing.B,可與Test函數放在同個文件中。默認情況下,go test不執行Benchmark測試,必須用“-bench <pattern>”指定性能測試函數。
測試代碼:
// filename: add_test.go
package test
import (
"testing"
)
func Add(a, b int) int {
return a + b
}
func Benchmark(b *testing.B) {
for i := 0; i < b.N; i++ { // b.N,測試循環次數
Add(4, 5)
}
}
執行命令測試:
$ go test -bench=.
輸出:
goos: darwin
goarch: amd64
Benchmark-4 2000000000 0.35 ns/op
PASS
ok _/Users/golang_learning/testTB 0.753s
B類型也有以下參數:
- benchmem:輸出內存分配統計
- benchtime:指定測試時間
- cpu:指定GOMAXPROCS
- timeout:超市限制
$ go test -v -bench=. -cpu=8 -benchtime="3s" -timeout="5s" -benchmem
輸出:
goos: darwin
goarch: amd64
Benchmark-8 5000000000 0.34 ns/op 0 B/op 0 allocs/op
PASS
ok _/Users/golang_learning/testTB 1.766s
- Benchmark-8:-cpu參數指定,-8表示8個CPU線程執行
- 5000000000:表示總共執行了5000000000次
- 0.34 ns/op:表示每次執行耗時0.34納秒
- 0 B/op:表示每次執行分配的內存(字節)
- 0 allocs/op:表示每次執行分配了多少次對象
pprof
go tools繼承了pprof,以便進行性能測試并找出瓶頸。
測試數據文件生成有兩種方法:命令行和代碼
1、命令行生成測試數據文件
$ go test -bench=. -cpuprofile cpu.out
輸出:
goos: darwin
goarch: amd64
Benchmark-4 2000000000 0.35 ns/op
PASS
ok _/Users/golang_learning/testTB 0.911s
$ ls
輸出:(上一條命令生成cpu.out和testTB.test)
add_test.go cpu.out testTB.test
用命令行分析
$ go tool pprof -text mem.out
輸出:
Main binary filename not available.
Type: inuse_space
Time: May 22, 2018 at 3:36pm (CST)
Showing nodes accounting for 1.16MB, 100% of 1.16MB total
flat flat% sum% cum cum%
1.16MB 100% 100% 1.16MB 100% runtime/pprof.StartCPUProfile
0 0% 100% 1.16MB 100% main.main
0 0% 100% 1.16MB 100% runtime.main
pprof交互模式分析
$ go tool pprof testTB.test cpu.out
輸出:
File: testTB.test
Type: cpu
Time: May 22, 2018 at 3:08pm (CST)
Duration: 906.10ms, Total samples = 670ms (73.94%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
輸出:
Showing nodes accounting for 670ms, 100% of 670ms total
flat flat% sum% cum cum%
670ms 100% 100% 670ms 100% _/Users/golang_learning/testTB.Benchmark
0 0% 100% 670ms 100% testing.(*B).launch
0 0% 100% 670ms 100% testing.(*B).runN
(pprof) quit
以上性能指標含義:
- 函數本地采樣數量(不包含其調用的其他函數)
- 函數本地采樣數量所占百分比
- 前幾個函數(包括當前函數)本地采樣總和所占百分比
- 函數(包括其調用的函數)采樣總數量
- 函數采樣總數量所占百分比
pprof交互模式命令:
- top、top10: 顯示前幾條信息;
- web:以svg文件展示。
pdf或者svg分析
$ go tool pprof -svg cpu.out > cpu.svg
$ go tool pprof -pdf cpu.out > cpu.pdf
$ ls(以上命令生成cpu.pdf和cpu.svg文件)
add_test.go cpu.out cpu.pdf cpu.svg testTB.test
打開cpu.pdf或者cpu.svg文件:
2、代碼生成測試數據文件
用runtime/pprof包生成cpu和mem文件:
// filename: pprof.go
package main
import (
"os"
"runtime/pprof"
)
func main() {
w, _ := os.Create("cpu.out")
defer w.Close()
pprof.StartCPUProfile(w)
defer pprof.StopCPUProfile()
w2, _ := os.Create("mem.out")
defer w2.Close()
defer pprof.WriteHeapProfile(w2)
Sum(3, 5)
}
func Sum(a, b int) int {
return a + b
}
編譯并執行以上文件:
$ go build pprof.go
$ ./pprof
$ ls(./pprof命令生成cpu.out和mem.out文件)
cpu.out mem.out pprof pprof.go
生成pdf文件:
$ go tool pprof -pdf cpu.out > cpu.pdf
$ go tool pprof -pdf mem.out > mem.pdf
net/http/pprof
若是測量http server的性能,則需要使用net/http/pprof包。只需要在源文件中加入代碼:
import (
_ "net/http/pprof"
)
CPU分析
啟動http server,然后在瀏覽器中訪問鏈接:http://localhost:【port】/debug/pprof/
就能看到prof信息了:
若要生成CPU狀態分析圖,可使用以下命令:
$ go tool pprof http://localhost:port/debug/pprof/profile
就會進入30秒的profile收集時間,在這段事件內向服務器連續發送多次請求,盡量讓cpu占用性能產生數據。
$ go tool pprof http://localhost:8080/debug/pprof/profile
輸出:
Fetching profile over HTTP from http://localhost:8080/debug/pprof/profile
// 這里會阻塞30s收集profile,在這段時間應多發請求
Saved profile in /Users/pprof/pprof.samples.cpu.002.pb.gz
Type: cpu
Time: May 22, 2018 at 7:16pm (CST)
Duration: 30s, Total samples = 330ms ( 1.10%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top10
輸出:(前10條信息)
Showing nodes accounting for 330ms, 100% of 330ms total
Showing top 10 nodes out of 39
flat flat% sum% cum cum%
100ms 30.30% 30.30% 100ms 30.30% runtime.mach_semaphore_signal
60ms 18.18% 48.48% 60ms 18.18% runtime.mach_semaphore_timedwait
60ms 18.18% 66.67% 60ms 18.18% runtime.mach_semaphore_wait
... ...
(pprof) web
// 在瀏覽器中以svg文件的方式展示cpu占用圖
內存分析
啟動服務器,訪問鏈接http://localhost:port/debug/pprof/heap,就會自動下載heap文件,調用以下命令,進入pprof交互模式:
$ go tool pprof heap
Main binary filename not available.
Type: inuse_space
Time: May 24, 2018 at 11:30pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top5
Showing nodes accounting for 62.02MB, 98.20% of 63.16MB total
Showing top 5 nodes out of 26
flat flat% sum% cum cum%
21.67MB 34.31% 34.31% 21.67MB 34.31% bytes.makeSlice
20.50MB 32.46% 66.77% 20.50MB 32.46% reflect.unsafe_New
15MB 23.75% 90.52% 15MB 23.75% github.com/golang/protobuf/proto.(*word32Slice).Append
2.85MB 4.51% 95.03% 2.85MB 4.51% github.com/golang/protobuf/proto.(*structPointerSlice).Append
2MB 3.17% 98.20% 2MB 3.17% github.com/golang/protobuf/proto.word32_Set
(pprof) web