2013-10-25 25 views
8

我有一個相對較大的使用Gorilla's mux進行路由的Go應用程序。我最近意識到我的Web應用程序非常慢,我想分析Web應用程序。分析使用Gorilla的多路複用器與網/ http/pprof構建的Go Web應用程序

看完後,它接縫net/http/pprof是我所需要的。但我不能讓它與mux;即使在最瑣碎的Web應用程序的情況下。

有誰知道如何使這項工作?

下面是一個不起作用的簡單代碼的例子(即沒有提供任何東西)。

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

回答

11

對不起這個問題。答案是在pprof的init()函數中。一個只需要將4個功能從pprof添加到多路複用路由器。這是來自上面的固定代碼。

package main 

import (
    "fmt" 
    "github.com/gorilla/mux" 
    "math" 

    "net/http" 
) 
import "net/http/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) 
} 

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() 
    AttachProfiler(r) 
    r.HandleFunc("/hello", SayHello) 
    http.ListenAndServe(":6060", r) 
} 
15

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

... 

如果任何人有同樣的問題,我希望這幫助!

9

我給這家首選的方法是隻讓net/http/pprof註冊本身http.DefaultServeMux,然後通過與/debug/pprof/開始沿所有請求:

package main 

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

    "github.com/gorilla/mux" 
) 

func main() { 
    router := mux.NewRouter() 
    router.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux) 
    if err := http.ListenAndServe(":6060", router); err != nil { 
     panic(err) 
    } 
} 

我發現,這種方法是很多比一個依賴更穩定對隱藏的初始化方法的執行,也保證你不會錯過任何東西。

1

我沒有別的東西,我加了一個不同的端口上的另一本地HTTP服務器,它只是開箱的

package main 

import (
    "fmt" 
    "log" 
    "net/http" 

    _ "net/http/pprof" 
) 


func main() { 
    go func() { 
     log.Println(http.ListenAndServe(":6060", nil)) 
    }() 
    log.Fatalln(http.ListenAndServe(":8080", route.Handlers())) 
} 

現在pprof終點是: http://localhost:6060/debug/pprof/和的applcation在端口上運行:8080

0

就是這麼回事,

r := mux.NewRouter() 
r.PathPrefix("/debug").Handler(http.DefaultServeMux) 
0

進出口使用https://github.com/julienschmidt/httprouter但我剛剛從去這個答案ogle搜索。 這就是我所做的

router := httprouter.New() 
router.Handler("GET", "/debug/pprof/profile", http.DefaultServeMux) 
router.Handler("GET", "/debug/pprof/heap", http.DefaultServeMux) 

我只需要這兩條路線。 這個答案是@damien和@ user983716答案的結合。

相關問題