github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/metrics/timer.go (about) 1 package metrics 2 3 import ( 4 "sync" 5 "time" 6 ) 7 8 type TimerSnapshot interface { 9 HistogramSnapshot 10 MeterSnapshot 11 } 12 13 // Timer capture the duration and rate of events. 14 type Timer interface { 15 Snapshot() TimerSnapshot 16 Stop() 17 Time(func()) 18 UpdateSince(time.Time) 19 Update(time.Duration) 20 } 21 22 // GetOrRegisterTimer returns an existing Timer or constructs and registers a 23 // new StandardTimer. 24 // Be sure to unregister the meter from the registry once it is of no use to 25 // allow for garbage collection. 26 func GetOrRegisterTimer(name string, r Registry) Timer { 27 if nil == r { 28 r = DefaultRegistry 29 } 30 return r.GetOrRegister(name, NewTimer).(Timer) 31 } 32 33 // NewCustomTimer constructs a new StandardTimer from a Histogram and a Meter. 34 // Be sure to call Stop() once the timer is of no use to allow for garbage collection. 35 func NewCustomTimer(h Histogram, m Meter) Timer { 36 if !Enabled { 37 return NilTimer{} 38 } 39 return &StandardTimer{ 40 histogram: h, 41 meter: m, 42 } 43 } 44 45 // NewRegisteredTimer constructs and registers a new StandardTimer. 46 // Be sure to unregister the meter from the registry once it is of no use to 47 // allow for garbage collection. 48 func NewRegisteredTimer(name string, r Registry) Timer { 49 c := NewTimer() 50 if nil == r { 51 r = DefaultRegistry 52 } 53 r.Register(name, c) 54 return c 55 } 56 57 // NewTimer constructs a new StandardTimer using an exponentially-decaying 58 // sample with the same reservoir size and alpha as UNIX load averages. 59 // Be sure to call Stop() once the timer is of no use to allow for garbage collection. 60 func NewTimer() Timer { 61 if !Enabled { 62 return NilTimer{} 63 } 64 return &StandardTimer{ 65 histogram: NewHistogram(NewExpDecaySample(1028, 0.015)), 66 meter: NewMeter(), 67 } 68 } 69 70 // NilTimer is a no-op Timer. 71 type NilTimer struct{} 72 73 func (NilTimer) Snapshot() TimerSnapshot { return (*emptySnapshot)(nil) } 74 func (NilTimer) Stop() {} 75 func (NilTimer) Time(f func()) { f() } 76 func (NilTimer) Update(time.Duration) {} 77 func (NilTimer) UpdateSince(time.Time) {} 78 79 // StandardTimer is the standard implementation of a Timer and uses a Histogram 80 // and Meter. 81 type StandardTimer struct { 82 histogram Histogram 83 meter Meter 84 mutex sync.Mutex 85 } 86 87 // Snapshot returns a read-only copy of the timer. 88 func (t *StandardTimer) Snapshot() TimerSnapshot { 89 t.mutex.Lock() 90 defer t.mutex.Unlock() 91 return &timerSnapshot{ 92 histogram: t.histogram.Snapshot(), 93 meter: t.meter.Snapshot(), 94 } 95 } 96 97 // Stop stops the meter. 98 func (t *StandardTimer) Stop() { 99 t.meter.Stop() 100 } 101 102 // Time record the duration of the execution of the given function. 103 func (t *StandardTimer) Time(f func()) { 104 ts := time.Now() 105 f() 106 t.Update(time.Since(ts)) 107 } 108 109 // Update the duration of an event, in nanoseconds. 110 func (t *StandardTimer) Update(d time.Duration) { 111 t.mutex.Lock() 112 defer t.mutex.Unlock() 113 t.histogram.Update(d.Nanoseconds()) 114 t.meter.Mark(1) 115 } 116 117 // UpdateSince update the duration of an event that started at a time and ends now. 118 // The record uses nanoseconds. 119 func (t *StandardTimer) UpdateSince(ts time.Time) { 120 t.Update(time.Since(ts)) 121 } 122 123 // timerSnapshot is a read-only copy of another Timer. 124 type timerSnapshot struct { 125 histogram HistogramSnapshot 126 meter MeterSnapshot 127 } 128 129 // Count returns the number of events recorded at the time the snapshot was 130 // taken. 131 func (t *timerSnapshot) Count() int64 { return t.histogram.Count() } 132 133 // Max returns the maximum value at the time the snapshot was taken. 134 func (t *timerSnapshot) Max() int64 { return t.histogram.Max() } 135 136 // Size returns the size of the sample at the time the snapshot was taken. 137 func (t *timerSnapshot) Size() int { return t.histogram.Size() } 138 139 // Mean returns the mean value at the time the snapshot was taken. 140 func (t *timerSnapshot) Mean() float64 { return t.histogram.Mean() } 141 142 // Min returns the minimum value at the time the snapshot was taken. 143 func (t *timerSnapshot) Min() int64 { return t.histogram.Min() } 144 145 // Percentile returns an arbitrary percentile of sampled values at the time the 146 // snapshot was taken. 147 func (t *timerSnapshot) Percentile(p float64) float64 { 148 return t.histogram.Percentile(p) 149 } 150 151 // Percentiles returns a slice of arbitrary percentiles of sampled values at 152 // the time the snapshot was taken. 153 func (t *timerSnapshot) Percentiles(ps []float64) []float64 { 154 return t.histogram.Percentiles(ps) 155 } 156 157 // Rate1 returns the one-minute moving average rate of events per second at the 158 // time the snapshot was taken. 159 func (t *timerSnapshot) Rate1() float64 { return t.meter.Rate1() } 160 161 // Rate5 returns the five-minute moving average rate of events per second at 162 // the time the snapshot was taken. 163 func (t *timerSnapshot) Rate5() float64 { return t.meter.Rate5() } 164 165 // Rate15 returns the fifteen-minute moving average rate of events per second 166 // at the time the snapshot was taken. 167 func (t *timerSnapshot) Rate15() float64 { return t.meter.Rate15() } 168 169 // RateMean returns the meter's mean rate of events per second at the time the 170 // snapshot was taken. 171 func (t *timerSnapshot) RateMean() float64 { return t.meter.RateMean() } 172 173 // StdDev returns the standard deviation of the values at the time the snapshot 174 // was taken. 175 func (t *timerSnapshot) StdDev() float64 { return t.histogram.StdDev() } 176 177 // Sum returns the sum at the time the snapshot was taken. 178 func (t *timerSnapshot) Sum() int64 { return t.histogram.Sum() } 179 180 // Variance returns the variance of the values at the time the snapshot was 181 // taken. 182 func (t *timerSnapshot) Variance() float64 { return t.histogram.Variance() }