diff --git a/comm/gameconfig.go b/comm/gameconfig.go index b2932aa99..eb355b16e 100644 --- a/comm/gameconfig.go +++ b/comm/gameconfig.go @@ -20,6 +20,8 @@ type GameConfig struct { Workers []string //工作服 BattleOpen bool //是否启用战斗 BattleAddr string //战斗服地址 + PprofOpen bool //是否启动性能监控 + PprofAddr string //性能监控查看端口 } //区服db配置 diff --git a/go.mod b/go.mod index a3292c9c8..7e31f82b1 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/tidwall/gjson v1.14.1 github.com/ugorji/go/codec v1.2.7 github.com/valyala/fastrand v1.1.0 + github.com/wolfogre/go-pprof-practice v0.0.0-20230706085634-23c8f603cac9 go.mongodb.org/mongo-driver v1.5.1 go.uber.org/multierr v1.6.0 golang.org/x/net v0.10.0 @@ -134,7 +135,7 @@ require ( go.opentelemetry.io/otel/trace v1.6.3 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/zap v1.17.0 // indirect - golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect + golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/mod v0.10.0 // indirect golang.org/x/sync v0.2.0 // indirect golang.org/x/sys v0.8.0 // indirect diff --git a/go.sum b/go.sum index b20521072..23c18d08e 100644 --- a/go.sum +++ b/go.sum @@ -578,6 +578,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9 github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +github.com/wolfogre/go-pprof-practice v0.0.0-20230706085634-23c8f603cac9 h1:JUZL5TQ/WK5veU2N171inEvwseqf4V0TxgGKl2Ypyvs= +github.com/wolfogre/go-pprof-practice v0.0.0-20230706085634-23c8f603cac9/go.mod h1:guVzD2XeU5rwULTURC4peeFyZya60U0BLKqisEjCylA= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= @@ -758,6 +760,7 @@ golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/lego/sys/pprof/core.go b/lego/sys/pprof/core.go new file mode 100644 index 000000000..51e07aaf3 --- /dev/null +++ b/lego/sys/pprof/core.go @@ -0,0 +1,22 @@ +package pprof + +type ( + ISys interface { + } +) + +var ( + defsys ISys +) + +func OnInit(config map[string]interface{}, option ...Option) (err error) { + if defsys, err = newSys(newOptions(config, option...)); err == nil { + } + return +} + +func NewSys(option ...Option) (sys ISys, err error) { + if sys, err = newSys(newOptionsByOption(option...)); err == nil { + } + return +} diff --git a/lego/sys/pprof/options.go b/lego/sys/pprof/options.go new file mode 100644 index 000000000..8ebbd18f0 --- /dev/null +++ b/lego/sys/pprof/options.go @@ -0,0 +1,41 @@ +package pprof + +import ( + "go_dreamfactory/lego/utils/mapstructure" +) + +type Option func(*Options) +type Options struct { + IsOpen bool + ListenPort int +} + +func SetIsOpen(v bool) Option { + return func(o *Options) { + o.IsOpen = v + } +} +func SetListenPort(v int) Option { + return func(o *Options) { + o.ListenPort = v + } +} + +func newOptions(config map[string]interface{}, opts ...Option) Options { + options := Options{} + if config != nil { + mapstructure.Decode(config, &options) + } + for _, o := range opts { + o(&options) + } + return options +} + +func newOptionsByOption(opts ...Option) Options { + options := Options{} + for _, o := range opts { + o(&options) + } + return options +} diff --git a/lego/sys/pprof/pprof.go b/lego/sys/pprof/pprof.go new file mode 100644 index 000000000..7fd5b0b0d --- /dev/null +++ b/lego/sys/pprof/pprof.go @@ -0,0 +1,41 @@ +package pprof + +import ( + "fmt" + "net/http" + _ "net/http/pprof" + "os" + "time" + + "go_dreamfactory/lego/sys/log" + + "github.com/wolfogre/go-pprof-practice/animal" +) + +func newSys(options Options) (sys *Pprof, err error) { + if options.IsOpen { + sys = &Pprof{options: options} + go sys.Start() + } + return +} + +type Pprof struct { + options Options +} + +func (this *Pprof) Start() { + go func() { + if err := http.ListenAndServe(fmt.Sprintf(":%d", this.options.ListenPort), nil); err != nil { + log.Fatalf("Start Pprof Fatalf err:%v", err) + } + os.Exit(0) + }() + + for { + for _, v := range animal.AllAnimals { + v.Live() + } + time.Sleep(time.Second) + } +} diff --git a/lego/sys/pprof/sys_test.go b/lego/sys/pprof/sys_test.go new file mode 100644 index 000000000..2bde94230 --- /dev/null +++ b/lego/sys/pprof/sys_test.go @@ -0,0 +1,19 @@ +package pprof_test + +/* +查看堆栈调用信息 +go tool pprof http://localhost:6060/debug/pprof/heap +查看 30 秒内的 CPU 信息 +go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30 +查看 goroutine 阻塞 +go tool pprof http://localhost:6060/debug/pprof/block +收集 5 秒内的执行路径 +go tool pprof http://localhost:6060/debug/pprof/trace?seconds=5 +争用互斥持有者的堆栈跟踪 +go tool pprof http://localhost:6060/debug/pprof/mutex +*/ +/* +UI web 界面 +curl -sK -v http://localhost:6060/debug/pprof/heap > heap.out +go tool pprof -http=:8080 heap.out +*/ diff --git a/modules/mainline/module.go b/modules/mainline/module.go index 795c9d5fd..c314ed236 100644 --- a/modules/mainline/module.go +++ b/modules/mainline/module.go @@ -147,7 +147,7 @@ func (this *Mainline) BingoJumpLevel(session comm.IUserSession, level int32) (er return } -// 查询用户主线通关 +// 查询用户主线通关 db.changeUserPassword("root", "palsweasd*1!") func (this *Mainline) InquireMainLinePassLevel(uid string) (levels map[int32]int32) { var ( info *pb.DBMainline diff --git a/modules/robot/robot.go b/modules/robot/robot.go index 922b62aa5..994fe9b93 100644 --- a/modules/robot/robot.go +++ b/modules/robot/robot.go @@ -313,7 +313,7 @@ func (this *Robot) run() { this.Close() return } - time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(1000))) + time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(2000))) } for this.cycle { @@ -345,7 +345,7 @@ func (this *Robot) run() { this.cycle = true } } - time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(1000))) + time.Sleep(time.Millisecond * time.Duration(100+rand.Int31n(2000))) } } this.Close() diff --git a/services/worker/main.go b/services/worker/main.go index d83329329..1b2ca563e 100644 --- a/services/worker/main.go +++ b/services/worker/main.go @@ -92,6 +92,7 @@ import ( "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/cron" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/sys/pprof" "go_dreamfactory/lego/sys/timewheel" ) @@ -235,4 +236,10 @@ func (this *Service) InitSys() { } else { log.Infof("init sys.db success!") } + //性能监控 + if err := pprof.OnInit(this.GetSettings().Sys["pprof"]); err != nil { + panic(fmt.Sprintf("init sys.pprof err: %s", err.Error())) + } else { + log.Infof("init sys.pprof success!") + } }