histogram.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package metrics
  2. // Histograms calculate distribution statistics from a series of int64 values.
  3. type Histogram interface {
  4. Clear()
  5. Count() int64
  6. Max() int64
  7. Mean() float64
  8. Min() int64
  9. Percentile(float64) float64
  10. Percentiles([]float64) []float64
  11. Sample() Sample
  12. Snapshot() Histogram
  13. StdDev() float64
  14. Sum() int64
  15. Update(int64)
  16. Variance() float64
  17. }
  18. // GetOrRegisterHistogram returns an existing Histogram or constructs and
  19. // registers a new StandardHistogram.
  20. func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
  21. if nil == r {
  22. r = DefaultRegistry
  23. }
  24. return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
  25. }
  26. // GetOrRegisterHistogramLazy returns an existing Histogram or constructs and
  27. // registers a new StandardHistogram.
  28. func GetOrRegisterHistogramLazy(name string, r Registry, s func() Sample) Histogram {
  29. if nil == r {
  30. r = DefaultRegistry
  31. }
  32. return r.GetOrRegister(name, func() Histogram { return NewHistogram(s()) }).(Histogram)
  33. }
  34. // NewHistogram constructs a new StandardHistogram from a Sample.
  35. func NewHistogram(s Sample) Histogram {
  36. if !Enabled {
  37. return NilHistogram{}
  38. }
  39. return &StandardHistogram{sample: s}
  40. }
  41. // NewRegisteredHistogram constructs and registers a new StandardHistogram from
  42. // a Sample.
  43. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
  44. c := NewHistogram(s)
  45. if nil == r {
  46. r = DefaultRegistry
  47. }
  48. r.Register(name, c)
  49. return c
  50. }
  51. // HistogramSnapshot is a read-only copy of another Histogram.
  52. type HistogramSnapshot struct {
  53. sample *SampleSnapshot
  54. }
  55. // Clear panics.
  56. func (*HistogramSnapshot) Clear() {
  57. panic("Clear called on a HistogramSnapshot")
  58. }
  59. // Count returns the number of samples recorded at the time the snapshot was
  60. // taken.
  61. func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() }
  62. // Max returns the maximum value in the sample at the time the snapshot was
  63. // taken.
  64. func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() }
  65. // Mean returns the mean of the values in the sample at the time the snapshot
  66. // was taken.
  67. func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() }
  68. // Min returns the minimum value in the sample at the time the snapshot was
  69. // taken.
  70. func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() }
  71. // Percentile returns an arbitrary percentile of values in the sample at the
  72. // time the snapshot was taken.
  73. func (h *HistogramSnapshot) Percentile(p float64) float64 {
  74. return h.sample.Percentile(p)
  75. }
  76. // Percentiles returns a slice of arbitrary percentiles of values in the sample
  77. // at the time the snapshot was taken.
  78. func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 {
  79. return h.sample.Percentiles(ps)
  80. }
  81. // Sample returns the Sample underlying the histogram.
  82. func (h *HistogramSnapshot) Sample() Sample { return h.sample }
  83. // Snapshot returns the snapshot.
  84. func (h *HistogramSnapshot) Snapshot() Histogram { return h }
  85. // StdDev returns the standard deviation of the values in the sample at the
  86. // time the snapshot was taken.
  87. func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() }
  88. // Sum returns the sum in the sample at the time the snapshot was taken.
  89. func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() }
  90. // Update panics.
  91. func (*HistogramSnapshot) Update(int64) {
  92. panic("Update called on a HistogramSnapshot")
  93. }
  94. // Variance returns the variance of inputs at the time the snapshot was taken.
  95. func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() }
  96. // NilHistogram is a no-op Histogram.
  97. type NilHistogram struct{}
  98. // Clear is a no-op.
  99. func (NilHistogram) Clear() {}
  100. // Count is a no-op.
  101. func (NilHistogram) Count() int64 { return 0 }
  102. // Max is a no-op.
  103. func (NilHistogram) Max() int64 { return 0 }
  104. // Mean is a no-op.
  105. func (NilHistogram) Mean() float64 { return 0.0 }
  106. // Min is a no-op.
  107. func (NilHistogram) Min() int64 { return 0 }
  108. // Percentile is a no-op.
  109. func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
  110. // Percentiles is a no-op.
  111. func (NilHistogram) Percentiles(ps []float64) []float64 {
  112. return make([]float64, len(ps))
  113. }
  114. // Sample is a no-op.
  115. func (NilHistogram) Sample() Sample { return NilSample{} }
  116. // Snapshot is a no-op.
  117. func (NilHistogram) Snapshot() Histogram { return NilHistogram{} }
  118. // StdDev is a no-op.
  119. func (NilHistogram) StdDev() float64 { return 0.0 }
  120. // Sum is a no-op.
  121. func (NilHistogram) Sum() int64 { return 0 }
  122. // Update is a no-op.
  123. func (NilHistogram) Update(v int64) {}
  124. // Variance is a no-op.
  125. func (NilHistogram) Variance() float64 { return 0.0 }
  126. // StandardHistogram is the standard implementation of a Histogram and uses a
  127. // Sample to bound its memory use.
  128. type StandardHistogram struct {
  129. sample Sample
  130. }
  131. // Clear clears the histogram and its sample.
  132. func (h *StandardHistogram) Clear() { h.sample.Clear() }
  133. // Count returns the number of samples recorded since the histogram was last
  134. // cleared.
  135. func (h *StandardHistogram) Count() int64 { return h.sample.Count() }
  136. // Max returns the maximum value in the sample.
  137. func (h *StandardHistogram) Max() int64 { return h.sample.Max() }
  138. // Mean returns the mean of the values in the sample.
  139. func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() }
  140. // Min returns the minimum value in the sample.
  141. func (h *StandardHistogram) Min() int64 { return h.sample.Min() }
  142. // Percentile returns an arbitrary percentile of the values in the sample.
  143. func (h *StandardHistogram) Percentile(p float64) float64 {
  144. return h.sample.Percentile(p)
  145. }
  146. // Percentiles returns a slice of arbitrary percentiles of the values in the
  147. // sample.
  148. func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
  149. return h.sample.Percentiles(ps)
  150. }
  151. // Sample returns the Sample underlying the histogram.
  152. func (h *StandardHistogram) Sample() Sample { return h.sample }
  153. // Snapshot returns a read-only copy of the histogram.
  154. func (h *StandardHistogram) Snapshot() Histogram {
  155. return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)}
  156. }
  157. // StdDev returns the standard deviation of the values in the sample.
  158. func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() }
  159. // Sum returns the sum in the sample.
  160. func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() }
  161. // Update samples a new value.
  162. func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) }
  163. // Variance returns the variance of the values in the sample.
  164. func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() }