准备工作:
1 go get github.com/ziutek/myMysqL/thrsafe
2 在MysqL建表和初始化数据(db是test)
1
2
3
4
5
6
7
8
9
10
11
12
|
drop table
if
exists admin;
CREATE TABLE `admin` (
`adminid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`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,
PRIMARY KEY(`adminid`)
)
COLLATE=
'utf8_general_ci'
ENGINE=InnoDB;
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'
;
|
3 gopath下建立myMysqL
4 myMysqL.go的代码:
package myMysqL
import(
"log"
_
"github.com/ziutek/myMysqL/native"
)
func getAdmin(adminid int) (string,string){
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"
)
err := db.Connect()
err != nil {
panic(err)
}
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)
err != nil {
panic(err)
}
len(rows) < 1 {
log.Panic(
"rows error"
)
}
row := rows[0]
first := res.Map(
"username"
)
second := res.Map(
)
username,password := row.Str(first),row.Str(second)
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
}
|
5 myMysqL_test.go的代码:
"testing"
)
func Test_getAdmin(t *testing.T) {
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)
(username !=
"admin"
) {
t.Error(
"getAdmin get data error"
}
}<br>这里做单元测试的,测试getAdmin函数
|
写到这里你就可以在命令行中运行go test了
这里有个 -v参数,如果不加这个参数的话,只会显示错误的测试用例,否则就显示所有的测试用例(成功 + 错误)
6 下面做性能测试
import (
func Benchmark_getAdmin(b *testing.B){
for
i := 0; i < b.N; i++ {
//use b.N for looping
getAdmin(1)
}
然后运行 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相关文章 |