go - 如何使用 `go test` 对 golang 中程序的 *section* 进行计时和分析?

标签 go profiling

问题:如何使用 go test 对 golang 中程序的部分进行计时和分析?

用例:我有一个B+tree的并发批量操作处理算法。我正在使用 go test 进行分析并与其他基线算法(序列化版本、悲观锁定等)进行比较。对于测试用例设置,我将创建一个包含 1M 条目的 B+ 树并创建一个 1M 操作列表,然后我开始实际测试以BulkProcess这些操作。

func TestInputTreeM1e6N1e6(*testing.T) {
    M := 1000000

    //Test Preparation 1: Setup the tree
    tree := NewTree(cmp)
    file1name := "InitalTree_10000000.txt"

    testF1, err := os.Open(file1name)
    if err != nil {
        panic(err)
    }
    defer testF1.Close()

    nums, _ := ReadInts(testF1)
    for i, num := range nums {
        if i == M {
            break
        }
        tree.Set(Int(num), Int(1))
    }


    fmt.Println("Tree initialized")

    //Test Prepration 2: Initialize operations
    N := 1000000 
    inputs := make([]SearchPair, N)

    file2name := "Operations_10000000.txt"
    testF2, err := os.Open(file2name)
    if err != nil {
        panic(err)
    }
    defer testF2.Close()

    var opt string
    var num int
    var input SearchPair
    for i:=0; i < N; i++{

        fmt.Fscanf(testF2, "%s %d", &opt, &num)

        switch opt {
        case "DEL":
            input = SearchPair{
                opt: DEL,   //Operation to be applied
                k: Int(num),    //k to be operated upon
                v: Int(0),
            }
        case "PUT":
            input = SearchPair{
                opt: PUT,   //Operation to be applied
                k: Int(num),    //k to be operated upon
                v: Int(-1),
            }
        case "SET":
            input = SearchPair{
                opt: SET,   //Operation to be applied
                k: Int(num),    //k to be operated upon
                v: Int(2),
            }
        }

        inputs[i] = input
    }

    //Start actual testing
    fmt.Println("Start processing...")
    tree.BulkProcess(inputs)
    fmt.Println("Done")

}

问题:我正在使用 go test -v -cpuprofile cpu.out -memprofile mem.out 来分析 BulkProcess 性能,然后使用 go tool pprof --pdf ConcurrentBPlusTree.test mem.out > mgraph.pdfgo tool pprof --pdf ConcurrentBPlusTree.test cpu.out > cgraph.pdf 进行可视化结果。

现在,go test 似乎正在分析整个测试用例,包括树构建和操作构建。测试设置期间的文件 I/O 一直主导着性能,以至于我什至无法看到实际测试中发生了什么。

还有,go test 有这样的输出行

=== RUN   TestInputTreeM1e6N1e6
--- PASS: TestInputTreeM1e6N1e6 (63.36s)

是否可以配置计时器,使其只显示批量处理所花费的时间而不是整个案例?我知道 b *testing.Bb.ResetTimer()b.StopTimer() 等 API。 *testing.T 是否有类似的 API,以便我可以修改计时行为?

具体问题

  1. 如何使用 go test分析当前测试用例的最后三行?
  2. 如何使用 go test计时当前测试用例的最后三行?

最佳答案

如果您不想分析整个程序,您可以使用 runtime/pprof 自行启动和停止 CPUProfile包裹:

f, err := os.Create(profileFileName)
if err != nil {
    log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
    log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()

关于go - 如何使用 `go test` 对 golang 中程序的 *section* 进行计时和分析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40139983/

相关文章:

windows - 如何测量 Windows 上的内存带宽利用率?

PHP:在运行时更改 xdebug.profiler_output_name 选项

go - 如何将函数内部创建的变量作为指向另一个函数的指针传递

json - 从 json post 请求中转义 html

amazon-web-services - 在没有 session 的情况下在 Go AWS SDK 中创建客户端

php - PHP 的 'profiler with visualization' 和 `full backtrace visualization` ?

go - 如何从 [][]byte 转换为 **char

不能用宏包装 cgo 标志

node.js - nodejs内存分析

c - 如何分析/识别紧密处理循环中的缓慢步骤?