github.com/ethereum/go-ethereum@v1.16.1/metrics/timer.go (about)

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