github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/resetting_timer.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 package metrics 10 11 import ( 12 "math" 13 "sort" 14 "sync" 15 "time" 16 ) 17 18 //存储在重置计时器中的值的初始切片容量 19 const InitialResettingTimerSliceCap = 10 20 21 //ResettingTimer用于存储计时器的聚合值,这些值在每个刷新间隔都会重置。 22 type ResettingTimer interface { 23 Values() []int64 24 Snapshot() ResettingTimer 25 Percentiles([]float64) []int64 26 Mean() float64 27 Time(func()) 28 Update(time.Duration) 29 UpdateSince(time.Time) 30 } 31 32 //GetOrRegisterResettingTimer返回现有的ResettingTimer或构造并注册 33 //新标准重置计时器。 34 func GetOrRegisterResettingTimer(name string, r Registry) ResettingTimer { 35 if nil == r { 36 r = DefaultRegistry 37 } 38 return r.GetOrRegister(name, NewResettingTimer).(ResettingTimer) 39 } 40 41 //NewRegisteredResettingTimer构造并注册新的StandardResettingTimer。 42 func NewRegisteredResettingTimer(name string, r Registry) ResettingTimer { 43 c := NewResettingTimer() 44 if nil == r { 45 r = DefaultRegistry 46 } 47 r.Register(name, c) 48 return c 49 } 50 51 //NewResettingTimer构造新的标准ResettingTimer 52 func NewResettingTimer() ResettingTimer { 53 if !Enabled { 54 return NilResettingTimer{} 55 } 56 return &StandardResettingTimer{ 57 values: make([]int64, 0, InitialResettingTimerSliceCap), 58 } 59 } 60 61 //NilResettingTimer是一个不允许的ResettingTimer。 62 type NilResettingTimer struct { 63 } 64 65 //值是不可操作的。 66 func (NilResettingTimer) Values() []int64 { return nil } 67 68 //快照是不可操作的。 69 func (NilResettingTimer) Snapshot() ResettingTimer { 70 return &ResettingTimerSnapshot{ 71 values: []int64{}, 72 } 73 } 74 75 //时间是不允许的。 76 func (NilResettingTimer) Time(func()) {} 77 78 //更新是不可操作的。 79 func (NilResettingTimer) Update(time.Duration) {} 80 81 //百分位数恐慌。 82 func (NilResettingTimer) Percentiles([]float64) []int64 { 83 panic("Percentiles called on a NilResettingTimer") 84 } 85 86 //平均恐慌。 87 func (NilResettingTimer) Mean() float64 { 88 panic("Mean called on a NilResettingTimer") 89 } 90 91 //updateSince是一个no-op。 92 func (NilResettingTimer) UpdateSince(time.Time) {} 93 94 //StandardResettingTimer是ResettingTimer的标准实现。 95 //和米。 96 type StandardResettingTimer struct { 97 values []int64 98 mutex sync.Mutex 99 } 100 101 //值返回包含所有测量值的切片。 102 func (t *StandardResettingTimer) Values() []int64 { 103 return t.values 104 } 105 106 //快照重置计时器并返回其内容的只读副本。 107 func (t *StandardResettingTimer) Snapshot() ResettingTimer { 108 t.mutex.Lock() 109 defer t.mutex.Unlock() 110 currentValues := t.values 111 t.values = make([]int64, 0, InitialResettingTimerSliceCap) 112 113 return &ResettingTimerSnapshot{ 114 values: currentValues, 115 } 116 } 117 118 //百分位数恐慌。 119 func (t *StandardResettingTimer) Percentiles([]float64) []int64 { 120 panic("Percentiles called on a StandardResettingTimer") 121 } 122 123 //平均恐慌。 124 func (t *StandardResettingTimer) Mean() float64 { 125 panic("Mean called on a StandardResettingTimer") 126 } 127 128 //记录给定函数执行的持续时间。 129 func (t *StandardResettingTimer) Time(f func()) { 130 ts := time.Now() 131 f() 132 t.Update(time.Since(ts)) 133 } 134 135 //记录事件的持续时间。 136 func (t *StandardResettingTimer) Update(d time.Duration) { 137 t.mutex.Lock() 138 defer t.mutex.Unlock() 139 t.values = append(t.values, int64(d)) 140 } 141 142 //记录一次开始到现在结束的事件的持续时间。 143 func (t *StandardResettingTimer) UpdateSince(ts time.Time) { 144 t.mutex.Lock() 145 defer t.mutex.Unlock() 146 t.values = append(t.values, int64(time.Since(ts))) 147 } 148 149 //ResettingTimersSnapshot是另一个ResettingTimer的时间点副本。 150 type ResettingTimerSnapshot struct { 151 values []int64 152 mean float64 153 thresholdBoundaries []int64 154 calculated bool 155 } 156 157 //快照返回快照。 158 func (t *ResettingTimerSnapshot) Snapshot() ResettingTimer { return t } 159 160 //时间恐慌。 161 func (*ResettingTimerSnapshot) Time(func()) { 162 panic("Time called on a ResettingTimerSnapshot") 163 } 164 165 //更新恐慌。 166 func (*ResettingTimerSnapshot) Update(time.Duration) { 167 panic("Update called on a ResettingTimerSnapshot") 168 } 169 170 //更新自恐慌。 171 func (*ResettingTimerSnapshot) UpdateSince(time.Time) { 172 panic("UpdateSince called on a ResettingTimerSnapshot") 173 } 174 175 //值返回快照中的所有值。 176 func (t *ResettingTimerSnapshot) Values() []int64 { 177 return t.values 178 } 179 180 //Percentiles返回输入百分位数的边界。 181 func (t *ResettingTimerSnapshot) Percentiles(percentiles []float64) []int64 { 182 t.calc(percentiles) 183 184 return t.thresholdBoundaries 185 } 186 187 //mean返回快照值的平均值 188 func (t *ResettingTimerSnapshot) Mean() float64 { 189 if !t.calculated { 190 t.calc([]float64{}) 191 } 192 193 return t.mean 194 } 195 196 func (t *ResettingTimerSnapshot) calc(percentiles []float64) { 197 sort.Sort(Int64Slice(t.values)) 198 199 count := len(t.values) 200 if count > 0 { 201 min := t.values[0] 202 max := t.values[count-1] 203 204 cumulativeValues := make([]int64, count) 205 cumulativeValues[0] = min 206 for i := 1; i < count; i++ { 207 cumulativeValues[i] = t.values[i] + cumulativeValues[i-1] 208 } 209 210 t.thresholdBoundaries = make([]int64, len(percentiles)) 211 212 thresholdBoundary := max 213 214 for i, pct := range percentiles { 215 if count > 1 { 216 var abs float64 217 if pct >= 0 { 218 abs = pct 219 } else { 220 abs = 100 + pct 221 } 222 //可怜人的数学。圆(X): 223 //数学楼层(x+0.5) 224 indexOfPerc := int(math.Floor(((abs / 100.0) * float64(count)) + 0.5)) 225 if pct >= 0 && indexOfPerc > 0 { 226 indexOfPerc -= 1 //索引偏移=0 227 } 228 thresholdBoundary = t.values[indexOfPerc] 229 } 230 231 t.thresholdBoundaries[i] = thresholdBoundary 232 } 233 234 sum := cumulativeValues[count-1] 235 t.mean = float64(sum) / float64(count) 236 } else { 237 t.thresholdBoundaries = make([]int64, len(percentiles)) 238 t.mean = 0 239 } 240 241 t.calculated = true 242 } 243 244 //Int64Slice将sort.interface方法附加到[]Int64,按递增顺序排序。 245 type Int64Slice []int64 246 247 func (s Int64Slice) Len() int { return len(s) } 248 func (s Int64Slice) Less(i, j int) bool { return s[i] < s[j] } 249 func (s Int64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }