timer.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. Sum() int64
  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. // Max is a no-op.
  72. func (NilTimer) Max() int64 { return 0 }
  73. // Mean is a no-op.
  74. func (NilTimer) Mean() float64 { return 0.0 }
  75. // Min is a no-op.
  76. func (NilTimer) Min() int64 { return 0 }
  77. // Percentile is a no-op.
  78. func (NilTimer) Percentile(p float64) float64 { return 0.0 }
  79. // Percentiles is a no-op.
  80. func (NilTimer) Percentiles(ps []float64) []float64 {
  81. return make([]float64, len(ps))
  82. }
  83. // Rate1 is a no-op.
  84. func (NilTimer) Rate1() float64 { return 0.0 }
  85. // Rate5 is a no-op.
  86. func (NilTimer) Rate5() float64 { return 0.0 }
  87. // Rate15 is a no-op.
  88. func (NilTimer) Rate15() float64 { return 0.0 }
  89. // RateMean is a no-op.
  90. func (NilTimer) RateMean() float64 { return 0.0 }
  91. // Snapshot is a no-op.
  92. func (NilTimer) Snapshot() Timer { return NilTimer{} }
  93. // StdDev is a no-op.
  94. func (NilTimer) StdDev() float64 { return 0.0 }
  95. // Sum is a no-op.
  96. func (NilTimer) Sum() int64 { return 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. // Max returns the maximum value in the sample.
  117. func (t *StandardTimer) Max() int64 {
  118. return t.histogram.Max()
  119. }
  120. // Mean returns the mean of the values in the sample.
  121. func (t *StandardTimer) Mean() float64 {
  122. return t.histogram.Mean()
  123. }
  124. // Min returns the minimum value in the sample.
  125. func (t *StandardTimer) Min() int64 {
  126. return t.histogram.Min()
  127. }
  128. // Percentile returns an arbitrary percentile of the values in the sample.
  129. func (t *StandardTimer) Percentile(p float64) float64 {
  130. return t.histogram.Percentile(p)
  131. }
  132. // Percentiles returns a slice of arbitrary percentiles of the values in the
  133. // sample.
  134. func (t *StandardTimer) Percentiles(ps []float64) []float64 {
  135. return t.histogram.Percentiles(ps)
  136. }
  137. // Rate1 returns the one-minute moving average rate of events per second.
  138. func (t *StandardTimer) Rate1() float64 {
  139. return t.meter.Rate1()
  140. }
  141. // Rate5 returns the five-minute moving average rate of events per second.
  142. func (t *StandardTimer) Rate5() float64 {
  143. return t.meter.Rate5()
  144. }
  145. // Rate15 returns the fifteen-minute moving average rate of events per second.
  146. func (t *StandardTimer) Rate15() float64 {
  147. return t.meter.Rate15()
  148. }
  149. // RateMean returns the meter's mean rate of events per second.
  150. func (t *StandardTimer) RateMean() float64 {
  151. return t.meter.RateMean()
  152. }
  153. // Snapshot returns a read-only copy of the timer.
  154. func (t *StandardTimer) Snapshot() Timer {
  155. t.mutex.Lock()
  156. defer t.mutex.Unlock()
  157. return &TimerSnapshot{
  158. histogram: t.histogram.Snapshot().(*HistogramSnapshot),
  159. meter: t.meter.Snapshot().(*MeterSnapshot),
  160. }
  161. }
  162. // StdDev returns the standard deviation of the values in the sample.
  163. func (t *StandardTimer) StdDev() float64 {
  164. return t.histogram.StdDev()
  165. }
  166. // Sum returns the sum in the sample.
  167. func (t *StandardTimer) Sum() int64 {
  168. return t.histogram.Sum()
  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. // Max returns the maximum value at the time the snapshot was taken.
  203. func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
  204. // Mean returns the mean value at the time the snapshot was taken.
  205. func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
  206. // Min returns the minimum value at the time the snapshot was taken.
  207. func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
  208. // Percentile returns an arbitrary percentile of sampled values at the time the
  209. // snapshot was taken.
  210. func (t *TimerSnapshot) Percentile(p float64) float64 {
  211. return t.histogram.Percentile(p)
  212. }
  213. // Percentiles returns a slice of arbitrary percentiles of sampled values at
  214. // the time the snapshot was taken.
  215. func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
  216. return t.histogram.Percentiles(ps)
  217. }
  218. // Rate1 returns the one-minute moving average rate of events per second at the
  219. // time the snapshot was taken.
  220. func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
  221. // Rate5 returns the five-minute moving average rate of events per second at
  222. // the time the snapshot was taken.
  223. func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
  224. // Rate15 returns the fifteen-minute moving average rate of events per second
  225. // at the time the snapshot was taken.
  226. func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
  227. // RateMean returns the meter's mean rate of events per second at the time the
  228. // snapshot was taken.
  229. func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
  230. // Snapshot returns the snapshot.
  231. func (t *TimerSnapshot) Snapshot() Timer { return t }
  232. // StdDev returns the standard deviation of the values at the time the snapshot
  233. // was taken.
  234. func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
  235. // Sum returns the sum at the time the snapshot was taken.
  236. func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }
  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() }