// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package ghttp import ( netpprof "net/http/pprof" runpprof "runtime/pprof" "strings" "github.com/gogf/gf/os/gview" ) // utilPProf is the PProf interface implementer. type utilPProf struct{} const ( defaultPProfServerName = "pprof-server" defaultPProfPattern = "/debug/pprof" ) // StartPProfServer starts and runs a new server for pprof. func StartPProfServer(port int, pattern ...string) { s := GetServer(defaultPProfServerName) s.EnablePProf() s.SetPort(port) s.Run() } // EnablePProf enables PProf feature for server. func (s *Server) EnablePProf(pattern ...string) { s.Domain(defaultDomainName).EnablePProf(pattern...) } // EnablePProf enables PProf feature for server of specified domain. func (d *Domain) EnablePProf(pattern ...string) { p := defaultPProfPattern if len(pattern) > 0 && pattern[0] != "" { p = pattern[0] } up := &utilPProf{} _, _, uri, _ := d.server.parsePattern(p) uri = strings.TrimRight(uri, "/") d.Group(uri, func(group *RouterGroup) { group.ALL("/*action", up.Index) group.ALL("/cmdline", up.Cmdline) group.ALL("/profile", up.Profile) group.ALL("/symbol", up.Symbol) group.ALL("/trace", up.Trace) }) } // Index shows the PProf index page. func (p *utilPProf) Index(r *Request) { profiles := runpprof.Profiles() action := r.GetString("action") data := map[string]interface{}{ "uri": strings.TrimRight(r.URL.Path, "/") + "/", "profiles": profiles, } if len(action) == 0 { buffer, _ := gview.ParseContent(r.Context(), `
{{.Count}} | {{.Name}} |