timer.go 8.1 KB

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