histogram.go 6.1 KB

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