histogram.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package metrics
  2. import (
  3. "math"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. // Histograms calculate distribution statistics from a series of int64 values.
  8. type Histogram interface {
  9. Clear()
  10. Count() int64
  11. Max() int64
  12. Mean() float64
  13. Min() int64
  14. Percentile(float64) float64
  15. Percentiles([]float64) []float64
  16. Sample() Sample
  17. Snapshot() Histogram
  18. StdDev() float64
  19. Update(int64)
  20. Variance() float64
  21. }
  22. // GetOrRegisterHistogram returns an existing Histogram or constructs and
  23. // registers a new StandardHistogram.
  24. func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
  25. if nil == r {
  26. r = DefaultRegistry
  27. }
  28. return r.GetOrRegister(name, NewHistogram(s)).(Histogram)
  29. }
  30. // NewHistogram constructs a new StandardHistogram from a Sample.
  31. func NewHistogram(s Sample) Histogram {
  32. if UseNilMetrics {
  33. return NilHistogram{}
  34. }
  35. return &StandardHistogram{
  36. max: math.MinInt64,
  37. min: math.MaxInt64,
  38. sample: s,
  39. sampleMean: -1.0,
  40. }
  41. }
  42. // NewRegisteredHistogram constructs and registers a new StandardHistogram from
  43. // a Sample.
  44. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
  45. c := NewHistogram(s)
  46. if nil == r {
  47. r = DefaultRegistry
  48. }
  49. r.Register(name, c)
  50. return c
  51. }
  52. // HistogramSnapshot is a read-only copy of another Histogram.
  53. type HistogramSnapshot struct {
  54. count, max, min, sum int64
  55. sample *SampleSnapshot
  56. variance float64
  57. }
  58. // Clear panics.
  59. func (*HistogramSnapshot) Clear() {
  60. panic("Clear called on a HistogramSnapshot")
  61. }
  62. // Count returns the count of inputs at the time the snapshot was taken.
  63. func (h *HistogramSnapshot) Count() int64 { return h.count }
  64. // Max returns the maximum value at the time the snapshot was taken.
  65. func (h *HistogramSnapshot) Max() int64 { return h.max }
  66. // Mean returns the mean value at the time the snapshot was taken.
  67. func (h *HistogramSnapshot) Mean() float64 {
  68. return float64(h.sum) / float64(h.count)
  69. }
  70. // Min returns the minimum value at the time the snapshot was taken.
  71. func (h *HistogramSnapshot) Min() int64 { return h.min }
  72. // Percentile returns an arbitrary percentile of sampled values at the time the
  73. // snapshot was taken.
  74. func (h *HistogramSnapshot) Percentile(p float64) float64 {
  75. return h.sample.Percentile(p)
  76. }
  77. // Percentiles returns a slice of arbitrary percentiles of sampled values at
  78. // the time the snapshot was taken.
  79. func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 {
  80. return h.sample.Percentiles(ps)
  81. }
  82. // Sample returns the underlying Sample.
  83. func (h *HistogramSnapshot) Sample() Sample { return h.sample }
  84. // Snapshot returns the snapshot.
  85. func (h *HistogramSnapshot) Snapshot() Histogram { return h }
  86. // StdDev returns the standard deviation of inputs at the time the snapshot was
  87. // taken.
  88. func (h *HistogramSnapshot) StdDev() float64 { return math.Sqrt(h.variance) }
  89. // Update panics.
  90. func (*HistogramSnapshot) Update(int64) {
  91. panic("Update called on a HistogramSnapshot")
  92. }
  93. // Variance returns the variance of inputs at the time the snapshot was taken.
  94. func (h *HistogramSnapshot) Variance() float64 { return h.variance }
  95. // NilHistogram is a no-op Histogram.
  96. type NilHistogram struct{}
  97. // Clear is a no-op.
  98. func (NilHistogram) Clear() {}
  99. // Count is a no-op.
  100. func (NilHistogram) Count() int64 { return 0 }
  101. // Max is a no-op.
  102. func (NilHistogram) Max() int64 { return 0 }
  103. // Mean is a no-op.
  104. func (NilHistogram) Mean() float64 { return 0.0 }
  105. // Min is a no-op.
  106. func (NilHistogram) Min() int64 { return 0 }
  107. // Percentile is a no-op.
  108. func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
  109. // Percentiles is a no-op.
  110. func (NilHistogram) Percentiles(ps []float64) []float64 {
  111. return make([]float64, len(ps))
  112. }
  113. // Sample is a no-op.
  114. func (NilHistogram) Sample() Sample { return NilSample{} }
  115. // Snapshot is a no-op.
  116. func (NilHistogram) Snapshot() Histogram { return NilHistogram{} }
  117. // StdDev is a no-op.
  118. func (NilHistogram) StdDev() float64 { return 0.0 }
  119. // Update is a no-op.
  120. func (NilHistogram) Update(v int64) {}
  121. // Variance is a no-op.
  122. func (NilHistogram) Variance() float64 { return 0.0 }
  123. // StandardHistogram is the standard implementation of a Histogram and uses a
  124. // Sample to bound its memory use.
  125. type StandardHistogram struct {
  126. count, max, min, sum int64
  127. mutex sync.Mutex
  128. sample Sample
  129. sampleMean float64
  130. varianceNumerator float64
  131. }
  132. // Clear clears the histogram and its sample.
  133. func (h *StandardHistogram) Clear() {
  134. h.mutex.Lock()
  135. defer h.mutex.Unlock()
  136. h.count = 0
  137. h.max = math.MinInt64
  138. h.min = math.MaxInt64
  139. h.sample.Clear()
  140. h.sum = 0
  141. h.sampleMean = -1.0
  142. h.varianceNumerator = 0.0
  143. }
  144. // Count returns the count of events since the histogram was last cleared.
  145. func (h *StandardHistogram) Count() int64 {
  146. return atomic.LoadInt64(&h.count)
  147. }
  148. // Max returns the maximum value seen since the histogram was last cleared.
  149. func (h *StandardHistogram) Max() int64 {
  150. h.mutex.Lock()
  151. defer h.mutex.Unlock()
  152. if 0 == h.count {
  153. return 0
  154. }
  155. return h.max
  156. }
  157. // Mean returns the mean of all values seen since the histogram was last
  158. // cleared.
  159. func (h *StandardHistogram) Mean() float64 {
  160. h.mutex.Lock()
  161. defer h.mutex.Unlock()
  162. if 0 == h.count {
  163. return 0
  164. }
  165. return float64(h.sum) / float64(h.count)
  166. }
  167. // Min returns the minimum value seen since the histogram was last cleared.
  168. func (h *StandardHistogram) Min() int64 {
  169. h.mutex.Lock()
  170. defer h.mutex.Unlock()
  171. if 0 == h.count {
  172. return 0
  173. }
  174. return h.min
  175. }
  176. // Percentile returns an arbitrary percentile of the values in the sample.
  177. func (h *StandardHistogram) Percentile(p float64) float64 {
  178. return h.sample.Percentile(p)
  179. }
  180. // Percentiles returns a slice of arbitrary percentiles of the values in the
  181. // sample.
  182. func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
  183. return h.sample.Percentiles(ps)
  184. }
  185. // Sample returns a read-only copy of the underlying Sample.
  186. func (h *StandardHistogram) Sample() Sample { return h.sample.Snapshot() }
  187. // Snapshot returns a read-only copy of the histogram.
  188. func (h *StandardHistogram) Snapshot() Histogram {
  189. h.mutex.Lock()
  190. defer h.mutex.Unlock()
  191. return &HistogramSnapshot{
  192. count: h.count,
  193. max: h.max,
  194. min: h.min,
  195. sample: h.sample.Snapshot().(*SampleSnapshot),
  196. sum: h.sum,
  197. variance: h.variance(),
  198. }
  199. }
  200. // StdDev returns the standard deviation of all values seen since the histogram
  201. // was last cleared.
  202. func (h *StandardHistogram) StdDev() float64 {
  203. return math.Sqrt(h.Variance())
  204. }
  205. // Update updates the histogram with a new value.
  206. func (h *StandardHistogram) Update(v int64) {
  207. h.mutex.Lock()
  208. defer h.mutex.Unlock()
  209. h.sample.Update(v)
  210. h.count++
  211. if v < h.min {
  212. h.min = v
  213. }
  214. if v > h.max {
  215. h.max = v
  216. }
  217. h.sum += v
  218. fv := float64(v)
  219. if -1.0 == h.sampleMean {
  220. h.sampleMean = fv
  221. h.varianceNumerator = 0.0
  222. } else {
  223. sampleMean := h.sampleMean
  224. varianceNumerator := h.varianceNumerator
  225. h.sampleMean = sampleMean + (fv-sampleMean)/float64(h.count)
  226. h.varianceNumerator = varianceNumerator + (fv-sampleMean)*(fv-h.sampleMean)
  227. }
  228. }
  229. // Variance returns the variance of all values seen since the histogram was
  230. // last cleared.
  231. func (h *StandardHistogram) Variance() float64 {
  232. h.mutex.Lock()
  233. defer h.mutex.Unlock()
  234. return h.variance()
  235. }
  236. // variance returns the variance of all the values in the sample but expects
  237. // the lock to already be held.
  238. func (h *StandardHistogram) variance() float64 {
  239. if 1 >= h.count {
  240. return 0.0
  241. }
  242. return h.varianceNumerator / float64(h.count-1)
  243. }
  244. type int64Slice []int64
  245. func (p int64Slice) Len() int { return len(p) }
  246. func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  247. func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }