mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
refactor(net/ghttp): enhance ghttp.StartPProfServer
(#3555)
This commit is contained in:
parent
9589384b36
commit
55b92151f4
@ -388,7 +388,12 @@ func (s *Server) SetConfig(c ServerConfig) error {
|
||||
}
|
||||
|
||||
// SetAddr sets the listening address for the server.
|
||||
// The address is like ':80', '0.0.0.0:80', '127.0.0.1:80', '180.18.99.10:80', etc.
|
||||
// The address is like:
|
||||
// SetAddr(":80")
|
||||
// SetAddr("0.0.0.0:80")
|
||||
// SetAddr("127.0.0.1:80")
|
||||
// SetAddr("180.18.99.10:80")
|
||||
// etc.
|
||||
func (s *Server) SetAddr(address string) {
|
||||
s.config.Address = address
|
||||
}
|
||||
|
@ -23,12 +23,13 @@ const (
|
||||
defaultPProfPattern = "/debug/pprof"
|
||||
)
|
||||
|
||||
// StartPProfServer starts and runs a new server for pprof.
|
||||
func StartPProfServer(port int, pattern ...string) {
|
||||
s := GetServer(defaultPProfServerName)
|
||||
// StartPProfServer starts and runs a new server for pprof in another goroutine.
|
||||
func StartPProfServer(address string, pattern ...string) (s *Server, err error) {
|
||||
s = GetServer(defaultPProfServerName)
|
||||
s.EnablePProf(pattern...)
|
||||
s.SetPort(port)
|
||||
s.Run()
|
||||
s.SetAddr(address)
|
||||
err = s.Start()
|
||||
return
|
||||
}
|
||||
|
||||
// EnablePProf enables PProf feature for server.
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
. "github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
)
|
||||
@ -29,30 +30,37 @@ func TestServer_EnablePProf(t *testing.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
r, err := client.Get(ctx, "/pprof/index")
|
||||
Assert(err, nil)
|
||||
Assert(r.StatusCode, 200)
|
||||
r.Close()
|
||||
|
||||
r, err = client.Get(ctx, "/pprof/cmdline")
|
||||
Assert(err, nil)
|
||||
Assert(r.StatusCode, 200)
|
||||
r.Close()
|
||||
|
||||
//r, err = client.Get(ctx, "/pprof/profile")
|
||||
//Assert(err, nil)
|
||||
//Assert(r.StatusCode, 200)
|
||||
//r.Close()
|
||||
|
||||
r, err = client.Get(ctx, "/pprof/symbol")
|
||||
Assert(err, nil)
|
||||
Assert(r.StatusCode, 200)
|
||||
r.Close()
|
||||
|
||||
r, err = client.Get(ctx, "/pprof/trace")
|
||||
Assert(err, nil)
|
||||
Assert(r.StatusCode, 200)
|
||||
r.Close()
|
||||
urlPaths := []string{
|
||||
"/pprof/index", "/pprof/cmdline", "/pprof/symbol", "/pprof/trace",
|
||||
}
|
||||
for _, urlPath := range urlPaths {
|
||||
r, err := client.Get(ctx, urlPath)
|
||||
AssertNil(err)
|
||||
Assert(r.StatusCode, 200)
|
||||
AssertNil(r.Close())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestServer_StartPProfServer(t *testing.T) {
|
||||
C(t, func(t *T) {
|
||||
s, err := ghttp.StartPProfServer(":0")
|
||||
t.AssertNil(err)
|
||||
|
||||
defer ghttp.ShutdownAllServer(ctx)
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d/debug", s.GetListenedPort()))
|
||||
|
||||
urlPaths := []string{
|
||||
"/pprof/index", "/pprof/cmdline", "/pprof/symbol", "/pprof/trace",
|
||||
}
|
||||
for _, urlPath := range urlPaths {
|
||||
r, err := client.Get(ctx, urlPath)
|
||||
AssertNil(err)
|
||||
Assert(r.StatusCode, 200)
|
||||
AssertNil(r.Close())
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user