You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/prof/profiler.go

64 lines
1.2 KiB
Go

4 years ago
package prof
import "github.com/tal-tech/go-zero/core/utils"
4 years ago
type (
// A ProfilePoint is a profile time point.
4 years ago
ProfilePoint struct {
*utils.ElapsedTimer
}
// A Profiler interface represents a profiler that used to report profile points.
4 years ago
Profiler interface {
Start() ProfilePoint
Report(name string, point ProfilePoint)
}
realProfiler struct{}
4 years ago
nullProfiler struct{}
4 years ago
)
var profiler = newNullProfiler()
// EnableProfiling enables profiling.
4 years ago
func EnableProfiling() {
profiler = newRealProfiler()
}
// Start starts a Profiler, and returns a start profiling point.
4 years ago
func Start() ProfilePoint {
return profiler.Start()
}
// Report reports a ProfilePoint with given name.
4 years ago
func Report(name string, point ProfilePoint) {
profiler.Report(name, point)
}
func newRealProfiler() Profiler {
return &realProfiler{}
4 years ago
}
func (rp *realProfiler) Start() ProfilePoint {
4 years ago
return ProfilePoint{
ElapsedTimer: utils.NewElapsedTimer(),
}
}
func (rp *realProfiler) Report(name string, point ProfilePoint) {
4 years ago
duration := point.Duration()
report(name, duration)
}
func newNullProfiler() Profiler {
return &nullProfiler{}
4 years ago
}
func (np *nullProfiler) Start() ProfilePoint {
4 years ago
return ProfilePoint{}
}
func (np *nullProfiler) Report(string, ProfilePoint) {
4 years ago
}