timer.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package metrics
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Timers capture the duration and rate of events.
  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. Snapshot() Timer
  19. StdDev() float64
  20. Time(func())
  21. Update(time.Duration)
  22. UpdateSince(time.Time)
  23. Variance() float64
  24. }
  25. // GetOrRegisterTimer returns an existing Timer or constructs and registers a
  26. // new StandardTimer.
  27. func GetOrRegisterTimer(name string, r Registry) Timer {
  28. if nil == r {
  29. r = DefaultRegistry
  30. }
  31. return r.GetOrRegister(name, NewTimer()).(Timer)
  32. }
  33. // NewCustomTimer constructs a new StandardTimer from a Histogram and a Meter.
  34. func NewCustomTimer(h Histogram, m Meter) Timer {
  35. if UseNilMetrics {
  36. return NilTimer{}
  37. }
  38. return &StandardTimer{
  39. histogram: h,
  40. meter: m,
  41. }
  42. }
  43. // NewRegisteredTimer constructs and registers a new StandardTimer.
  44. func NewRegisteredTimer(name string, r Registry) Timer {
  45. c := NewTimer()
  46. if nil == r {
  47. r = DefaultRegistry
  48. }
  49. r.Register(name, c)
  50. return c
  51. }
  52. // NewTimer constructs a new StandardTimer using an exponentially-decaying
  53. // sample with the same reservoir size and alpha as UNIX load averages.
  54. func NewTimer() Timer {
  55. if UseNilMetrics {
  56. return NilTimer{}
  57. }
  58. return &StandardTimer{
  59. histogram: NewHistogram(NewExpDecaySample(1028, 0.015)),
  60. meter: NewMeter(),
  61. }
  62. }
  63. // NilTimer is a no-op Timer.
  64. type NilTimer struct {
  65. h Histogram
  66. m Meter
  67. }
  68. // Count is a no-op.
  69. func (NilTimer) Count() int64 { return 0 }
  70. // Max is a no-op.
  71. func (NilTimer) Max() int64 { return 0 }
  72. // Mean is a no-op.
  73. func (NilTimer) Mean() float64 { return 0.0 }
  74. // Min is a no-op.
  75. func (NilTimer) Min() int64 { return 0 }
  76. // Percentile is a no-op.
  77. func (NilTimer) Percentile(p float64) float64 { return 0.0 }
  78. // Percentiles is a no-op.
  79. func (NilTimer) Percentiles(ps []float64) []float64 {
  80. return make([]float64, len(ps))
  81. }
  82. // Rate1 is a no-op.
  83. func (NilTimer) Rate1() float64 { return 0.0 }
  84. // Rate5 is a no-op.
  85. func (NilTimer) Rate5() float64 { return 0.0 }
  86. // Rate15 is a no-op.
  87. func (NilTimer) Rate15() float64 { return 0.0 }
  88. // RateMean is a no-op.
  89. func (NilTimer) RateMean() float64 { return 0.0 }
  90. // Snapshot is a no-op.
  91. func (NilTimer) Snapshot() Timer { return NilTimer{} }
  92. // StdDev is a no-op.
  93. func (NilTimer) StdDev() float64 { return 0.0 }
  94. // Time is a no-op.
  95. func (NilTimer) Time(func()) {}
  96. // Update is a no-op.
  97. func (NilTimer) Update(time.Duration) {}
  98. // UpdateSince is a no-op.
  99. func (NilTimer) UpdateSince(time.Time) {}
  100. // Variance is a no-op.
  101. func (NilTimer) Variance() float64 { return 0.0 }
  102. // StandardTimer is the standard implementation of a Timer and uses a Histogram
  103. // and Meter.
  104. type StandardTimer struct {
  105. histogram Histogram
  106. meter Meter
  107. mutex sync.Mutex
  108. }
  109. // Count returns the number of events recorded.
  110. func (t *StandardTimer) Count() int64 {
  111. return t.histogram.Count()
  112. }
  113. // Max returns the maximum value in the sample.
  114. func (t *StandardTimer) Max() int64 {
  115. return t.histogram.Max()
  116. }
  117. // Mean returns the mean of the values in the sample.
  118. func (t *StandardTimer) Mean() float64 {
  119. return t.histogram.Mean()
  120. }
  121. // Min returns the minimum value in the sample.
  122. func (t *StandardTimer) Min() int64 {
  123. return t.histogram.Min()
  124. }
  125. // Percentile returns an arbitrary percentile of the values in the sample.
  126. func (t *StandardTimer) Percentile(p float64) float64 {
  127. return t.histogram.Percentile(p)
  128. }
  129. // Percentiles returns a slice of arbitrary percentiles of the values in the
  130. // sample.
  131. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  132. return t.histogram.Percentiles(ps)
  133. }
  134. // Rate1 returns the one-minute moving average rate of events per second.
  135. func (t *StandardTimer) Rate1() float64 {
  136. return t.meter.Rate1()
  137. }
  138. // Rate5 returns the five-minute moving average rate of events per second.
  139. func (t *StandardTimer) Rate5() float64 {
  140. return t.meter.Rate5()
  141. }
  142. // Rate15 returns the fifteen-minute moving average rate of events per second.
  143. func (t *StandardTimer) Rate15() float64 {
  144. return t.meter.Rate15()
  145. }
  146. // RateMean returns the meter's mean rate of events per second.
  147. func (t *StandardTimer) RateMean() float64 {
  148. return t.meter.RateMean()
  149. }
  150. // Snapshot returns a read-only copy of the timer.
  151. func (t *StandardTimer) Snapshot() Timer {
  152. t.mutex.Lock()
  153. defer t.mutex.Unlock()
  154. return &TimerSnapshot{
  155. histogram: t.histogram.Snapshot().(*HistogramSnapshot),
  156. meter: t.meter.Snapshot().(*MeterSnapshot),
  157. }
  158. }
  159. // StdDev returns the standard deviation of the values in the sample.
  160. func (t *StandardTimer) StdDev() float64 {
  161. return t.histogram.StdDev()
  162. }
  163. // Record the duration of the execution of the given function.
  164. func (t *StandardTimer) Time(f func()) {
  165. ts := time.Now()
  166. f()
  167. t.Update(time.Since(ts))
  168. }
  169. // Record the duration of an event.
  170. func (t *StandardTimer) Update(d time.Duration) {
  171. t.mutex.Lock()
  172. defer t.mutex.Unlock()
  173. t.histogram.Update(int64(d))
  174. t.meter.Mark(1)
  175. }
  176. // Record the duration of an event that started at a time and ends now.
  177. func (t *StandardTimer) UpdateSince(ts time.Time) {
  178. t.mutex.Lock()
  179. defer t.mutex.Unlock()
  180. t.histogram.Update(int64(time.Since(ts)))
  181. t.meter.Mark(1)
  182. }
  183. // Variance returns the variance of the values in the sample.
  184. func (t *StandardTimer) Variance() float64 {
  185. return t.histogram.Variance()
  186. }
  187. // TimerSnapshot is a read-only copy of another Timer.
  188. type TimerSnapshot struct {
  189. histogram *HistogramSnapshot
  190. meter *MeterSnapshot
  191. }
  192. // Count returns the number of events recorded at the time the snapshot was
  193. // taken.
  194. func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
  195. // Max returns the maximum value at the time the snapshot was taken.
  196. func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
  197. // Mean returns the mean value at the time the snapshot was taken.
  198. func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
  199. // Min returns the minimum value at the time the snapshot was taken.
  200. func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
  201. // Percentile returns an arbitrary percentile of sampled values at the time the
  202. // snapshot was taken.
  203. func (t *TimerSnapshot) Percentile(p float64) float64 {
  204. return t.histogram.Percentile(p)
  205. }
  206. // Percentiles returns a slice of arbitrary percentiles of sampled values at
  207. // the time the snapshot was taken.
  208. func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
  209. return t.histogram.Percentiles(ps)
  210. }
  211. // Rate1 returns the one-minute moving average rate of events per second at the
  212. // time the snapshot was taken.
  213. func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
  214. // Rate5 returns the five-minute moving average rate of events per second at
  215. // the time the snapshot was taken.
  216. func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
  217. // Rate15 returns the fifteen-minute moving average rate of events per second
  218. // at the time the snapshot was taken.
  219. func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
  220. // RateMean returns the meter's mean rate of events per second at the time the
  221. // snapshot was taken.
  222. func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
  223. // Snapshot returns the snapshot.
  224. func (t *TimerSnapshot) Snapshot() Timer { return t }
  225. // StdDev returns the standard deviation of the values at the time the snapshot
  226. // was taken.
  227. func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
  228. // Time panics.
  229. func (*TimerSnapshot) Time(func()) {
  230. panic("Time called on a TimerSnapshot")
  231. }
  232. // Update panics.
  233. func (*TimerSnapshot) Update(time.Duration) {
  234. panic("Update called on a TimerSnapshot")
  235. }
  236. // UpdateSince panics.
  237. func (*TimerSnapshot) UpdateSince(time.Time) {
  238. panic("UpdateSince called on a TimerSnapshot")
  239. }
  240. // Variance returns the variance of the values at the time the snapshot was
  241. // taken.
  242. func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }