github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zpprof/handler.go (about) 1 package zpprof 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/pprof" 7 "time" 8 9 "github.com/sohaha/zlsgo/znet" 10 ) 11 12 var startTime = time.Now() 13 14 func infoHandler(c *znet.Context) { 15 m := NewSystemInfo(startTime) 16 info := fmt.Sprintf("%s:%s\n", "服务器", m.ServerName) 17 info += fmt.Sprintf("%s:%s\n", "运行时间", m.Runtime) 18 info += fmt.Sprintf("%s:%s\n", "goroutine数量", m.GoroutineNum) 19 info += fmt.Sprintf("%s:%s\n", "CPU核数", m.CPUNum) 20 info += fmt.Sprintf("%s:%s\n", "当前内存使用量", m.UsedMem) 21 info += fmt.Sprintf("%s:%s\n", "当前堆内存使用量", m.HeapInuse) 22 info += fmt.Sprintf("%s:%s\n", "总分配的内存", m.TotalMem) 23 info += fmt.Sprintf("%s:%s\n", "系统内存占用量", m.SysMem) 24 info += fmt.Sprintf("%s:%s\n", "指针查找次数", m.Lookups) 25 info += fmt.Sprintf("%s:%s\n", "内存分配次数", m.Mallocs) 26 info += fmt.Sprintf("%s:%s\n", "内存释放次数", m.Frees) 27 info += fmt.Sprintf("%s:%s\n", "距离上次GC时间", m.LastGCTime) 28 info += fmt.Sprintf("%s:%s\n", "下次GC内存回收量", m.NextGC) 29 info += fmt.Sprintf("%s:%s\n", "GC暂停时间总量", m.PauseTotalNs) 30 info += fmt.Sprintf("%s:%s\n", "上次GC暂停时间", m.PauseNs) 31 _, _ = fmt.Fprint(c.Writer, info) 32 } 33 34 func indexHandler(c *znet.Context) { 35 pprof.Index(c.Writer, c.Request) 36 } 37 38 func allocsHandler(c *znet.Context) { 39 pprof.Handler("allocs").ServeHTTP(c.Writer, c.Request) 40 } 41 42 func mutexHandler(c *znet.Context) { 43 pprof.Handler("mutex").ServeHTTP(c.Writer, c.Request) 44 } 45 46 func heapHandler(c *znet.Context) { 47 pprof.Handler("heap").ServeHTTP(c.Writer, c.Request) 48 } 49 50 func goroutineHandler(c *znet.Context) { 51 pprof.Handler("goroutine").ServeHTTP(c.Writer, c.Request) 52 } 53 54 func blockHandler(c *znet.Context) { 55 pprof.Handler("block").ServeHTTP(c.Writer, c.Request) 56 } 57 58 func threadCreateHandler(c *znet.Context) { 59 pprof.Handler("threadcreate").ServeHTTP(c.Writer, c.Request) 60 } 61 62 func cmdlineHandler(c *znet.Context) { 63 pprof.Cmdline(c.Writer, c.Request) 64 } 65 66 func profileHandler(c *znet.Context) { 67 pprof.Profile(c.Writer, c.Request) 68 } 69 70 func symbolHandler(c *znet.Context) { 71 pprof.Symbol(c.Writer, c.Request) 72 } 73 74 func traceHandler(c *znet.Context) { 75 pprof.Trace(c.Writer, c.Request) 76 } 77 78 func redirectPprof(c *znet.Context) { 79 http.Redirect(c.Writer, c.Request, "/debug/pprof/", http.StatusFound) 80 } 81 82 func authDebug(token string) znet.HandlerFunc { 83 return func(c *znet.Context) { 84 if token != "" { 85 getToken := c.DefaultQuery("token", c.GetCookie("debug-token")) 86 c.SetCookie("debug-token", token, 600) 87 if getToken != token { 88 c.Byte(401, []byte("No permission")) 89 c.Abort() 90 } 91 } 92 c.Next() 93 } 94 }