timer.go 8.1 KB

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