perf.go 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package blademaster
  2. import (
  3. "flag"
  4. "net/http"
  5. "net/http/pprof"
  6. "os"
  7. "sync"
  8. "go-common/library/conf/dsn"
  9. "github.com/pkg/errors"
  10. )
  11. var (
  12. _perfOnce sync.Once
  13. _perfDSN string
  14. )
  15. func init() {
  16. v := os.Getenv("HTTP_PERF")
  17. if v == "" {
  18. v = "tcp://0.0.0.0:2333"
  19. }
  20. flag.StringVar(&_perfDSN, "http.perf", v, "listen http perf dsn, or use HTTP_PERF env variable.")
  21. }
  22. func startPerf() {
  23. _perfOnce.Do(func() {
  24. mux := http.NewServeMux()
  25. mux.HandleFunc("/debug/pprof/", pprof.Index)
  26. mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  27. mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
  28. mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  29. go func() {
  30. d, err := dsn.Parse(_perfDSN)
  31. if err != nil {
  32. panic(errors.Errorf("blademaster: http perf dsn must be tcp://$host:port, %s:error(%v)", _perfDSN, err))
  33. }
  34. if err := http.ListenAndServe(d.Host, mux); err != nil {
  35. panic(errors.Errorf("blademaster: listen %s: error(%v)", d.Host, err))
  36. }
  37. }()
  38. })
  39. }