准备工作:
1 go get github.com/ziutek/myMysqL/thrsafe
2 在MysqL建表和初始化数据(db是test)
drop table
if
exists admin;
@H_403_15@
CREATE TABLE `admin` (
@H_403_15@
`adminid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
@H_403_15@
`username` varchar(20) NOT NULL DEFAULT
''
COMMENT
'后台用户名'
,
@H_403_15@
`password` char(32) NOT NULL DEFAULT
'密码,md5存'
PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
@H_403_15@
PRIMARY KEY(`adminid`)
@H_403_15@
)
@H_403_15@
COMMENT=
'后台用户信息表'
@H_403_15@
COLLATE=
'utf8_general_ci'
@H_403_15@
ENGINE=InnoDB;
@H_403_15@
@H_403_15@
insert into admin set adminid=1,username=
'admin'
PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,password=
'21232f297a57a5a743894a0e4a801fc3'
;
@H_403_15@
@H_403_15@ |
3 gopath下建立myMysqL
4 myMysqL.go的代码:
package myMysqL
@H_403_15@
import(
@H_403_15@
"log"
@H_403_15@
"github.com/ziutek/myMysqL/MysqL"
@H_403_15@
_
"github.com/ziutek/myMysqL/native"
@H_403_15@
)
@H_403_15@
func getAdmin(adminid int) (string,string){
@H_403_15@
db := MysqL.New(
"tcp"
PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
""
"127.0.0.1:3306"
"root"
"password"
"test"
)
@H_403_15@
err := db.Connect()
@H_403_15@
err != nil {
@H_403_15@
panic(err)
@H_403_15@
}
@H_403_15@
rows,res,err := db.Query(
"select * from admin where adminid=%d"
PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,adminid)
@H_403_15@
err != nil {
@H_403_15@
panic(err)
@H_403_15@
}
@H_403_15@
len(rows) < 1 {
@H_403_15@
log.Panic(
"rows error"
)
@H_403_15@
}
@H_403_15@
row := rows[0]
@H_403_15@
first := res.Map(
"username"
)
@H_403_15@
second := res.Map(
)
@H_403_15@
username,password := row.Str(first),row.Str(second)
@H_403_15@
return
PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,password
@H_403_15@
|
5 myMysqL_test.go的代码:
"testing"
@H_403_15@
)
@H_403_15@
func Test_getAdmin(t *testing.T) {
@H_403_15@
PHP spaces" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_ := getAdmin(1)
@H_403_15@
|
写到这里你就可以在命令行中运行go test了
这里有个 -v参数,如果不加这个参数的话,只会显示错误的测试用例,否则就显示所有的测试用例(成功 + 错误)
6 下面做性能测试
import (
@H_403_15@
func Benchmark_getAdmin(b *testing.B){
@H_403_15@
for
i := 0; i < b.N; i++ {
//use b.N for looping
@H_403_15@
getAdmin(1)
@H_403_15@
}
@H_403_15@
然后运行 go test -v -bench=".*"
这里的-bench是可以指定运行的用例 返回结果表示这个测试用例在1s中内运行了2000次,每次调用大约用了891898ns 7 用性能测试生成CPU状态图使用命令: go test -bench=".*" -cpuprofile=cpu.prof -c cpuprofile是表示生成的cpu profile文件 -c是生成可执行的二进制文件,这个是生成状态图必须的,它会在本目录下生成可执行文件mymysql.test 然后使用go tool pprof工具 go tool pprof mymysql.test cpu.prof
调用web(需要安装graphviz) 猜你在找的Go相关文章 |