histogram.go 5.2 KB

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