简体   繁体   English

Golang:如何在Web服务器中使用pprof获得计算时间

[英]Golang: How to get computing time using pprof within a web server

I have built a web server and did an ab(apache benchmark) test. 我已经建立了一个Web服务器并进行了AB(Apache基准)测试。 Now I want to know the computing time of each part. 现在我想知道每个部分的计算时间。

I used the go tool pprof url:port/xxx and get the profile of this program, but it does not tell me the computing time(only has memory). 我使用了go tool pprof url:port/xxx并获取了该程序的配置文件,但它没有告诉我计算时间(仅具有内存)。 Following is the results: 以下是结果:

(pprof) top10
1827.59MB of 1978.12MB total (92.39%)
Dropped 175 nodes (cum <= 9.89MB)
Showing top 10 nodes out of 48 (cum >= 43.50MB)
  flat  flat%   sum%        cum   cum%
  769.54MB 38.90% 38.90%   769.54MB 38.90%  reflect.unsafe_New
  459.08MB 23.21% 62.11%   459.08MB 23.21%  services/semanticnew.TopicProbCounter.Update
  189.17MB  9.56% 71.67%  1081.21MB 54.66% github.com/golang/protobuf/proto.(*Buffer).dec_slice_struct
  122MB  6.17% 77.84%      122MB  6.17%  github.com/golang/protobuf/proto.(*Buffer).dec_int32
  107.56MB  5.44% 83.28%   107.56MB  5.44% github.com/garyburd/redigo/redis.(*conn).readReply
  68.13MB  3.44% 86.72%   527.21MB 26.65%  services/semanticnew.caculApPoiScore

  40.51MB  2.05% 88.77%    40.51MB  2.05%  runtime.malg
  28.59MB  1.45% 90.22%    28.59MB  1.45%  net/http.newBufioWriterSize
  22.50MB  1.14% 91.35%       23MB  1.16%  redismanage.(*Manager).getRequest
  20.50MB  1.04% 92.39%    43.50MB  2.20%  redismanage.(*Manager).GetRequest

In my web program, I have added the following code: 在我的网络程序中,我添加了以下代码:

f, _ := os.Create("x.cpuprofile")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

but it still does not work. 但它仍然不起作用。

I have checked http://blog.golang.org/profiling-go-programs , but the xxx.prof file confused me. 我已经检查了http://blog.golang.org/profiling-go-programs ,但是xxx.prof文件使我感到困惑。 How can I generate this xxx.prof file? 如何生成此xxx.prof文件? And the author used go tool pprof xxx xxx.prof , does it mean that xxx is the binary file which generated by xxx.go ? 作者使用go tool pprof xxx xxx.prof ,这是否意味着xxxxxx.go生成的二进制文件?

Anyway, the target is getting the computing time, but how? 无论如何,目标是获取计算时间,但是如何? Do I have to generate this xxx.prof file to achieve this goal? 我是否必须生成此xxx.prof文件才能实现此目标?

Many thanks 非常感谢

I've had success using Dmitry's advice from here: https://software.intel.com/en-us/blogs/2014/05/10/debugging-performance-issues-in-go-programs 我已经从这里获得了使用Dmitry的建议的成功: https : //software.intel.com/zh-cn/blogs/2014/05/10/debugging-performance-issues-in-go-programs

You merely need to import net/http/pprof, and collect profiles with: 您只需要导入net / http / pprof,并使用以下命令收集配置文件:

$ go tool pprof --text mybin http://myserver:6060:/debug/pprof/profile $ go工具pprof --text mybin http:// myserver:6060:/ debug / pprof / profile

The net/http/pprof is imported solely for its side effects, so you can use net / http / pprof的导入仅出于副作用,因此您可以使用

import (
   ...
   _ "net/http/pprof"
)

Aside from "mybin" and "myserver" you also need to substitute your port number in and remove the trailing colons after the port number. 除了“ mybin”和“ myserver”,您还需要替换端口号,并删除端口号后的冒号。 Eg for my pet project, the command is 例如对于我的宠物项目,命令是

go tool pprof http://127.0.0.1:8000/debug/pprof/profile

You'll then get a pprof archive created which you can explore interactively through pprof, or you can use an external tool - personally I prefer the built-in capabilities. 然后,您将创建一个pprof存档,可以通过pprof进行交互浏览,也可以使用外部工具-我个人更喜欢内置功能。

Note that https://github.com/golang/go/issues/13841 and https://github.com/golang/go/issues/6047 are existing problems with the combination of pprof and some kernels. 请注意,pprof和某些内核的组合存在https://github.com/golang/go/issues/13841https://github.com/golang/go/issues/6047的现有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM