github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zpprof/pprof.go (about)

     1  // Package zpprof provides a register for zweb framework to use net/http/pprof easily.
     2  package zpprof
     3  
     4  import (
     5  	"net/http"
     6  
     7  	"github.com/sohaha/zlsgo/znet"
     8  )
     9  
    10  // Register Registration routing
    11  func Register(r *znet.Engine, token string) (RouterGroup *znet.Engine) {
    12  
    13  	// go tool pprof http://127.0.0.1:8081/debug/pprof/profile
    14  	// go tool pprof -alloc_space http://127.0.0.1:8081/debug/pprof/heap
    15  	// go tool pprof -inuse_space http://127.0.0.1:8081/debug/pprof/heap
    16  
    17  	RouterGroup = r.Group("/debug", func(g *znet.Engine) {
    18  		g.Use(authDebug(token), func(c *znet.Context) {
    19  			c.Next()
    20  			c.Abort(200)
    21  		})
    22  		g.GET("", infoHandler)
    23  		g.GET("/", redirectPprof)
    24  		g.GET("/pprof", redirectPprof)
    25  		g.GET("/pprof/", indexHandler)
    26  		g.GET("/pprof/allocs", allocsHandler)
    27  		g.GET("/pprof/mutex", mutexHandler)
    28  		g.GET("/pprof/heap", heapHandler)
    29  		g.GET("/pprof/goroutine", goroutineHandler)
    30  		g.GET("/pprof/block", blockHandler)
    31  		g.GET("/pprof/threadcreate", threadCreateHandler)
    32  		g.GET("/pprof/cmdline", cmdlineHandler)
    33  		g.GET("/pprof/profile", profileHandler)
    34  		g.GET("/pprof/symbol", symbolHandler)
    35  		g.POST("/pprof/symbol", symbolHandler)
    36  		g.GET("/pprof/trace", traceHandler)
    37  	})
    38  	return
    39  }
    40  
    41  func ListenAndServe(addr ...string) error {
    42  	a := "localhost:8082"
    43  	if len(addr) > 0 {
    44  		a = addr[0]
    45  	}
    46  	return http.ListenAndServe(a, nil)
    47  }