Codebase list golang-github-go-kit-kit / 3b9658d9-4065-4804-9af8-16961c419f60/v0.5.0 metrics / timer_test.go
3b9658d9-4065-4804-9af8-16961c419f60/v0.5.0

Tree @3b9658d9-4065-4804-9af8-16961c419f60/v0.5.0 (Download .tar.gz)

timer_test.go @3b9658d9-4065-4804-9af8-16961c419f60/v0.5.0raw · history · blame

package metrics_test

import (
	"math"
	"testing"

	"time"

	"github.com/go-kit/kit/metrics"
	"github.com/go-kit/kit/metrics/generic"
)

func TestTimerFast(t *testing.T) {
	h := generic.NewSimpleHistogram()
	metrics.NewTimer(h).ObserveDuration()

	tolerance := 0.050
	if want, have := 0.000, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
		t.Errorf("want %.3f, have %.3f", want, have)
	}
}

func TestTimerSlow(t *testing.T) {
	h := generic.NewSimpleHistogram()
	timer := metrics.NewTimer(h)
	time.Sleep(250 * time.Millisecond)
	timer.ObserveDuration()

	tolerance := 0.050
	if want, have := 0.250, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
		t.Errorf("want %.3f, have %.3f", want, have)
	}
}