Skip to content

Commit

Permalink
add Stop() to Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
inooka_shiroyuki committed Apr 17, 2017
1 parent c83b0ea commit 4b2ddcb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func TestMeterNonzero(t *testing.T) {
}
}

func TestMeterStop(t *testing.T) {
l := len(arbiter.meters)
m := NewMeter()
if len(arbiter.meters) != l+1 {
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
}
m.Stop()
if len(arbiter.meters) != l {
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
}
}

func TestMeterSnapshot(t *testing.T) {
m := NewMeter()
m.Mark(1)
Expand Down
15 changes: 15 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (r *StandardRegistry) RunHealthchecks() {
func (r *StandardRegistry) Unregister(name string) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.stop(name)
delete(r.metrics, name)
}

Expand All @@ -121,6 +122,7 @@ func (r *StandardRegistry) UnregisterAll() {
r.mutex.Lock()
defer r.mutex.Unlock()
for name, _ := range r.metrics {
r.stop(name)
delete(r.metrics, name)
}
}
Expand All @@ -146,6 +148,19 @@ func (r *StandardRegistry) registered() map[string]interface{} {
return metrics
}

func (r *StandardRegistry) stop(name string) {
if i, ok := r.metrics[name]; ok {
if s, ok := i.(Stoppable); ok {
s.Stop()
}
}
}

// Stoppable defines the metrics which has to be stopped.
type Stoppable interface {
Stop()
}

type PrefixedRegistry struct {
underlying Registry
prefix string
Expand Down
17 changes: 17 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
}
}

func TestRegistryUnregister(t *testing.T) {
l := len(arbiter.meters)
r := NewRegistry()
r.Register("foo", NewCounter())
r.Register("bar", NewMeter())
r.Register("baz", NewTimer())
if len(arbiter.meters) != l+2 {
t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
}
r.Unregister("foo")
r.Unregister("bar")
r.Unregister("baz")
if len(arbiter.meters) != l {
t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters))
}
}

func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
r := NewRegistry()
pr := NewPrefixedChildRegistry(r, "prefix.")
Expand Down
12 changes: 12 additions & 0 deletions timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Timer interface {
RateMean() float64
Snapshot() Timer
StdDev() float64
Stop()
Sum() int64
Time(func())
Update(time.Duration)
Expand Down Expand Up @@ -112,6 +113,9 @@ func (NilTimer) Snapshot() Timer { return NilTimer{} }
// StdDev is a no-op.
func (NilTimer) StdDev() float64 { return 0.0 }

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

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

Expand Down Expand Up @@ -201,6 +205,11 @@ func (t *StandardTimer) StdDev() float64 {
return t.histogram.StdDev()
}

// Stop stops the meter.
func (t *StandardTimer) Stop() {
t.meter.Stop()
}

// Sum returns the sum in the sample.
func (t *StandardTimer) Sum() int64 {
return t.histogram.Sum()
Expand Down Expand Up @@ -288,6 +297,9 @@ func (t *TimerSnapshot) Snapshot() Timer { return t }
// was taken.
func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }

// Stop is a no-op.
func (t *TimerSnapshot) Stop() {}

// Sum returns the sum at the time the snapshot was taken.
func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }

Expand Down
12 changes: 12 additions & 0 deletions timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func TestTimerExtremes(t *testing.T) {
}
}

func TestTimerStop(t *testing.T) {
l := len(arbiter.meters)
tm := NewTimer()
if len(arbiter.meters) != l+1 {
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
}
tm.Stop()
if len(arbiter.meters) != l {
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
}
}

func TestTimerFunc(t *testing.T) {
tm := NewTimer()
tm.Time(func() { time.Sleep(50e6) })
Expand Down

0 comments on commit 4b2ddcb

Please sign in to comment.