簡體   English   中英

Golang:如何在Web服務器中使用pprof獲得計算時間

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

我已經建立了一個Web服務器並進行了AB(Apache基准)測試。 現在我想知道每個部分的計算時間。

我使用了go tool pprof url:port/xxx並獲取了該程序的配置文件,但它沒有告訴我計算時間(僅具有內存)。 以下是結果:

(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

在我的網絡程序中,我添加了以下代碼:

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

但它仍然不起作用。

我已經檢查了http://blog.golang.org/profiling-go-programs ,但是xxx.prof文件使我感到困惑。 如何生成此xxx.prof文件? 作者使用go tool pprof xxx xxx.prof ,這是否意味着xxxxxx.go生成的二進制文件?

無論如何,目標是獲取計算時間,但是如何? 我是否必須生成此xxx.prof文件才能實現此目標?

非常感謝

我已經從這里獲得了使用Dmitry的建議的成功: https : //software.intel.com/zh-cn/blogs/2014/05/10/debugging-performance-issues-in-go-programs

您只需要導入net / http / pprof,並使用以下命令收集配置文件:

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

net / http / pprof的導入僅出於副作用,因此您可以使用

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

除了“ mybin”和“ myserver”,您還需要替換端口號,並刪除端口號后的冒號。 例如對於我的寵物項目,命令是

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

然后,您將創建一個pprof存檔,可以通過pprof進行交互瀏覽,也可以使用外部工具-我個人更喜歡內置功能。

請注意,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