github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/rcrowley/go-metrics/timer_test.go (about) 1 package metrics 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 ) 8 9 func BenchmarkTimer(b *testing.B) { 10 tm := NewTimer() 11 b.ResetTimer() 12 for i := 0; i < b.N; i++ { 13 tm.Update(1) 14 } 15 } 16 17 func TestGetOrRegisterTimer(t *testing.T) { 18 r := NewRegistry() 19 NewRegisteredTimer("foo", r).Update(47) 20 if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() { 21 t.Fatal(tm) 22 } 23 } 24 25 func TestTimerExtremes(t *testing.T) { 26 tm := NewTimer() 27 tm.Update(math.MaxInt64) 28 tm.Update(0) 29 if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev { 30 t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev) 31 } 32 } 33 34 func TestTimerFunc(t *testing.T) { 35 tm := NewTimer() 36 tm.Time(func() { time.Sleep(50e6) }) 37 if max := tm.Max(); 45e6 > max || max > 55e6 { 38 t.Errorf("tm.Max(): 45e6 > %v || %v > 55e6\n", max, max) 39 } 40 } 41 42 func TestTimerZero(t *testing.T) { 43 tm := NewTimer() 44 if count := tm.Count(); 0 != count { 45 t.Errorf("tm.Count(): 0 != %v\n", count) 46 } 47 if min := tm.Min(); 0 != min { 48 t.Errorf("tm.Min(): 0 != %v\n", min) 49 } 50 if max := tm.Max(); 0 != max { 51 t.Errorf("tm.Max(): 0 != %v\n", max) 52 } 53 if mean := tm.Mean(); 0.0 != mean { 54 t.Errorf("tm.Mean(): 0.0 != %v\n", mean) 55 } 56 if stdDev := tm.StdDev(); 0.0 != stdDev { 57 t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev) 58 } 59 ps := tm.Percentiles([]float64{0.5, 0.75, 0.99}) 60 if 0.0 != ps[0] { 61 t.Errorf("median: 0.0 != %v\n", ps[0]) 62 } 63 if 0.0 != ps[1] { 64 t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) 65 } 66 if 0.0 != ps[2] { 67 t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) 68 } 69 if rate1 := tm.Rate1(); 0.0 != rate1 { 70 t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1) 71 } 72 if rate5 := tm.Rate5(); 0.0 != rate5 { 73 t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5) 74 } 75 if rate15 := tm.Rate15(); 0.0 != rate15 { 76 t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15) 77 } 78 if rateMean := tm.RateMean(); 0.0 != rateMean { 79 t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean) 80 } 81 }