Go testing使用

性能测试
go test -bench=.*
  • 代码:
func BenchmarkQueryYellowPagesMessageWithStaff(b *testing.B) {
    var yps YellowPagesStorage

    // 获得连接数据库参数
    mysqlUtility.DB_CONF = getDbConfig()

    // 打开数据库连接
    mysqlUtility.DBConn = mysqlUtility.ConnectToDB()
    // alog初始化
    // alog.RegisterAlog("../../apps/web/conf/config.yaml")
    // alog.SetLogTag("XG")

    // 测试方法
    for i := 0; i < b.N; i++ {
        yps.QueryYellowPagesMessageWithStaff("测", 1, 1)
    }

    // 关闭数据库连接
    mysqlUtility.DBConn.Close()
}
  • 结果:
HaoCdeMacBook-Pro:apiphone haoc$ go test -bench=.*
2017/03/31 16:38:01 [I] 数据库初始化成功... 
2017/03/31 16:38:01 [I] 数据库初始化成功... 
BenchmarkQueryYellowPagesMessageWithStaff-8     2017/03/31 16:38:02 [I] 数据库初始化成功... 
       2     560568385 ns/op
PASS
ok      AntLinkCampus/CampusServer/storage/apiphone 2.413s
测试单个方法
go test -v -test.run TestQueryYellowPagesMessageWithStaff
  • 代码:
func TestQueryYellowPagesMessageWithStaff(t *testing.T) {
    var yps YellowPagesStorage

    // 获得数据库连接参数
    mysqlUtility.DB_CONF = getDbConfig()

    // 打开数据库连接
    mysqlUtility.DBConn = mysqlUtility.ConnectToDB()

    // alog初始化
    alog.RegisterAlog("../../apps/web/conf/config.yaml")
    alog.SetLogTag("XG")

    // 测试方法
    yps.QueryYellowPagesMessageWithStaff("测", 1, 1)

    // 关闭数据库连接
    mysqlUtility.DBConn.Close()
}
  • 结果:
HaoCdeMacBook-Pro:apiphone haoc$ go test -v -test.run TestQueryYellowPagesMessageWithStaff
=== RUN   TestQueryYellowPagesMessageWithStaff
2017/03/31 16:42:14 [I] 数据库初始化成功... 
--- PASS: TestQueryYellowPagesMessageWithStaff (0.57s)
PASS
ok      AntLinkCampus/CampusServer/storage/apiphone 0.580s

你可能感兴趣的:(Go testing使用)