• golang基准测试详解


    为什么需要基准测试?

    如果你的代码中有性能问题,或者你怀疑某段代码有性能问题(当然最好得明确排除I/O性能问题),可以用基准测试生成CPU分析报告。

    基准测试前的准备

    生成以_test后缀的go文件(例:xxx_test.go)后,编写基准测试用例,以Benchmark开头的。以测试冒泡排序为例,代码如下:

    func BenchmarkSort(b *testing.B) {
    arr := make([]int, 100000)
    for i:=100000; i > 0; i-- {
    arr = append(arr, i)
    }

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
    bubbleSort(arr)
    }
    }

    func bubbleSort(nums []int) []int {
    length := len(nums)
    for i := 1; i < length; i++ {
    for j := length - 1; j >= i; j-- {
    if nums[j] < nums[j-1] {
    nums[j], nums[j-1] = nums[j-1], nums[j]
    }
    }
    }
    return nums
    }
      

    用例中一些细节的说明

    基准测试函数会被一直调用直到b.N无效,它是基准测试循环的次数
    b.N 从 1 开始,如果基准测试函数在1秒内就完成 (默认值),则 b.N 增加,并再次运行基准测试函数。
    b.N 在近似这样的序列中不断增加;1, 2, 3, 5, 10, 20, 30, 50, 100 等等,

    b.ResetTimer(). 开始执行for b.N循环前需要一些准备时间,可以通过该命令重制基准计时器。
    b.StopTimer()b.StartTimer()。如果在for b.N内还有一些为测试方法准备的前置条件,可以用b.StopTimer()暂停基准计时器,然后再用b.StartTimer()启动计时器。例:
    for i := 0; i < b.N; i++ {
      b.StopTimer()
       someBeforTest()
      b.StartTimer()
       bubbleSort(arr)
    }
    
    

      

    运行基准测试用例

    在测试文件目录下执行 
    go test -bench=^BenchmarkSort$ -run=^$ -benchmem -cpuprofile=cpuprof

    参数说明

    -run           // go test 会在运行基准测试之前之前执行包里所有的单元测试. -run 标识排除这些单元测试,不让它们执行; 比如: go test -run=^$
    -bench regexp     // 匹配要执行的bench方法,以正则表达式来匹配
    -benchtime t     // t时间内,执行最大化的b.N迭代。默认是1秒。 当然至少迭代一次
    -benchmem     // 打印基准测试消耗的内存
    -cpu 1,2,4     // 设置每次测试执行GOMAXPROCS的值
    -count n     // 运行n次测试,默认是1.(注意,有时候执行单元测试发现新的改动没生效,是因为单元测试会用到之前的缓存,设置count=1即可解决
    -cpuprofile=$FILE  // 将cpu分析结果写入 $FILE用于分析

     更多参数说明请见: https://golang.org/cmd/go/#hdr-Testing_flags

    返回说明

    goos: darwin
    goarch: amd64
    pkg: gopool
    BenchmarkSort-4                1        16661414276 ns/op          17792 B/op         10 allocs/op
    PASS
    ok      gopool  16.849s

    说明:
    只看第四行
    BenchmarkSort-4      // 基准测试名-4 后缀和用于运行次测试的 GOMAXPROCS 值有关。

    1             // 1秒中只迭代了一次
    16661414276 ns/op    // 执行一次测试的方法即bubbleSort(arr) 需要这么多纳秒

    17792 B/op   // 每个op操作分配了多少字节(即需要多少)
    10 allocs/op      // 每个op发生多少个不同的内存分配(这块也不知道怎么给出合理解释)

      

     最后我们也可以通过go tool提供的性能分析工具,查看某个方法的具体耗时。如下:

    go tool pprof cpuprof                                          
    Type: cpu
    Time: Mar 16, 2020 at 12:13pm (CST)
    Duration: 16.84s, Total samples = 14.19s (84.26%)
    Entering interactive mode (type "help" for commands, "o" for options)
    (pprof) web
    

    cpuprof就是上面-cpuprofile=cpuprof生成的cpu性能分析文件

  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/xiaoxlm/p/12500900.html
Copyright © 2020-2023  润新知