timer.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package metrics
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Timers capture the duration and rate of events.
  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. Variance() float64
  23. }
  24. // GetOrRegisterTimer returns an existing Timer or constructs and registers a
  25. // new StandardTimer.
  26. func GetOrRegisterTimer(name string, r Registry) Timer {
  27. if nil == r {
  28. r = DefaultRegistry
  29. }
  30. return r.GetOrRegister(name, NewTimer()).(Timer)
  31. }
  32. // NewCustomTimer constructs a new StandardTimer from a Histogram and a Meter.
  33. func NewCustomTimer(h Histogram, m Meter) Timer {
  34. if UseNilMetrics {
  35. return NilTimer{}
  36. }
  37. return &StandardTimer{
  38. histogram: h,
  39. meter: m,
  40. }
  41. }
  42. // NewRegisteredTimer constructs and registers a new StandardTimer.
  43. func NewRegisteredTimer(name string, r Registry) Timer {
  44. c := NewTimer()
  45. if nil == r {
  46. r = DefaultRegistry
  47. }
  48. r.Register(name, c)
  49. return c
  50. }
  51. // NewTimer constructs a new StandardTimer using an exponentially-decaying
  52. // sample with the same reservoir size and alpha as UNIX load averages.
  53. func NewTimer() Timer {
  54. if UseNilMetrics {
  55. return NilTimer{}
  56. }
  57. return &StandardTimer{
  58. histogram: NewHistogram(NewExpDecaySample(1028, 0.015)),
  59. meter: NewMeter(),
  60. }
  61. }
  62. // NilTimer is a no-op Timer.
  63. type NilTimer struct {
  64. h Histogram
  65. m Meter
  66. }
  67. // Count is a no-op.
  68. func (NilTimer) Count() int64 { return 0 }
  69. // Max is a no-op.
  70. func (NilTimer) Max() int64 { return 0 }
  71. // Mean is a no-op.
  72. func (NilTimer) Mean() float64 { return 0.0 }
  73. // Min is a no-op.
  74. func (NilTimer) Min() int64 { return 0 }
  75. // Percentile is a no-op.
  76. func (NilTimer) Percentile(p float64) float64 { return 0.0 }
  77. // Percentiles is a no-op.
  78. func (NilTimer) Percentiles(ps []float64) []float64 {
  79. return make([]float64, len(ps))
  80. }
  81. // Rate1 is a no-op.
  82. func (NilTimer) Rate1() float64 { return 0.0 }
  83. // Rate5 is a no-op.
  84. func (NilTimer) Rate5() float64 { return 0.0 }
  85. // Rate15 is a no-op.
  86. func (NilTimer) Rate15() float64 { return 0.0 }
  87. // RateMean is a no-op.
  88. func (NilTimer) RateMean() float64 { return 0.0 }
  89. // No-op.
  90. func (NilTimer) StdDev() float64 { return 0.0 }
  91. // Time is a no-op.
  92. func (NilTimer) Time(func()) {}
  93. // Update is a no-op.
  94. func (NilTimer) Update(time.Duration) {}
  95. // UpdateSince is a no-op.
  96. func (NilTimer) UpdateSince(time.Time) {}
  97. // Variance is a no-op.
  98. func (NilTimer) Variance() float64 { return 0.0 }
  99. // StandardTimer is the standard implementation of a Timer and uses a Histogram
  100. // and Meter.
  101. type StandardTimer struct {
  102. histogram Histogram
  103. meter Meter
  104. mutex sync.Mutex
  105. }
  106. // Count returns the number of events recorded.
  107. func (t *StandardTimer) Count() int64 {
  108. return t.histogram.Count()
  109. }
  110. // Max returns the maximum value in the sample.
  111. func (t *StandardTimer) Max() int64 {
  112. return t.histogram.Max()
  113. }
  114. // Mean returns the mean of the values in the sample.
  115. func (t *StandardTimer) Mean() float64 {
  116. return t.histogram.Mean()
  117. }
  118. // Min returns the minimum value in the sample.
  119. func (t *StandardTimer) Min() int64 {
  120. return t.histogram.Min()
  121. }
  122. // Percentile returns an arbitrary percentile of the values in the sample.
  123. func (t *StandardTimer) Percentile(p float64) float64 {
  124. return t.histogram.Percentile(p)
  125. }
  126. // Percentiles returns a slice of arbitrary percentiles of the values in the
  127. // sample.
  128. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  129. return t.histogram.Percentiles(ps)
  130. }
  131. // Rate1 returns the one-minute moving average rate of events per second.
  132. func (t *StandardTimer) Rate1() float64 {
  133. return t.meter.Rate1()
  134. }
  135. // Rate5 returns the five-minute moving average rate of events per second.
  136. func (t *StandardTimer) Rate5() float64 {
  137. return t.meter.Rate5()
  138. }
  139. // Rate15 returns the fifteen-minute moving average rate of events per second.
  140. func (t *StandardTimer) Rate15() float64 {
  141. return t.meter.Rate15()
  142. }
  143. // RateMean returns the meter's mean rate of events per second.
  144. func (t *StandardTimer) RateMean() float64 {
  145. return t.m.RateMean()
  146. }
  147. // StdDev returns the standard deviation of the values in the sample.
  148. func (t *StandardTimer) StdDev() float64 {
  149. return t.histogram.StdDev()
  150. }
  151. // Record the duration of the execution of the given function.
  152. func (t *StandardTimer) Time(f func()) {
  153. ts := time.Now()
  154. f()
  155. t.Update(time.Since(ts))
  156. }
  157. // Record the duration of an event.
  158. func (t *StandardTimer) Update(d time.Duration) {
  159. t.h.Update(int64(d))
  160. t.m.Mark(1)
  161. }
  162. // Record the duration of an event that started at a time and ends now.
  163. func (t *StandardTimer) UpdateSince(ts time.Time) {
  164. t.h.Update(int64(time.Since(ts)))
  165. t.m.Mark(1)
  166. }