1
0
mirror of https://github.com/go-kit/kit.git synced 2025-07-17 01:12:38 +02:00

Set time unit on metrics.Timer

This commit is contained in:
esenac
2017-09-15 10:55:39 +02:00
parent f7593d1908
commit 4e222a33e0
3 changed files with 34 additions and 1 deletions

View File

@ -31,3 +31,29 @@ func TestTimerSlow(t *testing.T) {
t.Errorf("want %.3f, have %.3f", want, have)
}
}
func TestTimerUnit(t *testing.T) {
for _, tc := range []struct {
name string
unit time.Duration
tolerance float64
want float64
}{
{"Seconds", time.Second, 0.010, 0.100},
{"Milliseconds", time.Millisecond, 10, 100},
{"Nanoseconds", time.Nanosecond, 10000000, 100000000},
} {
t.Run(tc.name, func(t *testing.T) {
h := generic.NewSimpleHistogram()
timer := metrics.NewTimer(h)
time.Sleep(100 * time.Millisecond)
timer.Unit(tc.unit)
timer.ObserveDuration()
if want, have := tc.want, h.ApproximateMovingAverage(); math.Abs(want-have) > tc.tolerance {
t.Errorf("want %.3f, have %.3f", want, have)
}
})
}
}