Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// Meters count events to produce exponentially-weighted moving average rates
// at one-, five-, and fifteen-minutes and a mean rate.
type Meter interface {
Clear()
Count() int64
Mark(int64)
Rate1() float64
Expand Down Expand Up @@ -59,6 +60,11 @@ type MeterSnapshot struct {
rate1, rate5, rate15, rateMean float64
}

// Clear panics.
func (m *MeterSnapshot) Clear() {
panic("Clear called on a MeterSnapshot")
}

// Count returns the count of events at the time the snapshot was taken.
func (m *MeterSnapshot) Count() int64 { return m.count }

Expand Down Expand Up @@ -89,6 +95,9 @@ func (m *MeterSnapshot) Snapshot() Meter { return m }
// NilMeter is a no-op Meter.
type NilMeter struct{}

// Clear is a no-op.
func (NilMeter) Clear() {}

// Count is a no-op.
func (NilMeter) Count() int64 { return 0 }

Expand Down Expand Up @@ -128,6 +137,18 @@ func newStandardMeter() *StandardMeter {
}
}

// 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()
Expand Down
17 changes: 17 additions & 0 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// Timers capture the duration and rate of events.
type Timer interface {
Clear()
Count() int64
Max() int64
Mean() float64
Expand Down Expand Up @@ -74,6 +75,9 @@ type NilTimer struct {
m Meter
}

// Clear is a no-op.
func (NilTimer) Clear() {}

// Count is a no-op.
func (NilTimer) Count() int64 { return 0 }

Expand Down Expand Up @@ -135,6 +139,14 @@ type StandardTimer struct {
mutex sync.Mutex
}

// Clear resets the timer.
func (t *StandardTimer) Clear() {
t.mutex.Lock()
defer t.mutex.Unlock()
t.histogram.Clear()
t.meter.Clear()
}

// Count returns the number of events recorded.
func (t *StandardTimer) Count() int64 {
return t.histogram.Count()
Expand Down Expand Up @@ -240,6 +252,11 @@ type TimerSnapshot struct {
meter *MeterSnapshot
}

// Clear panics.
func (*TimerSnapshot) Clear() {
panic("Clear called on a TimerSnapshot")
}

// Count returns the number of events recorded at the time the snapshot was
// taken.
func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
Expand Down