github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/metrics/timer.go (about) 1 package metrics 2 3 import ( 4 "sync" 5 "time" 6 ) 7 8 type Timer interface { 9 Count() int64 10 Max() int64 11 Mean() float64 12 Min() int64 13 Percentile(float64) float64 14 Percentiles([]float64) []float64 15 Rate1() float64 16 Rate5() float64 17 Rate15() float64 18 RateMean() float64 19 Snapshot() Timer 20 StdDev() float64 21 Stop() 22 Sum() int64 23 Time(func()) 24 Update(time.Duration) 25 UpdateSince(time.Time) 26 Variance() float64 27 } 28 29 func GetOrRegisterTimer(name string, r Registry) Timer { 30 if nil == r { 31 r = DefaultRegistry 32 } 33 return r.GetOrRegister(name, NewTimer).(Timer) 34 } 35 36 func NewCustomTimer(h Histogram, m Meter) Timer { 37 if !Enabled { 38 return NilTimer{} 39 } 40 return &StandardTimer{ 41 histogram: h, 42 meter: m, 43 } 44 } 45 46 func NewRegisteredTimer(name string, r Registry) Timer { 47 c := NewTimer() 48 if nil == r { 49 r = DefaultRegistry 50 } 51 r.Register(name, c) 52 return c 53 } 54 55 func NewTimer() Timer { 56 if !Enabled { 57 return NilTimer{} 58 } 59 return &StandardTimer{ 60 histogram: NewHistogram(NewExpDecaySample(1028, 0.015)), 61 meter: NewMeter(), 62 } 63 } 64 65 type NilTimer struct { 66 h Histogram 67 m Meter 68 } 69 70 func (NilTimer) Count() int64 { return 0 } 71 72 func (NilTimer) Max() int64 { return 0 } 73 74 func (NilTimer) Mean() float64 { return 0.0 } 75 76 func (NilTimer) Min() int64 { return 0 } 77 78 func (NilTimer) Percentile(p float64) float64 { return 0.0 } 79 80 func (NilTimer) Percentiles(ps []float64) []float64 { 81 return make([]float64, len(ps)) 82 } 83 84 func (NilTimer) Rate1() float64 { return 0.0 } 85 86 func (NilTimer) Rate5() float64 { return 0.0 } 87 88 func (NilTimer) Rate15() float64 { return 0.0 } 89 90 func (NilTimer) RateMean() float64 { return 0.0 } 91 92 func (NilTimer) Snapshot() Timer { return NilTimer{} } 93 94 func (NilTimer) StdDev() float64 { return 0.0 } 95 96 func (NilTimer) Stop() {} 97 98 func (NilTimer) Sum() int64 { return 0 } 99 100 func (NilTimer) Time(func()) {} 101 102 func (NilTimer) Update(time.Duration) {} 103 104 func (NilTimer) UpdateSince(time.Time) {} 105 106 func (NilTimer) Variance() float64 { return 0.0 } 107 108 type StandardTimer struct { 109 histogram Histogram 110 meter Meter 111 mutex sync.Mutex 112 } 113 114 func (t *StandardTimer) Count() int64 { 115 return t.histogram.Count() 116 } 117 118 func (t *StandardTimer) Max() int64 { 119 return t.histogram.Max() 120 } 121 122 func (t *StandardTimer) Mean() float64 { 123 return t.histogram.Mean() 124 } 125 126 func (t *StandardTimer) Min() int64 { 127 return t.histogram.Min() 128 } 129 130 func (t *StandardTimer) Percentile(p float64) float64 { 131 return t.histogram.Percentile(p) 132 } 133 134 func (t *StandardTimer) Percentiles(ps []float64) []float64 { 135 return t.histogram.Percentiles(ps) 136 } 137 138 func (t *StandardTimer) Rate1() float64 { 139 return t.meter.Rate1() 140 } 141 142 func (t *StandardTimer) Rate5() float64 { 143 return t.meter.Rate5() 144 } 145 146 func (t *StandardTimer) Rate15() float64 { 147 return t.meter.Rate15() 148 } 149 150 func (t *StandardTimer) RateMean() float64 { 151 return t.meter.RateMean() 152 } 153 154 func (t *StandardTimer) Snapshot() Timer { 155 t.mutex.Lock() 156 defer t.mutex.Unlock() 157 return &TimerSnapshot{ 158 histogram: t.histogram.Snapshot().(*HistogramSnapshot), 159 meter: t.meter.Snapshot().(*MeterSnapshot), 160 } 161 } 162 163 func (t *StandardTimer) StdDev() float64 { 164 return t.histogram.StdDev() 165 } 166 167 func (t *StandardTimer) Stop() { 168 t.meter.Stop() 169 } 170 171 func (t *StandardTimer) Sum() int64 { 172 return t.histogram.Sum() 173 } 174 175 func (t *StandardTimer) Time(f func()) { 176 ts := time.Now() 177 f() 178 t.Update(time.Since(ts)) 179 } 180 181 func (t *StandardTimer) Update(d time.Duration) { 182 t.mutex.Lock() 183 defer t.mutex.Unlock() 184 t.histogram.Update(int64(d)) 185 t.meter.Mark(1) 186 } 187 188 func (t *StandardTimer) UpdateSince(ts time.Time) { 189 t.mutex.Lock() 190 defer t.mutex.Unlock() 191 t.histogram.Update(int64(time.Since(ts))) 192 t.meter.Mark(1) 193 } 194 195 func (t *StandardTimer) Variance() float64 { 196 return t.histogram.Variance() 197 } 198 199 type TimerSnapshot struct { 200 histogram *HistogramSnapshot 201 meter *MeterSnapshot 202 } 203 204 func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() } 205 206 func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() } 207 208 func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() } 209 210 func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() } 211 212 func (t *TimerSnapshot) Percentile(p float64) float64 { 213 return t.histogram.Percentile(p) 214 } 215 216 func (t *TimerSnapshot) Percentiles(ps []float64) []float64 { 217 return t.histogram.Percentiles(ps) 218 } 219 220 func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() } 221 222 func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() } 223 224 func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() } 225 226 func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() } 227 228 func (t *TimerSnapshot) Snapshot() Timer { return t } 229 230 func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() } 231 232 func (t *TimerSnapshot) Stop() {} 233 234 func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() } 235 236 func (*TimerSnapshot) Time(func()) { 237 panic("Time called on a TimerSnapshot") 238 } 239 240 func (*TimerSnapshot) Update(time.Duration) { 241 panic("Update called on a TimerSnapshot") 242 } 243 244 func (*TimerSnapshot) UpdateSince(time.Time) { 245 panic("UpdateSince called on a TimerSnapshot") 246 } 247 248 func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }