go通过benchmark对代码进行性能测试详解(Go通过名称获取进程)满满干货

随心笔谈12个月前发布 admin
89 0



目录benchmark的使用运行Benchmark指定测试时长或测试次数重置时间和暂停计时查看内存使用情况testing.B的底层实现总结

在开发中我们要想编写高性能的代码,或者优化代码的性能时,你首先得知道当前代码的性能,在go中可以使用testing包的benchmark来做基准测试 ,首先我们写一个简单的返回随机字符串的方法

func randomStr(length int) string {
mathRand.Seed(time.Now().UnixNano())
letters :=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
b :=make([]byte, length)
for i :=range b {
b[i]=letters[mathRand.Intn(len(letters))]
}
return string(b)
}

要对上面的代码做基准测试,首先我们要新建一个测试文件,比如,然后新建一个基准测试方法,与普通的测试函数Test 开头,参数为t *testing.T类似,基准测试函数要以Benchmark开头,参数为b *testing.B,代码中的代表的是该用例的运行次数,这个值是会变的,对于每个用例都不一样,这个值会从1开始增加,具体的实现我会在下面的实现原理里进行介绍。

func BenchmarkRandomStr(b *testing.B) {
for i :=0; i < b.N; i++ {
randomStr(10000)
}
}

我们可以使用 命令直接运行当前目录下的所有基准测试用例,-bench后面也可以跟正则或者是字符串来匹配对应的用例

$ go test -bench=’Str$’
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ? 6692 ? ? ? ? ? ?181262 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?2.142s
?

对上面的一些关键指标我们要了解一下,首先BenchmarkRandomStr-12后面的代表的是这个跟你机器CPU的逻辑核数有关,在基准测试中可以通过参数指定需要以几核的cpu来运行测试用例

$ go test -bench=’Str$’ -cpu=2,4,8 .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-2 ? ? ? ?6715 ? ? ? ? ? ?181197 ns/op
BenchmarkRandomStr-4 ? ? ? ?6471 ? ? ? ? ? ?180249 ns/op
BenchmarkRandomStr-8 ? ? ? ?6616 ? ? ? ? ? ?179510 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?4.516s
?

和代表用例执行了6715次,每次花费的时间约为0.0001812s,总耗时约为1.2s(ns:s的换算为1000000000:1)

-benchtime=3s 指定时长

-benchtime=100000x 指定次数

-coun=3 指定轮数

$ go test -bench=’Str$’ -benchtime=3s .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ?19988 ? ? ? ? ? ?177572 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?5.384s
?
$ go test -bench=’Str$’ -benchtime=10000x .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ?10000 ? ? ? ? ? ?184832 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?1.870s
?
$ go test -bench=’Str$’ -count=2 .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ? 6702 ? ? ? ? ? ?177048 ns/op
BenchmarkRandomStr-12 ? ? ? ? ? ? ? 6482 ? ? ? ? ? ?177861 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?3.269s
?
?

有时候我们的测试用例会需要一些前置准备的耗时行为,这对我们的测试结果会产生影响,这个时候就需要在耗时操作后重置计时。下面我们用一个伪代码来模拟一下

func BenchmarkRandomStr(b *testing.B) {
time.Sleep(time.Second * 2) // 模拟耗时操作
for i :=0; i < b.N; i++ {
randomStr(10000)
}
}
?

这时候我们再执行一下用例

$ go test -bench=’Str$’ .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ? ? ?1 ? ? ? ?2001588866 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?2.009s
?

发现只执行了一次,时间变成了2s多,这显然不符合我们的预期,这个时候需要调用来重置时间

func BenchmarkRandomStr(b *testing.B) {
time.Sleep(time.Second * 2) // 模拟耗时操作
?b.ResetTimer()
for i :=0; i < b.N; i++ {
randomStr(10000)
}
}

再次执行基准测试

$ go test -bench=’Str$’ .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandomStr-12 ? ? ? ? ? ? ? 6506 ? ? ? ? ? ?183098 ns/op
PASS
ok ? ? learn/learn_test ? ? ? ?10.030s
?

运行次数和单次执行时间已经恢复到之前测试的情况了。基准测试还有和方法也是同样的道理,在影响耗时的操作之前停止计时,完成之后再开始计时。

我们再评估代码的性能时,除了时间的快慢,还有一个重要的指标就是内存使用率,基准测试中可以通过 来显示内存使用情况。下面我们用一组指定cap和不指定cap的返回int切片方法来看一下内存的使用情况

func getIntArr(n int) []int {
rand.Seed(uint64(time.Now().UnixNano()))
arr :=make([]int, 0)
for i :=0; i < n; i++ {
arr=append(arr, rand.Int())
}
?
return arr
}
?
func getIntArrWithCap(n int) []int {
rand.Seed(uint64(time.Now().UnixNano()))
arr :=make([]int, 0, n)
for i :=0; i < n; i++ {
arr=append(arr, rand.Int())
}
?
return arr
}
//——————————————
// 基准测试代码
//——————————————
func BenchmarkGetIntArr(b *testing.B) {
for i :=0; i < b.N; i++ {
getIntArr(100000)
}
}
?
func BenchmarkGetIntArrWithCap(b *testing.B) {
for i :=0; i < b.N; i++ {
getIntArrWithCap(100000)
}
}
?

