timer.go 3.9 KB

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