github.com/codingfuture/orig-energi3@v0.8.4/metrics/timer_test.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2018 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package metrics 19 20 import ( 21 "fmt" 22 "math" 23 "testing" 24 "time" 25 ) 26 27 func BenchmarkTimer(b *testing.B) { 28 tm := NewTimer() 29 b.ResetTimer() 30 for i := 0; i < b.N; i++ { 31 tm.Update(1) 32 } 33 } 34 35 func TestGetOrRegisterTimer(t *testing.T) { 36 r := NewRegistry() 37 NewRegisteredTimer("foo", r).Update(47) 38 if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() { 39 t.Fatal(tm) 40 } 41 } 42 43 func TestTimerExtremes(t *testing.T) { 44 tm := NewTimer() 45 tm.Update(math.MaxInt64) 46 tm.Update(0) 47 if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev { 48 t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev) 49 } 50 } 51 52 func TestTimerStop(t *testing.T) { 53 l := len(arbiter.meters) 54 tm := NewTimer() 55 if len(arbiter.meters) != l+1 { 56 t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) 57 } 58 tm.Stop() 59 if len(arbiter.meters) != l { 60 t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) 61 } 62 } 63 64 func TestTimerFunc(t *testing.T) { 65 tm := NewTimer() 66 tm.Time(func() { time.Sleep(50e6) }) 67 if max := tm.Max(); 35e6 > max || max > 145e6 { 68 t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max) 69 } 70 } 71 72 func TestTimerZero(t *testing.T) { 73 tm := NewTimer() 74 if count := tm.Count(); 0 != count { 75 t.Errorf("tm.Count(): 0 != %v\n", count) 76 } 77 if min := tm.Min(); 0 != min { 78 t.Errorf("tm.Min(): 0 != %v\n", min) 79 } 80 if max := tm.Max(); 0 != max { 81 t.Errorf("tm.Max(): 0 != %v\n", max) 82 } 83 if mean := tm.Mean(); 0.0 != mean { 84 t.Errorf("tm.Mean(): 0.0 != %v\n", mean) 85 } 86 if stdDev := tm.StdDev(); 0.0 != stdDev { 87 t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev) 88 } 89 ps := tm.Percentiles([]float64{0.5, 0.75, 0.99}) 90 if 0.0 != ps[0] { 91 t.Errorf("median: 0.0 != %v\n", ps[0]) 92 } 93 if 0.0 != ps[1] { 94 t.Errorf("75th percentile: 0.0 != %v\n", ps[1]) 95 } 96 if 0.0 != ps[2] { 97 t.Errorf("99th percentile: 0.0 != %v\n", ps[2]) 98 } 99 if rate1 := tm.Rate1(); 0.0 != rate1 { 100 t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1) 101 } 102 if rate5 := tm.Rate5(); 0.0 != rate5 { 103 t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5) 104 } 105 if rate15 := tm.Rate15(); 0.0 != rate15 { 106 t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15) 107 } 108 if rateMean := tm.RateMean(); 0.0 != rateMean { 109 t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean) 110 } 111 } 112 113 func ExampleGetOrRegisterTimer() { 114 m := "account.create.latency" 115 t := GetOrRegisterTimer(m, nil) 116 t.Update(47) 117 fmt.Println(t.Max()) // Output: 47 118 }