gpt4 book ai didi

go - 如何在 go 中获取功能持续时间分割(分析)

转载 作者:IT王子 更新时间:2023-10-29 00:59:33 27 4
gpt4 key购买 nike

更新(2019 年 1 月 24 日):

这个问题是 4 年前关于 Go 1.4 被问到的(并且仍在征求意见)。从那时起,使用 pprof 进行分析发生了巨大的变化。

原始问题:

我正在尝试分析我编写的基于 go martini 的服务器,我想分析单个请求,并获得函数及其运行时长的完整分解。我尝试同时使用 runtime/pprofnet/http/pprof 但输出如下所示:

Total: 3 samples
1 33.3% 33.3% 1 33.3% ExternalCode
1 33.3% 66.7% 1 33.3% runtime.futex
1 33.3% 100.0% 2 66.7% syscall.Syscall

Web View 也不是很有帮助。

我们定期分析另一个程序,输出似乎是我需要的:

20ms of 20ms total (  100%)
flat flat% sum% cum cum%
10ms 50.00% 50.00% 10ms 50.00% runtime.duffcopy
10ms 50.00% 100% 10ms 50.00% runtime.fastrand1
0 0% 100% 20ms 100% main.func·004
0 0% 100% 20ms 100% main.pruneAlerts
0 0% 100% 20ms 100% runtime.memclr

我不知道差异来自哪里。

最佳答案

pprof 是一个基于定时器的采样分析器,最初来自 gperftools套房。 Rus Cox 后来将 pprof 工具移植到 Go:http://research.swtch.com/pprof .

这个基于计时器的分析器通过使用系统分析计时器工作,并在收到 SIGPROF 时记录统计信息。在 go 中,当前设置为恒定的 100Hz。来自 pprof.go:

// The runtime routines allow a variable profiling rate,
// but in practice operating systems cannot trigger signals
// at more than about 500 Hz, and our processing of the
// signal is not cheap (mostly getting the stack trace).
// 100 Hz is a reasonable choice: it is frequent enough to
// produce useful data, rare enough not to bog down the
// system, and a nice round number to make it easy to
// convert sample counts to seconds. Instead of requiring
// each client to specify the frequency, we hard code it.
const hz = 100

您可以通过调用 runtime.SetCPUProfileRate 来设置此频率并自己编写配置文件输出,Gperftools 允许您使用 CPUPROFILE_FREQUENCY 设置此频率,但实际上它不是那么有用。

为了对程序进行采样,它需要始终执行您要测量的操作。对空闲运行时进行采样并没有显示出任何有用的信息。你通常做的是在基准测试或热循环中运行你想要的代码,使用尽可能多的 CPU 时间。在积累了足够多的样本后,所有函数中应该有足够的数量来按比例显示每个函数花费了多少时间。

另见:

关于go - 如何在 go 中获取功能持续时间分割(分析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com