histogram.go 5.6 KB

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