timer.go 3.9 KB

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