github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/metrics/histogram.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:40</date> 10 //</624450098964729856> 11 12 package metrics 13 14 //柱状图根据一系列Int64值计算分布统计信息。 15 type Histogram interface { 16 Clear() 17 Count() int64 18 Max() int64 19 Mean() float64 20 Min() int64 21 Percentile(float64) float64 22 Percentiles([]float64) []float64 23 Sample() Sample 24 Snapshot() Histogram 25 StdDev() float64 26 Sum() int64 27 Update(int64) 28 Variance() float64 29 } 30 31 //GetOrRegisterHistogram返回现有的柱状图或构造,以及 32 //注册新的标准柱状图。 33 func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram { 34 if nil == r { 35 r = DefaultRegistry 36 } 37 return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram) 38 } 39 40 //NewHistogram从一个样本构造一个新的标准柱状图。 41 func NewHistogram(s Sample) Histogram { 42 if !Enabled { 43 return NilHistogram{} 44 } 45 return &StandardHistogram{sample: s} 46 } 47 48 //NewRegisteredHistogram构造并注册来自 49 //样品。 50 func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram { 51 c := NewHistogram(s) 52 if nil == r { 53 r = DefaultRegistry 54 } 55 r.Register(name, c) 56 return c 57 } 58 59 //HistogramSnapshot是另一个柱状图的只读副本。 60 type HistogramSnapshot struct { 61 sample *SampleSnapshot 62 } 63 64 //清晰的恐慌。 65 func (*HistogramSnapshot) Clear() { 66 panic("Clear called on a HistogramSnapshot") 67 } 68 69 //count返回快照时记录的样本数 70 //拿。 71 func (h *HistogramSnapshot) Count() int64 { return h.sample.Count() } 72 73 //max返回快照时样本中的最大值。 74 //拿。 75 func (h *HistogramSnapshot) Max() int64 { return h.sample.Max() } 76 77 //mean返回快照时样本中值的平均值 78 //被带走了。 79 func (h *HistogramSnapshot) Mean() float64 { return h.sample.Mean() } 80 81 //Min返回快照时样本中的最小值 82 //拿。 83 func (h *HistogramSnapshot) Min() int64 { return h.sample.Min() } 84 85 //Percentile返回在 86 //拍摄快照的时间。 87 func (h *HistogramSnapshot) Percentile(p float64) float64 { 88 return h.sample.Percentile(p) 89 } 90 91 //Percentiles返回样本中任意百分位值的切片 92 //拍摄快照时。 93 func (h *HistogramSnapshot) Percentiles(ps []float64) []float64 { 94 return h.sample.Percentiles(ps) 95 } 96 97 //sample返回柱状图下的样本。 98 func (h *HistogramSnapshot) Sample() Sample { return h.sample } 99 100 //快照返回快照。 101 func (h *HistogramSnapshot) Snapshot() Histogram { return h } 102 103 //stdev返回在 104 //拍摄快照的时间。 105 func (h *HistogramSnapshot) StdDev() float64 { return h.sample.StdDev() } 106 107 //sum返回快照拍摄时样本中的总和。 108 func (h *HistogramSnapshot) Sum() int64 { return h.sample.Sum() } 109 110 //更新恐慌。 111 func (*HistogramSnapshot) Update(int64) { 112 panic("Update called on a HistogramSnapshot") 113 } 114 115 //variance返回拍摄快照时输入的方差。 116 func (h *HistogramSnapshot) Variance() float64 { return h.sample.Variance() } 117 118 //nilHistogram是一个无操作的柱状图。 119 type NilHistogram struct{} 120 121 //清除是不可操作的。 122 func (NilHistogram) Clear() {} 123 124 //计数是不允许的。 125 func (NilHistogram) Count() int64 { return 0 } 126 127 //马克斯不是一个OP。 128 func (NilHistogram) Max() int64 { return 0 } 129 130 //平均值是不允许的。 131 func (NilHistogram) Mean() float64 { return 0.0 } 132 133 //min是NO-OP。 134 func (NilHistogram) Min() int64 { return 0 } 135 136 //百分位数是不允许的。 137 func (NilHistogram) Percentile(p float64) float64 { return 0.0 } 138 139 //百分位数是不允许的。 140 func (NilHistogram) Percentiles(ps []float64) []float64 { 141 return make([]float64, len(ps)) 142 } 143 144 //样本是不可操作的。 145 func (NilHistogram) Sample() Sample { return NilSample{} } 146 147 //快照是不可操作的。 148 func (NilHistogram) Snapshot() Histogram { return NilHistogram{} } 149 150 //stdev是一个no-op。 151 func (NilHistogram) StdDev() float64 { return 0.0 } 152 153 //和是一个NO-op. 154 func (NilHistogram) Sum() int64 { return 0 } 155 156 //更新是不可操作的。 157 func (NilHistogram) Update(v int64) {} 158 159 //方差是不可操作的。 160 func (NilHistogram) Variance() float64 { return 0.0 } 161 162 //标准柱状图是柱状图的标准实现,使用 163 //用于绑定其内存使用的示例。 164 type StandardHistogram struct { 165 sample Sample 166 } 167 168 //清除清除柱状图及其样本。 169 func (h *StandardHistogram) Clear() { h.sample.Clear() } 170 171 //count返回自上次直方图以来记录的样本数 172 //变明朗。 173 func (h *StandardHistogram) Count() int64 { return h.sample.Count() } 174 175 //max返回样本中的最大值。 176 func (h *StandardHistogram) Max() int64 { return h.sample.Max() } 177 178 //mean返回样本值的平均值。 179 func (h *StandardHistogram) Mean() float64 { return h.sample.Mean() } 180 181 //Min返回样本中的最小值。 182 func (h *StandardHistogram) Min() int64 { return h.sample.Min() } 183 184 //Percentile返回样本中任意百分位数的值。 185 func (h *StandardHistogram) Percentile(p float64) float64 { 186 return h.sample.Percentile(p) 187 } 188 189 //Percentiles返回 190 //样品。 191 func (h *StandardHistogram) Percentiles(ps []float64) []float64 { 192 return h.sample.Percentiles(ps) 193 } 194 195 //sample返回柱状图下的样本。 196 func (h *StandardHistogram) Sample() Sample { return h.sample } 197 198 //快照返回柱状图的只读副本。 199 func (h *StandardHistogram) Snapshot() Histogram { 200 return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)} 201 } 202 203 //stdev返回样本值的标准偏差。 204 func (h *StandardHistogram) StdDev() float64 { return h.sample.StdDev() } 205 206 //sum返回样本中的和。 207 func (h *StandardHistogram) Sum() int64 { return h.sample.Sum() } 208 209 //更新示例新值。 210 func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) } 211 212 //方差返回样本中值的方差。 213 func (h *StandardHistogram) Variance() float64 { return h.sample.Variance() } 214