histogram.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package metrics
  2. import (
  3. "math"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. // Histograms calculate distribution statistics from an int64 value.
  8. //
  9. // This is an interface so as to encourage other structs to implement
  10. // the Histogram API as appropriate.
  11. type Histogram interface {
  12. Clear()
  13. Count() int64
  14. Max() int64
  15. Mean() float64
  16. Min() int64
  17. Percentile(float64) float64
  18. Percentiles([]float64) []float64
  19. Sample() Sample
  20. StdDev() float64
  21. Sum() int64
  22. Update(int64)
  23. Variance() float64
  24. }
  25. // Get an existing or create and register a new Histogram.
  26. func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
  27. if nil == r {
  28. r = DefaultRegistry
  29. }
  30. return r.GetOrRegister(name, NewHistogram(s)).(Histogram)
  31. }
  32. // Create a new Histogram with the given Sample. The initial values compare
  33. // so that the first value will be both min and max and the variance is flagged
  34. // for special treatment on its first iteration.
  35. func NewHistogram(s Sample) Histogram {
  36. if UseNilMetrics {
  37. return NilHistogram{}
  38. }
  39. return &StandardHistogram{
  40. max: math.MinInt64,
  41. min: math.MaxInt64,
  42. s: s,
  43. variance: [2]float64{-1.0, 0.0},
  44. }
  45. }
  46. // Create and register a new Histogram.
  47. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
  48. c := NewHistogram(s)
  49. if nil == r {
  50. r = DefaultRegistry
  51. }
  52. r.Register(name, c)
  53. return c
  54. }
  55. // No-op Histogram.
  56. type NilHistogram struct{}
  57. // No-op.
  58. func (NilHistogram) Clear() {}
  59. // No-op.
  60. func (NilHistogram) Count() int64 { return 0 }
  61. // No-op.
  62. func (NilHistogram) Max() int64 { return 0 }
  63. // No-op.
  64. func (NilHistogram) Mean() float64 { return 0.0 }
  65. // No-op.
  66. func (NilHistogram) Min() int64 { return 0 }
  67. // No-op.
  68. func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
  69. // No-op.
  70. func (NilHistogram) Percentiles(ps []float64) []float64 {
  71. return make([]float64, len(ps))
  72. }
  73. // No-op.
  74. func (NilHistogram) Sample() Sample { return NilSample{} }
  75. // No-op.
  76. func (NilHistogram) StdDev() float64 { return 0.0 }
  77. // No-op.
  78. func (NilHistogram) Sum() int64 { return 0 }
  79. // No-op.
  80. func (NilHistogram) Update(v int64) {}
  81. // No-op.
  82. func (NilHistogram) Variance() float64 { return 0.0 }
  83. // The standard implementation of a Histogram uses a Sample and a goroutine
  84. // to synchronize its calculations.
  85. type StandardHistogram struct {
  86. count, sum, min, max int64
  87. mutex sync.Mutex
  88. s Sample
  89. variance [2]float64
  90. }
  91. // Clear the histogram.
  92. func (h *StandardHistogram) Clear() {
  93. h.mutex.Lock()
  94. defer h.mutex.Unlock()
  95. h.count = 0
  96. h.max = math.MinInt64
  97. h.min = math.MaxInt64
  98. h.s.Clear()
  99. h.sum = 0
  100. h.variance = [2]float64{-1.0, 0.0}
  101. }
  102. // Return the count of inputs since the histogram was last cleared.
  103. func (h *StandardHistogram) Count() int64 {
  104. return atomic.LoadInt64(&h.count)
  105. }
  106. // Return the maximal value seen since the histogram was last cleared.
  107. func (h *StandardHistogram) Max() int64 {
  108. h.mutex.Lock()
  109. defer h.mutex.Unlock()
  110. if 0 == h.count {
  111. return 0
  112. }
  113. return h.max
  114. }
  115. // Return the mean of all values seen since the histogram was last cleared.
  116. func (h *StandardHistogram) Mean() float64 {
  117. h.mutex.Lock()
  118. defer h.mutex.Unlock()
  119. if 0 == h.count {
  120. return 0
  121. }
  122. return float64(h.sum) / float64(h.count)
  123. }
  124. // Return the minimal value seen since the histogram was last cleared.
  125. func (h *StandardHistogram) Min() int64 {
  126. h.mutex.Lock()
  127. defer h.mutex.Unlock()
  128. if 0 == h.count {
  129. return 0
  130. }
  131. return h.min
  132. }
  133. // Percentile returns an arbitrary percentile of sampled values.
  134. func (h *StandardHistogram) Percentile(p float64) float64 {
  135. return h.s.Percentile(p)
  136. }
  137. // Percentiles returns a slice of arbitrary percentiles of sampled values.
  138. func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
  139. return h.s.Percentiles(ps)
  140. }
  141. // Sample returns a copy of the Sample underlying the Histogram.
  142. func (h *StandardHistogram) Sample() Sample {
  143. return h.s.Dup()
  144. }
  145. // Return the standard deviation of all values seen since the histogram was
  146. // last cleared.
  147. func (h *StandardHistogram) StdDev() float64 {
  148. return math.Sqrt(h.Variance())
  149. }
  150. // Return the sum of inputs since the histogram was last cleared.
  151. func (h *StandardHistogram) Sum() int64 {
  152. return atomic.LoadInt64(&h.sum)
  153. }
  154. // Update the histogram with a new value.
  155. func (h *StandardHistogram) Update(v int64) {
  156. h.mutex.Lock()
  157. defer h.mutex.Unlock()
  158. h.s.Update(v)
  159. h.count++
  160. if v < h.min {
  161. h.min = v
  162. }
  163. if v > h.max {
  164. h.max = v
  165. }
  166. h.sum += v
  167. fv := float64(v)
  168. if -1.0 == h.variance[0] {
  169. h.variance[0] = fv
  170. h.variance[1] = 0.0
  171. } else {
  172. m := h.variance[0]
  173. s := h.variance[1]
  174. h.variance[0] = m + (fv-m)/float64(h.count)
  175. h.variance[1] = s + (fv-m)*(fv-h.variance[0])
  176. }
  177. }
  178. // Return the variance of all values seen since the histogram was last cleared.
  179. func (h *StandardHistogram) Variance() float64 {
  180. h.mutex.Lock()
  181. defer h.mutex.Unlock()
  182. if 1 >= h.count {
  183. return 0.0
  184. }
  185. return h.variance[1] / float64(h.count-1)
  186. }
  187. // Cribbed from the standard library's `sort` package.
  188. type int64Slice []int64
  189. func (p int64Slice) Len() int { return len(p) }
  190. func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  191. func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }