首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用pprof工具分析基准测试?

如何使用pprof工具分析基准测试?
EN

Stack Overflow用户
提问于 2014-04-13 21:05:53
回答 3查看 10.9K关注 0票数 14

我想要 profile --我的基准测试是由go test -c生成的,但是go tool pprof需要一个通常在主函数(如 )内生成的配置文件。

代码语言:javascript
复制
func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }

如何在基准测试中创建概要文件?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-13 21:14:19

标志中所述,您可以使用标志-cpuprofile指定配置文件。

例如

代码语言:javascript
复制
go test -cpuprofile cpu.out
票数 18
EN

Stack Overflow用户

发布于 2014-04-13 21:13:39

使用-cpuprofile标志到go test,如标志中所记录的

票数 3
EN

Stack Overflow用户

发布于 2018-03-27 09:54:52

这篇文章解释了如何用一个例子来分析基准测试:用pprof进行基准分析

下面的基准测试模拟了一些CPU工作。

代码语言:javascript
复制
package main

import (
    "math/rand"
    "testing"
)

func BenchmarkRand(b *testing.B) {
    for n := 0; n < b.N; n++ {
        rand.Int63()
    }
}

若要为基准测试生成CPU配置文件,请运行:

代码语言:javascript
复制
go test -bench=BenchmarkRand -benchmem -cpuprofile profile.out

-memprofile-blockprofile标志可用于生成内存分配和阻塞调用配置文件。

要分析配置文件,请使用Go工具:

代码语言:javascript
复制
go tool pprof profile.out
(pprof) top
Showing nodes accounting for 1.16s, 100% of 1.16s total
Showing top 10 nodes out of 22
      flat  flat%   sum%        cum   cum%
     0.41s 35.34% 35.34%      0.41s 35.34%  sync.(*Mutex).Unlock
     0.37s 31.90% 67.24%      0.37s 31.90%  sync.(*Mutex).Lock
     0.12s 10.34% 77.59%      1.03s 88.79%  math/rand.(*lockedSource).Int63
     0.08s  6.90% 84.48%      0.08s  6.90%  math/rand.(*rngSource).Uint64 (inline)
     0.06s  5.17% 89.66%      1.11s 95.69%  math/rand.Int63
     0.05s  4.31% 93.97%      0.13s 11.21%  math/rand.(*rngSource).Int63
     0.04s  3.45% 97.41%      1.15s 99.14%  benchtest.BenchmarkRand
     0.02s  1.72% 99.14%      1.05s 90.52%  math/rand.(*Rand).Int63
     0.01s  0.86%   100%      0.01s  0.86%  runtime.futex
         0     0%   100%      0.01s  0.86%  runtime.allocm

在这种情况下,瓶颈是互斥,这是由同步的数学/兰德中的默认源造成的。

其他配置文件演示和输出格式也是可能的,例如tree。键入help以获得更多选项。

注意,基准测试循环之前的任何初始化代码也将被分析。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23048455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档