github.com/dominant-strategies/go-quai@v0.28.2/metrics/histogram.go (about)

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