diff --git a/meter.go b/meter.go index 53ff329..ac8c287 100644 --- a/meter.go +++ b/meter.go @@ -16,6 +16,7 @@ type Meter interface { RateMean() float64 Snapshot() Meter Stop() + Clear() } // GetOrRegisterMeter returns an existing Meter or constructs and registers a @@ -95,6 +96,9 @@ func (m *MeterSnapshot) Snapshot() Meter { return m } // Stop is a no-op. func (m *MeterSnapshot) Stop() {} +// Clear is a no-op. +func (m *MeterSnapshot) Clear(){} + // NilMeter is a no-op Meter. type NilMeter struct{} @@ -122,6 +126,9 @@ func (NilMeter) Snapshot() Meter { return NilMeter{} } // Stop is a no-op. func (NilMeter) Stop() {} +// Clear is a no-op. +func (NilMeter) Clear(){} + // StandardMeter is the standard implementation of a Meter. type StandardMeter struct { lock sync.RWMutex @@ -154,6 +161,18 @@ func (m *StandardMeter) Stop() { } } +// Clear resets the meter. +func (m *StandardMeter) Clear(){ + m.lock.Lock() + defer m.lock.Unlock() + + m.snapshot = &MeterSnapshot{} + m.a1=NewEWMA1() + m.a5=NewEWMA5() + m.a15=NewEWMA15() + m.startTime = time.Now() +} + // Count returns the number of events recorded. func (m *StandardMeter) Count() int64 { m.lock.RLock() diff --git a/meter_test.go b/meter_test.go index e889222..e9f0521 100644 --- a/meter_test.go +++ b/meter_test.go @@ -71,3 +71,13 @@ func TestMeterZero(t *testing.T) { t.Errorf("m.Count(): 0 != %v\n", count) } } + +func TestMeterClear(t *testing.T) { + m := NewMeter() + m.Mark(1234) + m.Clear() + + if m.Count()>0{ + t.Errorf("meter clear error.") + } +} \ No newline at end of file diff --git a/timer.go b/timer.go index d6ec4c6..f4e0152 100644 --- a/timer.go +++ b/timer.go @@ -25,6 +25,7 @@ type Timer interface { Update(time.Duration) UpdateSince(time.Time) Variance() float64 + Clear() } // GetOrRegisterTimer returns an existing Timer or constructs and registers a @@ -137,6 +138,9 @@ func (NilTimer) UpdateSince(time.Time) {} // Variance is a no-op. func (NilTimer) Variance() float64 { return 0.0 } +// Clear is a no-op. +func (NilTimer) Clear(){} + // StandardTimer is the standard implementation of a Timer and uses a Histogram // and Meter. type StandardTimer struct { @@ -249,6 +253,14 @@ func (t *StandardTimer) Variance() float64 { return t.histogram.Variance() } +// Clear stops the meter and clear the histogram. +func (t *StandardTimer) Clear(){ + t.mutex.Lock() + t.mutex.Unlock() + t.meter.Clear() + t.histogram.Clear() +} + // TimerSnapshot is a read-only copy of another Timer. type TimerSnapshot struct { histogram *HistogramSnapshot @@ -327,3 +339,6 @@ func (*TimerSnapshot) UpdateSince(time.Time) { // Variance returns the variance of the values at the time the snapshot was // taken. func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() } + +// Clear is a no-op. +func (t *TimerSnapshot) Clear(){} \ No newline at end of file diff --git a/timer_test.go b/timer_test.go index f85c9b8..b8ce871 100644 --- a/timer_test.go +++ b/timer_test.go @@ -93,6 +93,18 @@ func TestTimerZero(t *testing.T) { } } +func TestTimerClear(t *testing.T) { + tm := NewTimer() + tm.Time(func() { time.Sleep(50e6) }) + if max := tm.Max(); 45e6 > max || max > 55e6 { + t.Errorf("tm.Max(): 45e6 > %v || %v > 55e6\n", max, max) + } + tm.Clear() + if tm.Count()>0{ + t.Errorf("timer clear error.") + } +} + func ExampleGetOrRegisterTimer() { m := "account.create.latency" t := GetOrRegisterTimer(m, nil)