欢迎访问WDPHP素材源码!今天是2024年04月29日 星期一,时间不早了,早点休息吧!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

go怎么通过benchmark对代码进行性能测试
栏目分类:其他教程    发布日期:2023-12-01    浏览次数:510次     收藏

本篇内容介绍了“go怎么通过benchmark对代码进行性能测试”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

benchmark的使用

在开发中我们要想编写高性能的代码,或者优化代码的性能时,你首先得知道当前代码的性能,在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)
}

要对上面的代码做基准测试,首先我们要新建一个测试文件,比如

main_test.go
,然后新建一个基准测试方法
BenchmarkRandomStr
,与普通的测试函数Test 开头,参数为t *testing.T类似,基准测试函数要以Benchmark开头,参数为b *testing.B,代码中的
b.N
代表的是该用例的运行次数,这个值是会变的,对于每个用例都不一样,这个值会从1开始增加,具体的实现我会在下面的实现原理里进行介绍。
func BenchmarkRandomStr(b *testing.B) {
  for i := 0; i < b.N; i++ {
    randomStr(10000)
  }
}

运行Benchmark

我们可以使用

go test -bench .
命令直接运行当前目录下的所有基准测试用例,-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后面的

-12
代表的是
GOMAXPROCS
这个跟你机器CPU的逻辑核数有关,在基准测试中可以通过
-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
181197 ns/op
代表用例执行了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多,这显然不符合我们的预期,这个时候需要调用

b.ResetTime()
来重置时间
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

运行次数和单次执行时间已经恢复到之前测试的情况了。基准测试还有

b.StopTimer()
b.StartTimer()
方法也是同样的道理,在影响耗时的操作之前停止计时,完成之后再开始计时。

查看内存使用情况

我们再评估代码的性能时,除了时间的快慢,还有一个重要的指标就是内存使用率,基准测试中可以通过

-benchmem
来显示内存使用情况。下面我们用一组指定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%左右,

802817 B/op
代表每次的内存使用情况,
1 allocs/op
表示每次操作分配内存的次数

testing.B的底层实现

在写基准测试的时候,最让我搞不懂的是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字段,可以找到几个关键的方法,

runN()
:每一次执行都会调用的方法,设置N的值。
run1()
:第一次迭代,根据它的结果决定是否需要运行更多的基准测试。
run()
: run1()执行的结果为true的情况会调用,这个方法里调用
doBench()
函数从而调用
launch()
函数,这个是最终决定执行次数的函数
// 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))
}
源码 模板 特效 素材 资源 教程 站长