2013-10-25 13 views
8

Goには、ルーティング用にGorilla's muxを使用する比較的大きなWebアプリケーションがあります。私は最近、私のWebアプリケーションがかなり遅いことを知り、Webアプリケーションをプロファイルしたいと思います。プロファイリングnet/http/pprofでGorillaのマルチプレクサで構築された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()関数にあります。 pprofからmuxルータに4つの機能を追加するだけです。ここから上の固定コードです。

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/pprofhttp.DefaultServeMuxに自身を登録し、その後に沿って/debug/pprof/で始まるすべての要求を渡すようにすることです:私はこのアプローチは異なり1よりも多く安定していることがわかり

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サーバを追加し、それがちょうど今pprofエンドポイントがにあるボックス

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

の外に動作します: http://localhost:6060/debug/pprof/とapplcationはポートで実行されています:8080

0

イムしかし、私はちょうど、外出先からこの答えを得ました色目検索。 これは私が行ったことです

私はこの2つのルートが必要です。 この回答は、@ damienと@ user983716の回答を組み合わせたものです。

関連する問題