timer.go 8.7 KB

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