github.com/codingfuture/orig-energi3@v0.8.4/metrics/histogram.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  // Histograms calculate distribution statistics from a series of int64 values.
    21  type Histogram interface {
    22  	Clear()
    23  	Count() int64
    24  	Max() int64
    25  	Mean() float64
    26  	Min() int64
    27  	Percentile(float64) float64
    28  	Percentiles([]float64) []float64
    29  	Sample() Sample
    30  	Snapshot() Histogram
    31  	StdDev() float64
    32  	Sum() int64
    33  	Update(int64)
    34  	Variance() float64
    35  }
    36  
    37  // GetOrRegisterHistogram returns an existing Histogram or constructs and
    38  // registers a new StandardHistogram.
    39  func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
    40  	if nil == r {
    41  		r = DefaultRegistry
    42  	}
    43  	return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
    44  }
    45  
    46  // NewHistogram constructs a new StandardHistogram from a Sample.
    47  func NewHistogram(s Sample) Histogram {
    48  	if !Enabled {
    49  		return NilHistogram{}
    50  	}
    51  	return &StandardHistogram{sample: s}
    52  }
    53  
    54  // NewRegisteredHistogram constructs and registers a new StandardHistogram from
    55  // a Sample.
    56  func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
    57  	c := NewHistogram(s)
    58  	if nil == r {
    59  		r = DefaultRegistry
    60  	}
    61  	r.Register(name, c)
    62  	return c
    63  }
    64  
    65  // HistogramSnapshot is a read-only copy of another Histogram.
    66  type HistogramSnapshot struct {
    67  	sample *SampleSnapshot
    68  }
    69  
    70  // Clear panics.
    71  func (*HistogramSnapshot) Clear() {
    72  	panic("Clear called on a HistogramSnapshot")
    73  }
    74  
    75  // Count returns the number of samples recorded at the time the snapshot was
    76  // taken.
    77  func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() }
    78  
    79  // Max returns the maximum value in the sample at the time the snapshot was
    80  // taken.
    81  func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() }
    82  
    83  // Mean returns the mean of the values in the sample at the time the snapshot
    84  // was taken.
    85  func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() }
    86  
    87  // Min returns the minimum value in the sample at the time the snapshot was
    88  // taken.
    89  func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() }
    90  
    91  // Percentile returns an arbitrary percentile of values in the sample at the
    92  // time the snapshot was taken.
    93  func (h *HistogramSnapshot) Percentile(p float64) float64 {
    94  	return h.sample.Percentile(p)
    95  }
    96  
    97  // Percentiles returns a slice of arbitrary percentiles of values in the sample
    98  // at the time the snapshot was taken.
    99  func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 {
   100  	return h.sample.Percentiles(ps)
   101  }
   102  
   103  // Sample returns the Sample underlying the histogram.
   104  func (h *HistogramSnapshot) Sample() Sample { return h.sample }
   105  
   106  // Snapshot returns the snapshot.
   107  func (h *HistogramSnapshot) Snapshot() Histogram { return h }
   108  
   109  // StdDev returns the standard deviation of the values in the sample at the
   110  // time the snapshot was taken.
   111  func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() }
   112  
   113  // Sum returns the sum in the sample at the time the snapshot was taken.
   114  func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() }
   115  
   116  // Update panics.
   117  func (*HistogramSnapshot) Update(int64) {
   118  	panic("Update called on a HistogramSnapshot")
   119  }
   120  
   121  // Variance returns the variance of inputs at the time the snapshot was taken.
   122  func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() }
   123  
   124  // NilHistogram is a no-op Histogram.
   125  type NilHistogram struct{}
   126  
   127  // Clear is a no-op.
   128  func (NilHistogram) Clear() {}
   129  
   130  // Count is a no-op.
   131  func (NilHistogram) Count() int64 { return 0 }
   132  
   133  // Max is a no-op.
   134  func (NilHistogram) Max() int64 { return 0 }
   135  
   136  // Mean is a no-op.
   137  func (NilHistogram) Mean() float64 { return 0.0 }
   138  
   139  // Min is a no-op.
   140  func (NilHistogram) Min() int64 { return 0 }
   141  
   142  // Percentile is a no-op.
   143  func (NilHistogram) Percentile(p float64) float64 { return 0.0 }
   144  
   145  // Percentiles is a no-op.
   146  func (NilHistogram) Percentiles(ps []float64) []float64 {
   147  	return make([]float64, len(ps))
   148  }
   149  
   150  // Sample is a no-op.
   151  func (NilHistogram) Sample() Sample { return NilSample{} }
   152  
   153  // Snapshot is a no-op.
   154  func (NilHistogram) Snapshot() Histogram { return NilHistogram{} }
   155  
   156  // StdDev is a no-op.
   157  func (NilHistogram) StdDev() float64 { return 0.0 }
   158  
   159  // Sum is a no-op.
   160  func (NilHistogram) Sum() int64 { return 0 }
   161  
   162  // Update is a no-op.
   163  func (NilHistogram) Update(v int64) {}
   164  
   165  // Variance is a no-op.
   166  func (NilHistogram) Variance() float64 { return 0.0 }
   167  
   168  // StandardHistogram is the standard implementation of a Histogram and uses a
   169  // Sample to bound its memory use.
   170  type StandardHistogram struct {
   171  	sample Sample
   172  }
   173  
   174  // Clear clears the histogram and its sample.
   175  func (h *StandardHistogram) Clear() { h.sample.Clear() }
   176  
   177  // Count returns the number of samples recorded since the histogram was last
   178  // cleared.
   179  func (h *StandardHistogram) Count() int64 { return h.sample.Count() }
   180  
   181  // Max returns the maximum value in the sample.
   182  func (h *StandardHistogram) Max() int64 { return h.sample.Max() }
   183  
   184  // Mean returns the mean of the values in the sample.
   185  func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() }
   186  
   187  // Min returns the minimum value in the sample.
   188  func (h *StandardHistogram) Min() int64 { return h.sample.Min() }
   189  
   190  // Percentile returns an arbitrary percentile of the values in the sample.
   191  func (h *StandardHistogram) Percentile(p float64) float64 {
   192  	return h.sample.Percentile(p)
   193  }
   194  
   195  // Percentiles returns a slice of arbitrary percentiles of the values in the
   196  // sample.
   197  func (h *StandardHistogram) Percentiles(ps []float64) []float64 {
   198  	return h.sample.Percentiles(ps)
   199  }
   200  
   201  // Sample returns the Sample underlying the histogram.
   202  func (h *StandardHistogram) Sample() Sample { return h.sample }
   203  
   204  // Snapshot returns a read-only copy of the histogram.
   205  func (h *StandardHistogram) Snapshot() Histogram {
   206  	return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)}
   207  }
   208  
   209  // StdDev returns the standard deviation of the values in the sample.
   210  func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() }
   211  
   212  // Sum returns the sum in the sample.
   213  func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() }
   214  
   215  // Update samples a new value.
   216  func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) }
   217  
   218  // Variance returns the variance of the values in the sample.
   219  func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() }