猿问

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

我有一个用 Go 编写的相对较大的 Web 应用程序,它使用Gorilla 的 mux进行路由。我最近意识到我的 Web 应用程序很慢,我想分析 Web 应用程序。

阅读后,似乎net/http/pprof是我需要的。但我不能让它与mux 一起运行;即使是最微不足道的 Web 应用程序。

有谁知道如何使它工作?

这是一个不起作用的普通代码示例(即在 处不提供任何内容/debug)。

package main


import (

    "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)

}


喵喵时光机
浏览 357回答 3
3回答

FFIVE

user983716 - 感谢您的问题和解决方案!我无法使用网络索引 ( http://[my-server]/debug/pprof ) 中的链接,直到我在您的解决方案中添加了几行,如下所示:...func AttachProfiler(router *mux.Router) {&nbsp; &nbsp; router.HandleFunc("/debug/pprof/", pprof.Index)&nbsp; &nbsp; router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)&nbsp; &nbsp; router.HandleFunc("/debug/pprof/profile", pprof.Profile)&nbsp; &nbsp; router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)&nbsp; &nbsp; // Manually add support for paths linked to by index page at /debug/pprof/&nbsp; &nbsp; router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))&nbsp; &nbsp; router.Handle("/debug/pprof/heap", pprof.Handler("heap"))&nbsp; &nbsp; router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))&nbsp; &nbsp; router.Handle("/debug/pprof/block", pprof.Handler("block"))}...如果有人遇到同样的问题,我希望这会有所帮助!
随时随地看视频慕课网APP

相关分类

Go
我要回答