1. 初始化工作可以放在TestMain中,覆蓋原方法
2. 可以對文件更改其位置,比如err := beego.LoadAppConfig("ini", "../conf/app.conf"),更改配置文件地址
3. 對方法進行測試,只需選中方法,右擊generate->test for function,對文件進行測試,右擊generate->test for file
4. 運行時,要運行測試方法可以右擊測試方法,運行或者調試,運行文件時,右擊文件,可以選擇run
5. golang項目的測試覆蓋率 go test -cover,首先定位到要運行的測試文件目錄下
5.1 更詳細的數據可以先輸出到固定文件:go test -coverprofile=size_coverage.out,之後再對這文件進行展開分析:go tool cover -func=size_coverage.out
更加炫酷的查看可以是go tool cover -html=size_coverage.out,網頁中打開
6. 查看某個包被引用的熱力圖go test -covermode=count -coverprofile=count.out github.com\astaxie\beego,運行之後查看細節go tool cover -html=count.out
後面的包名就是要測試覆蓋效果的包名,可以用‘.’,代表當前包
壓力測試
1.go-stress-testing工具 運行:go run main.go -c 1 -n 100 -u https://www.baidu.com/,-c代表併發數,-n代表請求次數
2.可以使用golang自帶的壓測工具Benchmark,只需重寫func BenchmarkXXX方法,添加b.ReportAllocs(),右擊運行即可
go-stress-testing用來測試接口,Benchmark用來測試方法
3 pprof使用教程
1. 在main方法里加入 go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) //啓動http服務器}()
2. 啓動完成後,使用
go tool pprof http://localhost:6060/debug/pprof/heap // 獲取堆的相關數據
go tool pprof http://localhost:6060/debug/pprof/profile // 獲取30s內cpu的相關數據
go tool pprof http://localhost:6060/debug/pprof/block // 在你程序調用 runtime.SetBlockProfileRate ,查看goroutine阻塞的相關數據
go tool pprof http://localhost:6060/debug/pprof/mutex // 在你程序調用 runtime.SetMutexProfileFraction,查看誰佔用mutex
3. 在https中使用
go tool pprof https+insecure://localhost:8001/debug/pprof/heap將原來的http替換成https+insecure即可
4.web命令可以查看圖形化的調用情況
5.flat和flat%表示函數在 CPU 上運行的時間以及百分比,sum%當前所有函數累加使用 CPU 的比例,cum和cum%代表這個函數以及子函數運行所佔用的時間和比例, 也被稱爲累加值 cumulative
6.list +正則表達式,可以查看匹配函數的代碼以及每行代碼的耗時
4 火焰圖安裝
4.1 首先安裝uber開源的工具go get -v github.com/uber/go-torch
4.2 進入go-torch目錄,安裝FlameGraph,git clone https://github.com/brendangregg/FlameGraph.git
4.3 安裝pl文件打開工具ActivePerl
4.4 將perl和FlameGraph加入環境變量
4.5 go-torch -u http://localhost:6060 -t 30