github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/metrics/histogram.go (about) 1 package metrics 2 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 19 func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram { 20 if nil == r { 21 r = DefaultRegistry 22 } 23 return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram) 24 } 25 26 func NewHistogram(s Sample) Histogram { 27 if !Enabled { 28 return NilHistogram{} 29 } 30 return &StandardHistogram{sample: s} 31 } 32 33 func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram { 34 c := NewHistogram(s) 35 if nil == r { 36 r = DefaultRegistry 37 } 38 r.Register(name, c) 39 return c 40 } 41 42 type HistogramSnapshot struct { 43 sample *SampleSnapshot 44 } 45 46 func (*HistogramSnapshot) Clear() { 47 panic("Clear called on a HistogramSnapshot") 48 } 49 50 func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() } 51 52 func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() } 53 54 func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() } 55 56 func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() } 57 58 func (h *HistogramSnapshot) Percentile(p float64) float64 { 59 return h.sample.Percentile(p) 60 } 61 62 func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 { 63 return h.sample.Percentiles(ps) 64 } 65 66 func (h *HistogramSnapshot) Sample() Sample { return h.sample } 67 68 func (h *HistogramSnapshot) Snapshot() Histogram { return h } 69 70 func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() } 71 72 func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() } 73 74 func (*HistogramSnapshot) Update(int64) { 75 panic("Update called on a HistogramSnapshot") 76 } 77 78 func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() } 79 80 type NilHistogram struct{} 81 82 func (NilHistogram) Clear() {} 83 84 func (NilHistogram) Count() int64 { return 0 } 85 86 func (NilHistogram) Max() int64 { return 0 } 87 88 func (NilHistogram) Mean() float64 { return 0.0 } 89 90 func (NilHistogram) Min() int64 { return 0 } 91 92 func (NilHistogram) Percentile(p float64) float64 { return 0.0 } 93 94 func (NilHistogram) Percentiles(ps []float64) []float64 { 95 return make([]float64, len(ps)) 96 } 97 98 func (NilHistogram) Sample() Sample { return NilSample{} } 99 100 func (NilHistogram) Snapshot() Histogram { return NilHistogram{} } 101 102 func (NilHistogram) StdDev() float64 { return 0.0 } 103 104 func (NilHistogram) Sum() int64 { return 0 } 105 106 func (NilHistogram) Update(v int64) {} 107 108 func (NilHistogram) Variance() float64 { return 0.0 } 109 110 type StandardHistogram struct { 111 sample Sample 112 } 113 114 func (h *StandardHistogram) Clear() { h.sample.Clear() } 115 116 func (h *StandardHistogram) Count() int64 { return h.sample.Count() } 117 118 func (h *StandardHistogram) Max() int64 { return h.sample.Max() } 119 120 func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() } 121 122 func (h *StandardHistogram) Min() int64 { return h.sample.Min() } 123 124 func (h *StandardHistogram) Percentile(p float64) float64 { 125 return h.sample.Percentile(p) 126 } 127 128 func (h *StandardHistogram) Percentiles(ps []float64) []float64 { 129 return h.sample.Percentiles(ps) 130 } 131 132 func (h *StandardHistogram) Sample() Sample { return h.sample } 133 134 func (h *StandardHistogram) Snapshot() Histogram { 135 return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)} 136 } 137 138 func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() } 139 140 func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() } 141 142 func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) } 143 144 func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() }