Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timer and meter clear #220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Meter interface {
RateMean() float64
Snapshot() Meter
Stop()
Clear()
}

// GetOrRegisterMeter returns an existing Meter or constructs and registers a
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
15 changes: 15 additions & 0 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(){}
12 changes: 12 additions & 0 deletions timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down