Skip to content

Commit

Permalink
Merge pull request #78 from pinguo-yangbing/master
Browse files Browse the repository at this point in the history
setProfileEnable
  • Loading branch information
pinguo-yangbing authored Sep 24, 2020
2 parents 329d251 + 4a0e8da commit 9457817
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (c *Context) SetAccessLogFormat(v iface.IAccessLogFormat) {

func (c *Context) SetEnableAccessLog(v bool) {
c.enableAccessLog = v
c.SetProfileEnable(v)
}


Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions iface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 31 additions & 5 deletions logs/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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))
Expand All @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
}
Expand Down
12 changes: 12 additions & 0 deletions test/mock/iface/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9457817

Please sign in to comment.