timer.go 4.1 KB

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