timer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Create a new timer with the given Histogram and Meter.
  29. func NewCustomTimer(h Histogram, m Meter) *StandardTimer {
  30. return &StandardTimer{h, m}
  31. }
  32. // Create a new timer with a standard histogram and meter. The histogram
  33. // will use an exponentially-decaying sample with the same reservoir size
  34. // and alpha as UNIX load averages.
  35. func NewTimer() *StandardTimer {
  36. return &StandardTimer{
  37. NewHistogram(NewExpDecaySample(1028, 0.015)),
  38. NewMeter(),
  39. }
  40. }
  41. // Return the count of inputs.
  42. func (t *StandardTimer) Count() int64 {
  43. return t.h.Count()
  44. }
  45. // Return the maximal value seen.
  46. func (t *StandardTimer) Max() int64 {
  47. return t.h.Max()
  48. }
  49. // Return the mean of all values seen.
  50. func (t *StandardTimer) Mean() float64 {
  51. return t.h.Mean()
  52. }
  53. // Return the minimal value seen.
  54. func (t *StandardTimer) Min() int64 {
  55. return t.h.Min()
  56. }
  57. // Return an arbitrary percentile of all values seen.
  58. func (t *StandardTimer) Percentile(p float64) float64 {
  59. return t.h.Percentile(p)
  60. }
  61. // Return a slice of arbitrary percentiles of all values seen.
  62. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  63. return t.h.Percentiles(ps)
  64. }
  65. // Return the meter's one-minute moving average rate of events.
  66. func (t *StandardTimer) Rate1() float64 {
  67. return t.m.Rate1()
  68. }
  69. // Return the meter's five-minute moving average rate of events.
  70. func (t *StandardTimer) Rate5() float64 {
  71. return t.m.Rate5()
  72. }
  73. // Return the meter's fifteen-minute moving average rate of events.
  74. func (t *StandardTimer) Rate15() float64 {
  75. return t.m.Rate15()
  76. }
  77. // Return the meter's mean rate of events.
  78. func (t *StandardTimer) RateMean() float64 {
  79. return t.m.RateMean()
  80. }
  81. // Return the standard deviation of all values seen.
  82. func (t *StandardTimer) StdDev() float64 {
  83. return t.h.StdDev()
  84. }
  85. // Record the duration of the execution of the given function.
  86. func (t *StandardTimer) Time(f func()) {
  87. ts := time.Now()
  88. f()
  89. t.Update(time.Since(ts))
  90. }
  91. // Record the duration of an event.
  92. func (t *StandardTimer) Update(d time.Duration) {
  93. t.h.Update(int64(d))
  94. t.m.Mark(1)
  95. }
  96. // Record the duration of an event that started at a time and ends now.
  97. func (t *StandardTimer) UpdateSince(ts time.Time) {
  98. t.h.Update(int64(time.Since(ts)))
  99. t.m.Mark(1)
  100. }