...

Source file src/github.com/go-kit/kit/metrics/timer_test.go

Documentation: github.com/go-kit/kit/metrics

     1  package metrics_test
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"time"
     8  
     9  	"github.com/go-kit/kit/metrics"
    10  	"github.com/go-kit/kit/metrics/generic"
    11  )
    12  
    13  func TestTimerFast(t *testing.T) {
    14  	h := generic.NewSimpleHistogram()
    15  	metrics.NewTimer(h).ObserveDuration()
    16  
    17  	tolerance := 0.050
    18  	if want, have := 0.000, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
    19  		t.Errorf("want %.3f, have %.3f", want, have)
    20  	}
    21  }
    22  
    23  func TestTimerSlow(t *testing.T) {
    24  	h := generic.NewSimpleHistogram()
    25  	timer := metrics.NewTimer(h)
    26  	time.Sleep(250 * time.Millisecond)
    27  	timer.ObserveDuration()
    28  
    29  	tolerance := 0.050
    30  	if want, have := 0.250, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
    31  		t.Errorf("want %.3f, have %.3f", want, have)
    32  	}
    33  }
    34  
    35  func TestTimerUnit(t *testing.T) {
    36  	for _, tc := range []struct {
    37  		name      string
    38  		unit      time.Duration
    39  		tolerance float64
    40  		want      float64
    41  	}{
    42  		{"Seconds", time.Second, 0.010, 0.100},
    43  		{"Milliseconds", time.Millisecond, 10, 100},
    44  		{"Nanoseconds", time.Nanosecond, 10000000, 100000000},
    45  	} {
    46  		t.Run(tc.name, func(t *testing.T) {
    47  			h := generic.NewSimpleHistogram()
    48  			timer := metrics.NewTimer(h)
    49  			time.Sleep(100 * time.Millisecond)
    50  			timer.Unit(tc.unit)
    51  			timer.ObserveDuration()
    52  
    53  			if want, have := tc.want, h.ApproximateMovingAverage(); math.Abs(want-have) > tc.tolerance {
    54  				t.Errorf("want %.3f, have %.3f", want, have)
    55  			}
    56  		})
    57  	}
    58  }
    59  

View as plain text