亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 net/http/pprof 分析使用 Gorilla 的 mux 構建的 Go

使用 net/http/pprof 分析使用 Gorilla 的 mux 構建的 Go

Go
喵喵時光機 2021-06-29 16:12:20
我有一個用 Go 編寫的相對較大的 Web 應用程序,它使用Gorilla 的 mux進行路由。我最近意識到我的 Web 應用程序很慢,我想分析 Web 應用程序。閱讀后,似乎net/http/pprof是我需要的。但我不能讓它與mux 一起運行;即使是最微不足道的 Web 應用程序。有誰知道如何使它工作?這是一個不起作用的普通代碼示例(即在 處不提供任何內容/debug)。package mainimport (    "fmt"    "github.com/gorilla/mux"    "math"    "net/http")import _ "net/http/pprof"func SayHello(w http.ResponseWriter, r *http.Request) {    for i := 0; i < 1000000; i++ {        math.Pow(36, 89)    }    fmt.Fprint(w, "Hello!")}func main() {    r := mux.NewRouter()    r.HandleFunc("/hello", SayHello)    http.ListenAndServe(":6060", r)}
查看完整描述

3 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

user983716 - 感謝您的問題和解決方案!


我無法使用網絡索引 ( http://[my-server]/debug/pprof ) 中的鏈接,直到我在您的解決方案中添加了幾行,如下所示:


...


func AttachProfiler(router *mux.Router) {

    router.HandleFunc("/debug/pprof/", pprof.Index)

    router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)

    router.HandleFunc("/debug/pprof/profile", pprof.Profile)

    router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)


    // Manually add support for paths linked to by index page at /debug/pprof/

    router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))

    router.Handle("/debug/pprof/heap", pprof.Handler("heap"))

    router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))

    router.Handle("/debug/pprof/block", pprof.Handler("block"))

}


...

如果有人遇到同樣的問題,我希望這會有所幫助!


查看完整回答
反對 回復 2021-07-05
  • 3 回答
  • 0 關注
  • 394 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號