timer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package metrics
  2. import "time"
  3. // Timers capture the duration and rate of events.
  4. //
  5. // This is an interface so as to encourage other structs to implement
  6. // the Timer API as appropriate.
  7. type Timer interface {
  8. Count() int64
  9. Max() int64
  10. Mean() float64
  11. Min() int64
  12. Percentile(float64) float64
  13. Percentiles([]float64) []float64
  14. Rate1() float64
  15. Rate5() float64
  16. Rate15() float64
  17. RateMean() float64
  18. StdDev() float64
  19. Time(func())
  20. Update(time.Duration)
  21. UpdateSince(time.Time)
  22. }
  23. // The standard implementation of a Timer uses a Histogram and Meter directly.
  24. type StandardTimer struct {
  25. h Histogram
  26. m Meter
  27. }
  28. // Force the compiler to check that StandardTimer implements Timer.
  29. var _ Timer = &StandardTimer{}
  30. // Create a new timer with the given Histogram and Meter.
  31. func NewCustomTimer(h Histogram, m Meter) *StandardTimer {
  32. return &StandardTimer{h, m}
  33. }
  34. // Create a new timer with a standard histogram and meter. The histogram
  35. // will use an exponentially-decaying sample with the same reservoir size
  36. // and alpha as UNIX load averages.
  37. func NewTimer() *StandardTimer {
  38. return &StandardTimer{
  39. NewHistogram(NewExpDecaySample(1028, 0.015)),
  40. NewMeter(),
  41. }
  42. }
  43. // Return the count of inputs.
  44. func (t *StandardTimer) Count() int64 {
  45. return t.h.Count()
  46. }
  47. // Return the maximal value seen.
  48. func (t *StandardTimer) Max() int64 {
  49. return t.h.Max()
  50. }
  51. // Return the mean of all values seen.
  52. func (t *StandardTimer) Mean() float64 {
  53. return t.h.Mean()
  54. }
  55. // Return the minimal value seen.
  56. func (t *StandardTimer) Min() int64 {
  57. return t.h.Min()
  58. }
  59. // Return an arbitrary percentile of all values seen.
  60. func (t *StandardTimer) Percentile(p float64) float64 {
  61. return t.h.Percentile(p)
  62. }
  63. // Return a slice of arbitrary percentiles of all values seen.
  64. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  65. return t.h.Percentiles(ps)
  66. }
  67. // Return the meter's one-minute moving average rate of events.
  68. func (t *StandardTimer) Rate1() float64 {
  69. return t.m.Rate1()
  70. }
  71. // Return the meter's five-minute moving average rate of events.
  72. func (t *StandardTimer) Rate5() float64 {
  73. return t.m.Rate5()
  74. }
  75. // Return the meter's fifteen-minute moving average rate of events.
  76. func (t *StandardTimer) Rate15() float64 {
  77. return t.m.Rate15()
  78. }
  79. // Return the meter's mean rate of events.
  80. func (t *StandardTimer) RateMean() float64 {
  81. return t.m.RateMean()
  82. }
  83. // Return the standard deviation of all values seen.
  84. func (t *StandardTimer) StdDev() float64 {
  85. return t.h.StdDev()
  86. }
  87. // Record the duration of the execution of the given function.
  88. func (t *StandardTimer) Time(f func()) {
  89. ts := time.Now()
  90. f()
  91. t.Update(time.Since(ts))
  92. }
  93. // Record the duration of an event.
  94. func (t *StandardTimer) Update(d time.Duration) {
  95. t.h.Update(int64(d))
  96. t.m.Mark(1)
  97. }
  98. // Record the duration of an event that started at a time and ends now.
  99. func (t *StandardTimer) UpdateSince(ts time.Time) {
  100. t.h.Update(int64(time.Since(ts)))
  101. t.m.Mark(1)
  102. }