执行基准测试:

$ go test -bench=’Arr’ -benchmem .
goos: darwin
goarch: amd64
pkg: learn/learn_test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkGetIntArr-12 ? ? ? ? ? ? ? ? ? ? ? ?598 ? ? ? ? ? 1928991 ns/op ? ? ? ? 4101389 B/op ? ? ? ? 28 allocs/op
BenchmarkGetIntArrWithCap-12 ? ? ? ? ? ? ? ? 742 ? ? ? ? ? 1556204 ns/op ? ? ? ? ?802817 B/op ? ? ? ? ?1 allocs/op
PASS
ok ? ? learn/learn_test ? ? ? ?2.688s
?

可以看到指定了cap的方法执行的速度大约快20%,而内存的使用少了80%左右, 代表每次的内存使用情况,表示每次操作分配内存的次数

在写基准测试的时候,最让我搞不懂的是b.N的机制,如何根据不同的用例来自动调整执行的次数,然后我在源码中找到了一些蛛丝马迹。首先,先看一下基准测试的底层数据结构

type B struct {
common
importPath ? ? ? string
context ? ? ? ? ?*benchContext
N ? ? ? ? ? ? ? ?int // 这个就是要搞懂的N,代表要执行的次数
previousN ? ? ? ?int ? ? ? ? ?
previousDuration time.Duration
benchFunc ? ? ? ?func(b *B) // 测试函数
benchTime ? ? ? ?durationOrCountFlag // 执行时间,默认是1s 可以通过-benchtime指定
bytes ? ? ? ? ? ?int64
missingBytes ? ? bool
timerOn ? ? ? ? ?bool
showAllocResult ?bool
result ? ? ? ? ? BenchmarkResult
parallelism ? ? ?int

startAllocs uint64
startBytes ?uint64

netAllocs uint64
netBytes ?uint64

extra map[string]float64
}

通过结构体中的N字段,可以找到几个关键的方法,:每一次执行都会调用的方法,设置N的值。:第一次迭代,根据它的结果决定是否需要运行更多的基准测试。: run1()执行的结果为true的情况会调用,这个方法里调用函数从而调用函数,这个是最终决定执行次数的函数

// Run benchmarks f as a subbenchmark with the given name. It reports
// whether there were any failures.
//
// A subbenchmark is like any other benchmark. A benchmark that calls Run at
// least once will not be measured itself and will be called once with N=1.
func (b *B) Run(name string, f func(b *B)) bool {
// …省略部分代码
?// Run()方法是基准测试的启动方法,会新建一个子测试
sub :=&B{
common: common{
signal: ?make(chan bool),
name: ? ?benchName,
parent: ?&b.common,
level: ? b.level + 1,
creator: pc[:n],
w: ? ? ? b.w,
chatty: ?b.chatty,
bench: ? true,
},
importPath: b.importPath,
benchFunc: ?f,
benchTime: ?b.benchTime,
context: ? ?b.context,
}
// …省略部分代码
?if sub.run1() { // 执行一次子测试,如果不出错执行run()
? ?sub.run() //最终调用 launch()方法,决定需要执行多少次runN()
}
b.add(sub.result)
return !sub.failed
}
?
// runN runs a single benchmark for the specified number of iterations.
func (b *B) runN(n int) {
// ….省略部分代码
b.N=n //指定N
// …
}

// launch launches the benchmark function. It gradually increases the number
// of benchmark iterations until the benchmark runs for the requested benchtime.
// launch is run by the doBench function as a separate goroutine.
// run1 must have been called on b.
func (b *B) launch() {
?// ….省略部分代码
d :=b.benchTime.d
?// 最少执行时间为1s,最多执行次数为1e9次
for n :=int64(1); !b.failed && b.duration < d && n < 1e9; {
last :=n
// 预测所需要的迭代次数
goalns :=d.Nanoseconds()
prevIters :=int64(b.N)
prevns :=b.duration.Nanoseconds()
if prevns <=0 {
//四舍五入,预防除0
prevns=1
}
n=goalns * prevIters / prevns
? ? ?// 避免增长的太快,先按1.2倍增长,最少增加一次
n +=n / 5
n=min(n, 100*last)
n=max(n, last+1)
// 最多执行1e9次
n=min(n, 1e9)
b.runN(int(n))
}
?

1.基准测试方法要以Benchmark开头

2.执行基准测试用go test -bench .命令执行该目录下所有的基准测试,-bench后面可以跟正则表达式,来执行符合条件的测试

3.-cpu参数可以指定运行测试的cpu核心数

4.-benchtime参数可以指定运行测试的时间和次数

5.-count参数可以指定运行测试的轮数

6.b.ResetTimer()、b.StopTimer()、b.StartTimer()可以重置或暂停计时,来消除一些耗时操作的影响

以上就是go通过benchmark对代码进行性能测试详解的详细内容,更多关于go benchmark代码性能测试的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:go benchmark 基准测试详解浅谈c++性能测试工具google benchmark5个可以在Golang中优化代码以提高性能的技巧分享MongoDB创建一个索引而性能提升1000倍示例代码Django代码性能优化与Pycharm Profile使用详解Django查询数据库的性能优化示例代码

© 版权声明

相关文章