9 "github.com/go-kit/kit/metrics"
10 "github.com/go-kit/kit/metrics/generic"
13 func TestTimerFast(t *testing.T) {
14 h := generic.NewSimpleHistogram()
15 metrics.NewTimer(h).ObserveDuration()
18 if want, have := 0.000, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
19 t.Errorf("want %.3f, have %.3f", want, have)
23 func TestTimerSlow(t *testing.T) {
24 h := generic.NewSimpleHistogram()
25 timer := metrics.NewTimer(h)
26 time.Sleep(250 * time.Millisecond)
27 timer.ObserveDuration()
30 if want, have := 0.250, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
31 t.Errorf("want %.3f, have %.3f", want, have)
35 func TestTimerUnit(t *testing.T) {
36 for _, tc := range []struct {
42 {"Seconds", time.Second, 0.010, 0.100},
43 {"Milliseconds", time.Millisecond, 10, 100},
44 {"Nanoseconds", time.Nanosecond, 10000000, 100000000},
46 t.Run(tc.name, func(t *testing.T) {
47 h := generic.NewSimpleHistogram()
48 timer := metrics.NewTimer(h)
49 time.Sleep(100 * time.Millisecond)
51 timer.ObserveDuration()
53 if want, have := tc.want, h.ApproximateMovingAverage(); math.Abs(want-have) > tc.tolerance {
54 t.Errorf("want %.3f, have %.3f", want, have)