【GoLang】GoLang 单元测试、性能测试使用方法
单元测试代码:
ackage test import (
// "fmt"
"testing"
) func Test_FlowControl(t *testing.T) {
var x int64 =
if x == {
// 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 := ; 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
}
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)
}
测试所有的文件 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)
}
}
进行所有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
【GoLang】GoLang 单元测试、性能测试使用方法的更多相关文章
- golang 单元测试&&性能测试
一:单元测试 1.为什么要做单元测试和性能测试 减少bug 快速定位bug 减少调试时间 提高代码质量 2.golang的单元测试 单元测试代码的go文件必须以_test.go结尾 单元测试的函数名必 ...
- golang编译源代码和交叉编译方法
目录 golang编译源代码和交叉编译方法 编译源代码 编译go1.4 编译go1.12 交叉编译 golang编译源代码和交叉编译方法 编译源代码 golang编译其实很简单,下载一份最新的源代码后 ...
- golang init方法和main方法初始化顺序
init()和main()方法是golang默认的两个方法,不需要我们调用,程序执行会自动寻找项目中的这俩方法.现在我们就讲一种通用的情况:main 包下 导入了 init2 包而在init2 包下又 ...
- [Go] 单元测试/性能测试 (go test)
特征 Golang 单元测试对文件名和方法名,参数都有很严格的要求.例如: 1.文件名必须以 _test.go 结尾 2.方法名必须是 Test 开头 3.方法参数必须是 t *testing.T 或 ...
- 【NO.12-1】Jmeter - 在Linux执行性能测试的方法 [1]
前面讲过在Windows执行性能测试的方法,就是这篇了<jmeter - 一个完整的接口测试的脚本>, 在Windows执行性能测试之前,首先要有1个性能测试脚本嘛, 但是这个性能测试脚本 ...
- nmon-监控测试服务器 - Jmeter - 在Linux执行性能测试的方法 [2]
之所以把标题补充为<Jmeter - 在Linux执行性能测试的方法 [2]>,是因为在执行性能测试的过程中,我们需要关注的对象无非就是"测试服务器", 那么除了使用一些常见的观察服务器的 ...
- C# 单元测试几个方法的用法
单元测试的基本方法是调用被测代码的函数,输入函数的参数值,获取返回结果,然后与预期测试结果进行比较,如果相等则认为测试通过,否则认为测试不通过. 1.Assert类的使用 Assert.Inconcl ...
- 单元测试或main方法 进行单元测试时 idea检查其他类的语法是否正确的去除方法
在进行单元测试或者main方法时,在 运行/调试 设置中设置想要使用的测试单位的 before launch 即可
- [golang]golang如何覆盖输出console,实现进度条;golang一个骚气的进度提示库
[golang]golang如何覆盖输出console,实现进度条 package main import( "fmt" "os" "time&quo ...
随机推荐
- 【转】【Java】利用反射技术,实现对类的私有方法、变量访问
java关于反射机制的包主要在java.lang.reflect中,structs,hibernate,spring等框架都是基于java的反射机制. 下面是一个关于利用java的反射机制,实现了对私 ...
- Sphinx 2.2.6 window下安装全过程 未完 持续标记~~~~
由于在win8.1下安装 选的这个版本 Win64 binaries w/MySQL+PgSQL+libstemmer+id64 support 2.2.6-release 7.3M 下载页面 htt ...
- haproxy配置文件
haproxy配置文件 思路:读一行.写一行 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info de ...
- C# Redis消息队列例子
class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = ne ...
- Bootstrap学习------Tabel
Bootstrap的表格和Html表格大同小异,只是封装了一些css供我们使用 <table>标签必须引用class="table"基类样式,我们可以根据需求赛选需要的 ...
- 常用的MIME类型
.doc application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.d ...
- JavaScript数组属性与方法
Array 对象属性 属性 描述 constructor 返回对创建此对象的数组函数的引用. length 设置或返回数组中元素的数目. prototype 使您有能力向对象添加属性和方法. Arra ...
- spring 缓存(spring自带Cache)(入门)源码解读
spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...
- 清北暑假模拟day2 将
/* 爆搜,正解弃坑 */ #include<iostream> #include<cstdio> #include<string> #include<cst ...
- R语言练习(二)
op <- par(mfrow = c(2, 2)) #设置画布 p2 <- curve(x^2, 0, 1) #绘制曲线 legend("topleft", inse ...