github.com/aerth/aquachain@v1.4.1/metrics/meter_test.go (about) 1 package metrics 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func BenchmarkMeter(b *testing.B) { 9 m := NewMeter() 10 b.ResetTimer() 11 for i := 0; i < b.N; i++ { 12 m.Mark(1) 13 } 14 } 15 16 func TestGetOrRegisterMeter(t *testing.T) { 17 r := NewRegistry() 18 NewRegisteredMeter("foo", r).Mark(47) 19 if m := GetOrRegisterMeter("foo", r); 47 != m.Count() { 20 t.Fatal(m) 21 } 22 } 23 24 func TestMeterDecay(t *testing.T) { 25 ma := meterArbiter{ 26 ticker: time.NewTicker(time.Millisecond), 27 meters: make(map[*StandardMeter]struct{}), 28 } 29 m := newStandardMeter() 30 ma.meters[m] = struct{}{} 31 go ma.tick() 32 m.Mark(1) 33 rateMean := m.RateMean() 34 time.Sleep(100 * time.Millisecond) 35 if m.RateMean() >= rateMean { 36 t.Error("m.RateMean() didn't decrease") 37 } 38 } 39 40 func TestMeterNonzero(t *testing.T) { 41 m := NewMeter() 42 m.Mark(3) 43 if count := m.Count(); 3 != count { 44 t.Errorf("m.Count(): 3 != %v\n", count) 45 } 46 } 47 48 func TestMeterStop(t *testing.T) { 49 l := len(arbiter.meters) 50 m := NewMeter() 51 if len(arbiter.meters) != l+1 { 52 t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) 53 } 54 m.Stop() 55 if len(arbiter.meters) != l { 56 t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) 57 } 58 } 59 60 func TestMeterSnapshot(t *testing.T) { 61 m := NewMeter() 62 m.Mark(1) 63 if snapshot := m.Snapshot(); m.RateMean() != snapshot.RateMean() { 64 t.Fatal(snapshot) 65 } 66 } 67 68 func TestMeterZero(t *testing.T) { 69 m := NewMeter() 70 if count := m.Count(); 0 != count { 71 t.Errorf("m.Count(): 0 != %v\n", count) 72 } 73 }