diff --git a/context.go b/context.go index f353635..b169f96 100644 --- a/context.go +++ b/context.go @@ -52,6 +52,7 @@ func (c *Context) SetAccessLogFormat(v iface.IAccessLogFormat) { func (c *Context) SetEnableAccessLog(v bool) { c.enableAccessLog = v + c.SetProfileEnable(v) } @@ -61,6 +62,7 @@ func (c *Context) HttpRW(debug, enableAccessLog bool, r *http.Request, w http.Re c.input = r c.output = &c.response c.response.reset(w) + c.SetProfileEnable(enableAccessLog) } func (c *Context) reset() { diff --git a/iface/interface.go b/iface/interface.go index ac88969..853c99a 100644 --- a/iface/interface.go +++ b/iface/interface.go @@ -141,6 +141,7 @@ type IContext interface { ProfileString() string SetAccessLogFormat(v IAccessLogFormat) SetEnableAccessLog(v bool) + SetProfileEnable(v bool) } type IObjPoolFunc func(obj IObject, params ...interface{}) IObject diff --git a/logs/profiler.go b/logs/profiler.go index ec87eec..07e14db 100644 --- a/logs/profiler.go +++ b/logs/profiler.go @@ -9,15 +9,16 @@ import ( ) func NewProfiler() *Profiler { - return &Profiler{} + return &Profiler{ProfileEnable:true} } // Profiler type Profiler struct { - pushLog []string - counting map[string][2]int - profile map[string][2]int - profileStack map[string]time.Time + pushLog []string + counting map[string][2]int + profile map[string][2]int + profileStack map[string]time.Time + ProfileEnable bool } func (p *Profiler) Reset() { @@ -27,8 +28,17 @@ func (p *Profiler) Reset() { p.profileStack = nil } +// SetEnable log switch +func (p *Profiler) SetProfileEnable(v bool) { + p.ProfileEnable = v +} + // PushLog add push log, the push log string is key=Util.ToString(v) func (p *Profiler) PushLog(key string, v interface{}) { + if !p.ProfileEnable { + return + } + if p.pushLog == nil { p.pushLog = make([]string, 0) } @@ -39,6 +49,10 @@ func (p *Profiler) PushLog(key string, v interface{}) { // Counting add counting info, the counting string is key=sum(hit)/sum(total) func (p *Profiler) Counting(key string, hit, total int) { + if !p.ProfileEnable { + return + } + if p.counting == nil { p.counting = make(map[string][2]int) } @@ -59,6 +73,10 @@ func (p *Profiler) Counting(key string, hit, total int) { // ProfileStart mark start of profile func (p *Profiler) ProfileStart(key string) { + if !p.ProfileEnable { + return + } + if p.profileStack == nil { p.profileStack = make(map[string]time.Time) } @@ -68,6 +86,10 @@ func (p *Profiler) ProfileStart(key string) { // ProfileStop mark stop of profile func (p *Profiler) ProfileStop(key string) { + if !p.ProfileEnable { + return + } + if startTime, ok := p.profileStack[key]; ok { delete(p.profileStack, key) p.ProfileAdd(key, time.Now().Sub(startTime)) @@ -76,6 +98,10 @@ func (p *Profiler) ProfileStop(key string) { // ProfileAdd add profile info, the profile string is key=sum(elapse)/count func (p *Profiler) ProfileAdd(key string, elapse time.Duration) { + if !p.ProfileEnable { + return + } + if p.profile == nil { p.profile = make(map[string][2]int) } diff --git a/server.go b/server.go index f85c1ed..8beb06c 100644 --- a/server.go +++ b/server.go @@ -268,7 +268,9 @@ func (s *Server) Serve() { // ServeCMD serve command request func (s *Server) ServeCMD() { - ctx := Context{debug: s.debug, enableAccessLog: s.enableAccessLog, accessLogFormat:s.accessLogFormat} + ctx := Context{debug: s.debug} + ctx.SetEnableAccessLog(s.enableAccessLog) + ctx.SetAccessLogFormat(s.accessLogFormat) // only apply the last plugin for command ctx.Process(s.plugins[len(s.plugins)-1:]) } diff --git a/test/mock/iface/interface.go b/test/mock/iface/interface.go index 63d2e87..00d50cc 100644 --- a/test/mock/iface/interface.go +++ b/test/mock/iface/interface.go @@ -1439,6 +1439,18 @@ func (mr *MockIContextMockRecorder) SetEnableAccessLog(v interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetEnableAccessLog", reflect.TypeOf((*MockIContext)(nil).SetEnableAccessLog), v) } +// SetProfileEnable mocks base method +func (m *MockIContext) SetProfileEnable(v bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetProfileEnable", v) +} + +// SetProfileEnable indicates an expected call of SetProfileEnable +func (mr *MockIContextMockRecorder) SetProfileEnable(v interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetProfileEnable", reflect.TypeOf((*MockIContext)(nil).SetProfileEnable), v) +} + // MockIAccessLogFormat is a mock of IAccessLogFormat interface type MockIAccessLogFormat struct { ctrl *gomock.Controller