開發程序其中很重要的一點是測試,咱們如何保證代碼的質量,如何保證每一個函數是可運行,運行結果是正確的,又如何保證寫出來的代碼性能是好的,咱們知道單元測試的重點在於發現程序設計或實現的邏輯錯誤,使問題及早暴露,便於問題的定位解決,而性能測試的重點在於發現程序設計上的一些問題,讓線上的程序可以在高併發的狀況下還能保持穩定。本小節將帶着這一連串的問題來說解Go語言中如何來實現單元測試和性能測試。git
Go語言中自帶有一個輕量級的測試框架testing
和自帶的go test
命令來實現單元測試和性能測試,testing
框架和其餘語言中的測試框架相似,你能夠基於這個框架寫針對相應函數的測試用例,也能夠基於該框架寫相應的壓力測試用例,那麼接下來讓咱們一一來看一下怎麼寫。github
因爲go test
命令只能在一個相應的目錄下執行全部文件,因此咱們接下來新建一個項目目錄gotest
,這樣咱們全部的代碼和測試代碼都在這個目錄下。golang
接下來咱們在該目錄下面建立兩個文件:gotest.go和gotest_test.goweb
gotest.go:這個文件裏面咱們是建立了一個包,裏面有一個函數實現了除法運算:數據庫
package gotest
import (
"errors"
)
func Division(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("除數不能爲0")
}
return a / b, nil
}
gotest_test.go:這是咱們的單元測試文件,可是記住下面的這些原則:ruby
_test.go
結尾的,這樣在執行go test
的時候纔會執行到相應的代碼testing
這個包Test
開頭TestXxx()
的參數是testing.T
,咱們可使用該類型來記錄錯誤或者是測試狀態func TestXxx (t *testing.T)
,Xxx
部分能夠爲任意的字母數字的組合,可是首字母不能是小寫字母[a-z],例如Testintdiv
是錯誤的函數名。testing.T
的Error
, Errorf
, FailNow
, Fatal
, FatalIf
方法,說明測試不經過,調用Log
方法用來記錄測試的信息。下面是咱們的測試用例的代碼:併發
package gotest
import (
"testing"
)
func Test_Division_1(t *testing.T) {
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
t.Error("除法函數測試沒經過") // 若是不是如預期的那麼就報錯
} else {
t.Log("第一個測試經過了") //記錄一些你指望記錄的信息
}
}
func Test_Division_2(t *testing.T) {
t.Error("就是不經過")
}
咱們在項目目錄下面執行go test
,就會顯示以下信息:app
--- FAIL: Test_Division_2 (0.00 seconds)
gotest_test.go:16: 就是不經過
FAIL
exit status 1
FAIL gotest 0.013s
從這個結果顯示測試沒有經過,由於在第二個測試函數中咱們寫死了測試不經過的代碼t.Error
,那麼咱們的第一個函數執行的狀況怎麼樣呢?默認狀況下執行go test
是不會顯示測試經過的信息的,咱們須要帶上參數go test -v
,這樣就會顯示以下信息:框架
=== RUN Test_Division_1
--- PASS: Test_Division_1 (0.00 seconds)
gotest_test.go:11: 第一個測試經過了
=== RUN Test_Division_2
--- FAIL: Test_Division_2 (0.00 seconds)
gotest_test.go:16: 就是不經過
FAIL
exit status 1
FAIL gotest 0.012s
上面的輸出詳細的展現了這個測試的過程,咱們看到測試函數1Test_Division_1
測試經過,而測試函數2Test_Division_2
測試失敗了,最後得出結論測試不經過。接下來咱們把測試函數2修改爲以下代碼:函數
func Test_Division_2(t *testing.T) {
if _, e := Division(6, 0); e == nil { //try a unit test on function
t.Error("Division did not work as expected.") // 若是不是如預期的那麼就報錯
} else {
t.Log("one test passed.", e) //記錄一些你指望記錄的信息
}
}
而後咱們執行go test -v
,就顯示以下信息,測試經過了:
=== RUN Test_Division_1
--- PASS: Test_Division_1 (0.00 seconds)
gotest_test.go:11: 第一個測試經過了
=== RUN Test_Division_2
--- PASS: Test_Division_2 (0.00 seconds)
gotest_test.go:20: one test passed. 除數不能爲0
PASS
ok gotest 0.013s
壓力測試用來檢測函數(方法)的性能,和編寫單元功能測試的方法相似,此處再也不贅述,但須要注意如下幾點:
壓力測試用例必須遵循以下格式,其中XXX能夠是任意字母數字的組合,可是首字母不能是小寫字母
func BenchmarkXXX(b *testing.B) { ... }
go test
不會默認執行壓力測試的函數,若是要執行壓力測試須要帶上參數-test.bench
,語法:-test.bench="test_name_regex"
,例如go test -test.bench=".*"
表示測試所有的壓力測試函數
testing.B.N
,以使測試能夠正常的運行_test.go
結尾下面咱們新建一個壓力測試文件webbench_test.go,代碼以下所示:
package gotest
import (
"testing"
)
func Benchmark_Division(b *testing.B) {
for i := 0; i < b.N; i++ { //use b.N for looping
Division(4, 5)
}
}
func Benchmark_TimeConsumingFunction(b *testing.B) {
b.StopTimer() //調用該函數中止壓力測試的時間計數
//作一些初始化的工做,例如讀取文件數據,數據庫鏈接之類的,
//這樣這些時間不影響咱們測試函數自己的性能
b.StartTimer() //從新開始時間
for i := 0; i < b.N; i++ {
Division(4, 5)
}
}
咱們執行命令go test -test.bench=".*"
,能夠看到以下結果:
PASS
Benchmark_Division 500000000 7.76 ns/op
Benchmark_TimeConsumingFunction 500000000 7.80 ns/op
ok gotest 9.364s
上面的結果顯示咱們沒有執行任何TestXXX
的單元測試函數,顯示的結果只執行了壓力測試函數,第一條顯示了Benchmark_Division
執行了500000000次,每次的執行平均時間是7.76納秒,第二條顯示了Benchmark_TimeConsumingFunction
執行了500000000,每次的平均執行時間是7.80納秒。最後一條顯示總共的執行時間。
咱們執行命令go test -test.bench=".*" -count=5
,能夠看到以下結果: (使用-count能夠指定執行多少次)
PASS Benchmark_Division-2 300000000 4.60 ns/op Benchmark_Division-2 300000000 4.57 ns/op Benchmark_Division-2 300000000 4.63 ns/op Benchmark_Division-2 300000000 4.60 ns/op Benchmark_Division-2 300000000 4.63 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.64 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.61 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.60 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.59 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.60 ns/op ok _/home/diego/GoWork/src/app/testing 18.546s
go test -run=文件名字 -bench=bench名字 -cpuprofile=生產的cprofile文件名稱 文件夾
例子:
testBenchMark下有個popcnt文件夾,popcnt中有文件popcunt_test.go
➜ testBenchMark ls
popcnt
popcunt_test.go的問價內容:
ackage popcnt import ( "testing" ) const m1 = 0x5555555555555555 const m2 = 0x3333333333333333 const m4 = 0x0f0f0f0f0f0f0f0f const h01 = 0x0101010101010101 func popcnt(x uint64) uint64 { x -= (x >> 1) & m1 x = (x & m2) + ((x >> 2) & m2) x = (x + (x >> 4)) & m4 return (x * h01) >> 56 } func BenchmarkPopcnt(b *testing.B) { for i := 0; i < b.N; i++ { x := i x -= (x >> 1) & m1 x = (x & m2) + ((x >> 2) & m2) x = (x + (x >> 4)) & m4 _ = (x * h01) >> 56 } }
而後運行go test -bench=".*" -cpuprofile=cpu.profile ./popcnt
➜ testBenchMark go test -bench=".*" -cpuprofile=cpu.profile ./popcnt testing: warning: no tests to run PASS BenchmarkPopcnt-8 1000000000 2.01 ns/op ok app/testBenchMark/popcnt 2.219s ➜ testBenchMark ll total 6704 drwxr-xr-x 5 diego staff 170 5 6 13:57 . drwxr-xr-x 3 diego staff 102 5 6 11:12 .. -rw-r--r-- 1 diego staff 5200 5 6 13:57 cpu.profile drwxr-xr-x 4 diego staff 136 5 6 11:47 popcnt -rwxr-xr-x 1 diego staff 3424176 5 6 13:57 popcnt.test ➜ testBenchMark
生產 cpu.profile問價和popcnt.test 文件
➜ testBenchMark ll total 6704 drwxr-xr-x 5 diego staff 170 5 6 13:57 . drwxr-xr-x 3 diego staff 102 5 6 11:12 .. -rw-r--r-- 1 diego staff 5200 5 6 13:57 cpu.profile drwxr-xr-x 3 diego staff 102 5 6 14:01 popcnt -rwxr-xr-x 1 diego staff 3424176 5 6 13:57 popcnt.test ➜ testBenchMark
go tool pprof popcnt.test cpu.profile 進入交互模式
➜ testBenchMark go tool pprof popcnt.test cpu.profile Entering interactive mode (type "help" for commands) (pprof) top 1880ms of 1880ms total ( 100%) flat flat% sum% cum cum% 1790ms 95.21% 95.21% 1790ms 95.21% app/testBenchMark/popcnt.BenchmarkPopcnt 90ms 4.79% 100% 90ms 4.79% runtime.usleep 0 0% 100% 1790ms 95.21% runtime.goexit 0 0% 100% 90ms 4.79% runtime.mstart 0 0% 100% 90ms 4.79% runtime.mstart1 0 0% 100% 90ms 4.79% runtime.sysmon 0 0% 100% 1790ms 95.21% testing.(*B).launch 0 0% 100% 1790ms 95.21% testing.(*B).runN (pprof)
go tool pprof --web popcnt.test cpu.profile 進入web模式
$ go tool pprof --text mybin http://myserver:6060:/debug/pprof/profile
這有幾個可用的輸出類型,最有用的幾個爲: --text,--web 和 --list 。運行 go tool pprof
來獲得最完整的列表。
經過上面對單元測試和壓力測試的學習,咱們能夠看到testing
包很輕量,編寫單元測試和壓力測試用例很是簡單,配合內置的go test
命令就能夠很是方便的進行測試,這樣在咱們每次修改完代碼,執行一下go test就能夠簡單的完成迴歸測試了。