性能/内存优化 + 工具
原创
©著作权归作者所有:来自51CTO博客作者xiaoxiaoyu8510的原创作品,请联系作者获取转载授权,否则将追究法律责任
net/http/pprof
import (
"fmt"
_ "net/http/pprof"
"net/http"
)
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
然后访问http://localhost:6060/debug/pprof/ 就可以看到性能
2.可以自定义调试页面
import (
"net/http"
"net/http/pprof"
"fmt"
"bytes"
log "github.com/thinkboy/log4go"
)
func SocketHelp(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
fmt.Fprintf(&buf, "num_symbols: 1\n")
w.Write(buf.Bytes())
}
// StartPprof start http pprof.
func Init(pprofBind []string) {
pprofServeMux := http.NewServeMux()
pprofServeMux.HandleFunc("/debug/pprof/", pprof.Index)
pprofServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
pprofServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pprofServeMux.HandleFunc("/debug/pprof/socket", SocketHelp)
for _, addr := range pprofBind {
go func() {
if err := http.ListenAndServe(addr, pprofServeMux); err != nil {
log.Error("http.ListenAndServe(\"%s\", pprofServeMux) error(%v)", addr, err)
panic(err)
}
}()
}
}