profiler.go 979 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package prof
  2. import "github.com/tal-tech/go-zero/core/utils"
  3. type (
  4. ProfilePoint struct {
  5. *utils.ElapsedTimer
  6. }
  7. Profiler interface {
  8. Start() ProfilePoint
  9. Report(name string, point ProfilePoint)
  10. }
  11. RealProfiler struct{}
  12. NullProfiler struct{}
  13. )
  14. var profiler = newNullProfiler()
  15. func EnableProfiling() {
  16. profiler = newRealProfiler()
  17. }
  18. func Start() ProfilePoint {
  19. return profiler.Start()
  20. }
  21. func Report(name string, point ProfilePoint) {
  22. profiler.Report(name, point)
  23. }
  24. func newRealProfiler() Profiler {
  25. return &RealProfiler{}
  26. }
  27. func (rp *RealProfiler) Start() ProfilePoint {
  28. return ProfilePoint{
  29. ElapsedTimer: utils.NewElapsedTimer(),
  30. }
  31. }
  32. func (rp *RealProfiler) Report(name string, point ProfilePoint) {
  33. duration := point.Duration()
  34. report(name, duration)
  35. }
  36. func newNullProfiler() Profiler {
  37. return &NullProfiler{}
  38. }
  39. func (np *NullProfiler) Start() ProfilePoint {
  40. return ProfilePoint{}
  41. }
  42. func (np *NullProfiler) Report(string, ProfilePoint) {
  43. }