histogram.go 4.8 KB

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