【GoLang】GoLang 单元测试、性能测试使用方法
2016-11-16 18:57
597 查看
单元测试代码:
性能测试代码:
1.创建测试文件夹mysql,文件夹下的go文件的package必须与文件夹名一致(不然会识别不到)
2.创建需要测试的文件mysql.go(使用github.com/go-sql-driver/mysql包)
View Code
3.创建单元测试用例文件mysql_test.go(文件名必须是*_test.go的类型,*代表要测试的文件名,函数名必须以Test开头如:TestXxx或Test_xxx)
View Code
4.创建benchmark性能测试用例文件mysql_b_test.go(文件名必须是*_b_test.go的类型,*代表要测试的文件名,函数名必须以Benchmark开头如:BenchmarkXxx或Benchmark_xxx)
View Code
5.用性能测试生成CPU状态图(暂未测试使用)
ackage test import ( // "fmt" "testing" ) func Test_FlowControl(t *testing.T) { var x int64 = 10 if x == 10 { // fmt.Println("x is 10") t.Log("x is 10") } else { // fmt.Println("x is not 10") t.Log("x is not 10") } t.Log(x) }
性能测试代码:
package test import ( // "fmt" "testing" ) func Benchmark_FlowControl(b *testing.B) { for i := 0; i < b.N; i++ { //use b.N for looping b.Log(i) } }
1.创建测试文件夹mysql,文件夹下的go文件的package必须与文件夹名一致(不然会识别不到)
2.创建需要测试的文件mysql.go(使用github.com/go-sql-driver/mysql包)
package mysql import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) func findByPk(pk int) int { var num int = 0 db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/plugin_master?charset=utf8") if err != nil { panic(err.Error()) } defer db.Close() stmtOut, err := db.Prepare("select id from t_admin where id=?") if err != nil { panic(err.Error()) } defer stmtOut.Close() err = stmtOut.QueryRow(pk).Scan(&num) if err != nil { panic(err.Error()) } return num }
View Code
3.创建单元测试用例文件mysql_test.go(文件名必须是*_test.go的类型,*代表要测试的文件名,函数名必须以Test开头如:TestXxx或Test_xxx)
package mysql import ( "testing" ) func Test_findByPk(t *testing.T) { num := findByPk(1) t.Log(num) }
View Code
测试所有的文件 go test,将对当前目录下的所有*_test.go文件进行编译并自动运行测试。
测试某个文件使用”-file”参数。go test –file *.go 。例如:go test -file mysql_test.go,"-file"参数不是必须的,可以省略,如果你输入go test b_test.go也会得到一样的效果。
测试某个方法 go test -run='Test_xxx' "-v" 参数 go test -v ... 表示无论用例是否测试通过都会显示结果,不加"-v"表示只显示未通过的用例结果
4.创建benchmark性能测试用例文件mysql_b_test.go(文件名必须是*_b_test.go的类型,*代表要测试的文件名,函数名必须以Benchmark开头如:BenchmarkXxx或Benchmark_xxx)
package mysql import ( "testing" ) func Benchmark_findByPk(b *testing.B) { for i := 0; i < b.N; i++ { //use b.N for looping findByPk(1) } }
View Code
进行所有go文件的benchmark测试 go test -bench=".*" 或 go test . -bench=".*"
对某个go文件进行benchmark测试 go test mysql_b_test.go -bench=".*"
5.用性能测试生成CPU状态图(暂未测试使用)
使用命令:
go test -bench=".*" -cpuprofile=cpu.prof -c
cpuprofile是表示生成的cpu profile文件
-c是生成可执行的二进制文件,这个是生成状态图必须的,它会在本目录下生成可执行文件mysql.test
然后使用go tool pprof工具
go tool pprof mysql.test cpu.prof
调用web(需要安装graphviz)来生成svg文件,生成后使用浏览器查看svg文件 参考 http://www.cnblogs.com/yjf512/archive/2013/01/18/2865915.html 参考资料: http://studygolang.com/articles/2491 http://www.01happy.com/golang-unit-testing/ http://www.cnblogs.com/yjf512/archive/2013/01/18/2865915.html http://blog.csdn.net/samxx8/article/details/46894587 http://www.phpddt.com/go/go-testing.html
相关文章推荐
- 使用 BenchmarkDotnet 测试代码性能 【Win10】单元测试中捕获异步方法的指定异常
- Win7下的使用QTP进行猴子测试的性能日志实现方法
- 性能测试监控工具nmon安装及使用方法
- Benchmark Web App 性能瓶颈分析与性能测试工具的使用方法总结
- Golang 单元测试和性能测试
- Golang 单元测试和性能测试
- 网站压力测试方法- Apache自带的性能测试工具ab的使用
- linux 性能测试工具Lmbench 使用方法
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法?
- 性能测试监控工具nmon安装及使用方法
- 《杉岩两语》:Vdbench在分布式存储性能测试中的使用方法
- 嵌入式开发中网络性能测试工具iperf的使用方法和注意事项
- Loadrunner录制使用ajax性能测试脚本方法
- Android性能测试工具 使用方法
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法?[转载]
- 使用loadrunner Java Vuser协议进行性能测试时场景中报Error: Compilation process failed.解决方法
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法? 转
- 使用unixbench测试你VPS性能的方法
- golang 单元测试、性能测试、性能监控技术
- 性能测试监控工具nmon安装及使用方法