github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/metrics/timer.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 12:09:42</date> 10 //</624342651541393408> 11 12 package metrics 13 14 import ( 15 "sync" 16 "time" 17 ) 18 19 //计时器捕获事件的持续时间和速率。 20 type Timer interface { 21 Count() int64 22 Max() int64 23 Mean() float64 24 Min() int64 25 Percentile(float64) float64 26 Percentiles([]float64) []float64 27 Rate1() float64 28 Rate5() float64 29 Rate15() float64 30 RateMean() float64 31 Snapshot() Timer 32 StdDev() float64 33 Stop() 34 Sum() int64 35 Time(func()) 36 Update(time.Duration) 37 UpdateSince(time.Time) 38 Variance() float64 39 } 40 41 //GetOrRegisterTimer返回现有计时器或构造并注册 42 //新标准计时器。 43 //一旦没有必要从注册表中注销仪表 44 //允许垃圾收集。 45 func GetOrRegisterTimer(name string, r Registry) Timer { 46 if nil == r { 47 r = DefaultRegistry 48 } 49 return r.GetOrRegister(name, NewTimer).(Timer) 50 } 51 52 //NewCustomTimer从柱状图和仪表构造新的StandardTimer。 53 //确保在计时器不允许垃圾收集时调用stop()。 54 func NewCustomTimer(h Histogram, m Meter) Timer { 55 if !Enabled { 56 return NilTimer{} 57 } 58 return &StandardTimer{ 59 histogram: h, 60 meter: m, 61 } 62 } 63 64 //NewRegisteredTimer构造并注册新的StandardTimer。 65 //一旦没有必要从注册表中注销仪表 66 //允许垃圾收集。 67 func NewRegisteredTimer(name string, r Registry) Timer { 68 c := NewTimer() 69 if nil == r { 70 r = DefaultRegistry 71 } 72 r.Register(name, c) 73 return c 74 } 75 76 //NewTimer使用指数衰减构造新的StandardTimer 77 //具有与Unix平均负荷相同的水库大小和alpha的样本。 78 //确保在计时器不允许垃圾收集时调用stop()。 79 func NewTimer() Timer { 80 if !Enabled { 81 return NilTimer{} 82 } 83 return &StandardTimer{ 84 histogram: NewHistogram(NewExpDecaySample(1028, 0.015)), 85 meter: NewMeter(), 86 } 87 } 88 89 //niltimer是一个无操作计时器。 90 type NilTimer struct { 91 h Histogram 92 m Meter 93 } 94 95 //计数是不允许的。 96 func (NilTimer) Count() int64 { return 0 } 97 98 //马克斯不是一个OP。 99 func (NilTimer) Max() int64 { return 0 } 100 101 //平均值是不允许的。 102 func (NilTimer) Mean() float64 { return 0.0 } 103 104 //min是NO-OP。 105 func (NilTimer) Min() int64 { return 0 } 106 107 //百分位数是不允许的。 108 func (NilTimer) Percentile(p float64) float64 { return 0.0 } 109 110 //百分位数是不允许的。 111 func (NilTimer) Percentiles(ps []float64) []float64 { 112 return make([]float64, len(ps)) 113 } 114 115 //RATE1是不可操作的。 116 func (NilTimer) Rate1() float64 { return 0.0 } 117 118 //RATE5是不允许的。 119 func (NilTimer) Rate5() float64 { return 0.0 } 120 121 //RATE15是不允许的。 122 func (NilTimer) Rate15() float64 { return 0.0 } 123 124 //RateMean是一个不允许的人。 125 func (NilTimer) RateMean() float64 { return 0.0 } 126 127 //快照是不可操作的。 128 func (NilTimer) Snapshot() Timer { return NilTimer{} } 129 130 //stdev是一个no-op。 131 func (NilTimer) StdDev() float64 { return 0.0 } 132 133 //停止是不允许的。 134 func (NilTimer) Stop() {} 135 136 //和是一个NO-op. 137 func (NilTimer) Sum() int64 { return 0 } 138 139 //时间是不允许的。 140 func (NilTimer) Time(func()) {} 141 142 //更新是不可操作的。 143 func (NilTimer) Update(time.Duration) {} 144 145 //updateSince是一个no-op。 146 func (NilTimer) UpdateSince(time.Time) {} 147 148 //方差是不可操作的。 149 func (NilTimer) Variance() float64 { return 0.0 } 150 151 //StandardTimer是计时器的标准实现,使用柱状图 152 //和米。 153 type StandardTimer struct { 154 histogram Histogram 155 meter Meter 156 mutex sync.Mutex 157 } 158 159 //count返回记录的事件数。 160 func (t *StandardTimer) Count() int64 { 161 return t.histogram.Count() 162 } 163 164 //max返回样本中的最大值。 165 func (t *StandardTimer) Max() int64 { 166 return t.histogram.Max() 167 } 168 169 //mean返回样本值的平均值。 170 func (t *StandardTimer) Mean() float64 { 171 return t.histogram.Mean() 172 } 173 174 //Min返回样本中的最小值。 175 func (t *StandardTimer) Min() int64 { 176 return t.histogram.Min() 177 } 178 179 //Percentile返回样本中任意百分位数的值。 180 func (t *StandardTimer) Percentile(p float64) float64 { 181 return t.histogram.Percentile(p) 182 } 183 184 //Percentiles返回 185 //样品。 186 func (t *StandardTimer) Percentiles(ps []float64) []float64 { 187 return t.histogram.Percentiles(ps) 188 } 189 190 //Rate1返回每秒事件的一分钟移动平均速率。 191 func (t *StandardTimer) Rate1() float64 { 192 return t.meter.Rate1() 193 } 194 195 //Rate5返回每秒事件的5分钟移动平均速率。 196 func (t *StandardTimer) Rate5() float64 { 197 return t.meter.Rate5() 198 } 199 200 //Rate15返回每秒事件的15分钟移动平均速率。 201 func (t *StandardTimer) Rate15() float64 { 202 return t.meter.Rate15() 203 } 204 205 //RateMean返回仪表每秒事件的平均速率。 206 func (t *StandardTimer) RateMean() float64 { 207 return t.meter.RateMean() 208 } 209 210 //快照返回计时器的只读副本。 211 func (t *StandardTimer) Snapshot() Timer { 212 t.mutex.Lock() 213 defer t.mutex.Unlock() 214 return &TimerSnapshot{ 215 histogram: t.histogram.Snapshot().(*HistogramSnapshot), 216 meter: t.meter.Snapshot().(*MeterSnapshot), 217 } 218 } 219 220 //stdev返回样本值的标准偏差。 221 func (t *StandardTimer) StdDev() float64 { 222 return t.histogram.StdDev() 223 } 224 225 //停止停止计时表。 226 func (t *StandardTimer) Stop() { 227 t.meter.Stop() 228 } 229 230 //sum返回样本中的和。 231 func (t *StandardTimer) Sum() int64 { 232 return t.histogram.Sum() 233 } 234 235 //记录给定函数执行的持续时间。 236 func (t *StandardTimer) Time(f func()) { 237 ts := time.Now() 238 f() 239 t.Update(time.Since(ts)) 240 } 241 242 //记录事件的持续时间。 243 func (t *StandardTimer) Update(d time.Duration) { 244 t.mutex.Lock() 245 defer t.mutex.Unlock() 246 t.histogram.Update(int64(d)) 247 t.meter.Mark(1) 248 } 249 250 //记录一次开始到现在结束的事件的持续时间。 251 func (t *StandardTimer) UpdateSince(ts time.Time) { 252 t.mutex.Lock() 253 defer t.mutex.Unlock() 254 t.histogram.Update(int64(time.Since(ts))) 255 t.meter.Mark(1) 256 } 257 258 //方差返回样本中值的方差。 259 func (t *StandardTimer) Variance() float64 { 260 return t.histogram.Variance() 261 } 262 263 //TimersSnapshot是另一个计时器的只读副本。 264 type TimerSnapshot struct { 265 histogram *HistogramSnapshot 266 meter *MeterSnapshot 267 } 268 269 //count返回快照时记录的事件数 270 //拿。 271 func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() } 272 273 //max返回快照拍摄时的最大值。 274 func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() } 275 276 //mean返回拍摄快照时的平均值。 277 func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() } 278 279 //Min返回拍摄快照时的最小值。 280 func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() } 281 282 //percentile返回在 283 //已拍摄快照。 284 func (t *TimerSnapshot) Percentile(p float64) float64 { 285 return t.histogram.Percentile(p) 286 } 287 288 //Percentiles返回在 289 //拍摄快照的时间。 290 func (t *TimerSnapshot) Percentiles(ps []float64) []float64 { 291 return t.histogram.Percentiles(ps) 292 } 293 294 //RATE1返回在 295 //拍摄快照的时间。 296 func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() } 297 298 //RATE5返回每秒5分钟的移动平均事件速率 299 //拍摄快照的时间。 300 func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() } 301 302 //Rate15返回每秒15分钟的事件移动平均速率 303 //拍摄快照时。 304 func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() } 305 306 //RateMean返回在 307 //已拍摄快照。 308 func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() } 309 310 //快照返回快照。 311 func (t *TimerSnapshot) Snapshot() Timer { return t } 312 313 //stdev返回快照时值的标准偏差 314 //被带走了。 315 func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() } 316 317 //停止是不允许的。 318 func (t *TimerSnapshot) Stop() {} 319 320 //sum返回拍摄快照时的总和。 321 func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() } 322 323 //时间恐慌。 324 func (*TimerSnapshot) Time(func()) { 325 panic("Time called on a TimerSnapshot") 326 } 327 328 //更新恐慌。 329 func (*TimerSnapshot) Update(time.Duration) { 330 panic("Update called on a TimerSnapshot") 331 } 332 333 //更新自恐慌。 334 func (*TimerSnapshot) UpdateSince(time.Time) { 335 panic("UpdateSince called on a TimerSnapshot") 336 } 337 338 //variance返回快照时值的方差 339 //拿。 340 func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() } 341