timer.go 2.6 KB

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