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:
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user