go语言基准测试对比c,Go语言单元和性能测试的相关文档

为了帮助开发者对代码进行测试,Go语言也提供了相关的单元测试的基础框架。除此之外,Go语言还提供了简单的性能测试框架,给开发者提供了对比和改善算法的便利手段。Go语言的性能测试框架据说是参考了2002年JavaOne的《How NOT To Write A Microbenchmark》,它的基本测试机理是在一定时间内循环运行测试程序,并最终得出测试程序每次运行的平均时间。不仅如此,性能测试框架还支持输出用于性能调优用的了cpu和内存相关数据。从这方面看,我觉得要比Java、Scala好。[@more@]

单元测试

单元测试很重要,Go语言提供了相关的单元测试框架。允许对单个和一系列文件进行自动化测试。

Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the “go test” command, which automates execution of any function of the form

测试用例写法函数名称必须以Test开头,例如func TestXxx(*testing.T)

where Xxx can be any alphanumeric string (but the first letter must not be in [a-z]) and serves to identify the test routine. These TestXxx routines should be declared within the package they are testing.

go文件命名

必须是_test.go为后缀(*_test.go)。此外,package名最好不要用main.

举例,funcs_test.go

package mytest

import "testing"

func TestSomething(t *testing.T) {

//call some function

}

func TestAnotherthing(t *testing.T) {

//call some function

}

测试所有的文件

go test

将对当前目录下的所有*_test.go文件进行编译并自动运行测试。

测试某个文件

使用”-file”参数。go test –file **.go。例如,

go test -file b_test.go

”-file”参数不是必须的,可以省略。如果你输入go test b_test.go也会得到一样的效果

warning: GOPATH set to GOROOT (c:go) has no effect

PASS

ok _/F_/workspace/goSample01/src/test 0.359s

测试某个方法

go test -run=’ TestSomething’

Benchmark性能测试:

性能测试对于对比和改进算法很有帮助。因此,Go语言提供了相关的函数性能测试框架。当然,Go只是提供了基本测试框架,真正的测试代码还得由程序员来完成。

Benchmark测试用例写法:函数命名必须以Benchmark打头。例如,func BenchmarkXxx(*testing.B)

are considered benchmarks, and are executed by the "go test" command when the -test.bench flag is provided.

A sample benchmark function looks like this:func BenchmarkHello(b *testing.B) {for i := 0; i < b.N; i++ {fmt.Sprintf("hello")}}

对目录下所有go文件进行benchmark测试

在命令行里输入:go test . -bench="."

(注意:同一个包里面不能有相同的方法名。否则,可能会编译出错)

F:workspacegoSample01srctest>go test . -bench="."

warning: GOPATH set to GOROOT (c:go) has no effect

PASS

BenchmarkUpdate 500000 4906 ns/op

BenchmarkManual 200000 8750 ns/op

BenchmarkUnroll 500000 4531 ns/op

BenchmarkLoop 2000000000 0.00 ns/op

BenchmarkIteratively 1000000 2078 ns/op

单独对某个go文件进行benchmark测试在命令行里输入:go test b_test.go -bench="."

F:workspacegoSample01srctest>go test b_test.go -bench="."

warning: GOPATH set to GOROOT (c:go) has no effect

PASS

BenchmarkUpdate 500000 4562 ns/op

BenchmarkManual 200000 9062 ns/op

BenchmarkUnroll 500000 4187 ns/op

ok command-line-arguments 6.953s

性能测试结果的一些说明:

1、循环次数和每次操作的时间

The benchmark package will vary b.N until the benchmark function lasts long enough to be timed reliably. The outputtesting.BenchmarkHello 10000000 282 ns/op

means that the loop ran 10000000 times at a speed of 282 ns per loop.

2、计时器可以手工控制

If a benchmark needs some expensive setup before running, the timer may be stopped:来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/88031/viewspace-1058532/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值