bbgo_origin/pkg/util/profile.go

43 lines
762 B
Go
Raw Normal View History

2021-06-09 15:52:16 +00:00
package util
2022-06-02 08:51:44 +00:00
import (
"time"
)
2021-06-09 15:52:16 +00:00
type TimeProfile struct {
2022-06-02 08:51:44 +00:00
Name string
2021-06-09 15:52:16 +00:00
StartTime, EndTime time.Time
Duration time.Duration
}
2022-06-02 08:51:44 +00:00
func StartTimeProfile(args ...string) TimeProfile {
name := ""
if len(args) > 0 {
name = args[0]
}
return TimeProfile{StartTime: time.Now(), Name: name}
2021-06-09 15:52:16 +00:00
}
func (p *TimeProfile) TilNow() time.Duration {
return time.Now().Sub(p.StartTime)
}
func (p *TimeProfile) Stop() time.Duration {
p.EndTime = time.Now()
p.Duration = p.EndTime.Sub(p.StartTime)
return p.Duration
}
2022-06-02 08:51:44 +00:00
type logFunction func(format string, args ...interface{})
func (p *TimeProfile) StopAndLog(f logFunction) {
duration := p.Stop()
s := "[profile] "
if len(p.Name) > 0 {
s += p.Name
}
s += " " + duration.String()
f(s)
}