histogram.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Create and register a new Histogram.
  39. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
  40. c := NewHistogram(s)
  41. if nil == r {
  42. r = DefaultRegistry
  43. }
  44. r.Register(name, c)
  45. return c
  46. }
  47. // No-op Histogram.
  48. type NilHistogram struct{}
  49. // No-op.
  50. func (h NilHistogram) Clear() {}
  51. // No-op.
  52. func (h NilHistogram) Count() int64 { return 0 }
  53. // No-op.
  54. func (h NilHistogram) Max() int64 { return 0 }
  55. // No-op.
  56. func (h NilHistogram) Mean() float64 { return 0.0 }
  57. // No-op.
  58. func (h NilHistogram) Min() int64 { return 0 }
  59. // No-op.
  60. func (h NilHistogram) Percentile(p float64) float64 { return 0.0 }
  61. // No-op.
  62. func (h NilHistogram) Percentiles(ps []float64) []float64 {
  63. return make([]float64, len(ps))
  64. }
  65. // No-op.
  66. func (h NilHistogram) StdDev() float64 { return 0.0 }
  67. // No-op.
  68. func (h NilHistogram) Update(v int64) {}
  69. // No-op.
  70. func (h NilHistogram) Variance() float64 { return 0.0 }
  71. // The standard implementation of a Histogram uses a Sample and a goroutine
  72. // to synchronize its calculations.
  73. type StandardHistogram struct {
  74. count, sum, min, max int64
  75. mutex sync.Mutex
  76. s Sample
  77. variance [2]float64
  78. }
  79. // Clear the histogram.
  80. func (h *StandardHistogram) Clear() {
  81. h.mutex.Lock()
  82. defer h.mutex.Unlock()
  83. h.count = 0
  84. h.max = math.MinInt64
  85. h.min = math.MaxInt64
  86. h.s.Clear()
  87. h.sum = 0
  88. h.variance = [...]float64{-1.0, 0.0}
  89. }
  90. // Return the count of inputs since the histogram was last cleared.
  91. func (h *StandardHistogram) Count() int64 {
  92. return atomic.LoadInt64(&h.count)
  93. }
  94. // Return the maximal value seen since the histogram was last cleared.
  95. func (h *StandardHistogram) Max() int64 {
  96. h.mutex.Lock()
  97. defer h.mutex.Unlock()
  98. if 0 == h.count {
  99. return 0
  100. }
  101. return h.max
  102. }
  103. // Return the mean of all values seen since the histogram was last cleared.
  104. func (h *StandardHistogram) Mean() float64 {
  105. h.mutex.Lock()
  106. defer h.mutex.Unlock()
  107. if 0 == h.count {
  108. return 0
  109. }
  110. return float64(h.sum) / float64(h.count)
  111. }
  112. // Return the minimal value seen since the histogram was last cleared.
  113. func (h *StandardHistogram) Min() int64 {
  114. h.mutex.Lock()
  115. defer h.mutex.Unlock()
  116. if 0 == h.count {
  117. return 0
  118. }
  119. return h.min
  120. }
  121. // Return an arbitrary percentile of all values seen since the histogram was
  122. // last cleared.
  123. func (h *StandardHistogram) Percentile(p float64) float64 {
  124. return h.Percentiles([]float64{p})[0]
  125. }
  126. // Return a slice of arbitrary percentiles of all values seen since the
  127. // histogram was last cleared.
  128. func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
  129. scores := make([]float64, len(ps))
  130. values := int64Slice(h.s.Values())
  131. size := len(values)
  132. if size > 0 {
  133. sort.Sort(values)
  134. for i, p := range ps {
  135. pos := p * float64(size+1)
  136. if pos < 1.0 {
  137. scores[i] = float64(values[0])
  138. } else if pos >= float64(size) {
  139. scores[i] = float64(values[size-1])
  140. } else {
  141. lower := float64(values[int(pos)-1])
  142. upper := float64(values[int(pos)])
  143. scores[i] = lower + (pos-math.Floor(pos))*(upper-lower)
  144. }
  145. }
  146. }
  147. return scores
  148. }
  149. // Return the standard deviation of all values seen since the histogram was
  150. // last cleared.
  151. func (h *StandardHistogram) StdDev() float64 {
  152. return math.Sqrt(h.Variance())
  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] }