github.com/ethereum/go-ethereum@v1.16.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 func TestMeter(t *testing.T) { 16 m := NewMeter() 17 m.Mark(47) 18 if v := m.Snapshot().Count(); v != 47 { 19 t.Fatalf("have %d want %d", v, 47) 20 } 21 } 22 func TestGetOrRegisterMeter(t *testing.T) { 23 r := NewRegistry() 24 NewRegisteredMeter("foo", r).Mark(47) 25 if m := GetOrRegisterMeter("foo", r).Snapshot(); m.Count() != 47 { 26 t.Fatal(m.Count()) 27 } 28 } 29 30 func TestMeterDecay(t *testing.T) { 31 m := newMeter() 32 m.Mark(1) 33 m.tick() 34 rateMean := m.Snapshot().RateMean() 35 time.Sleep(100 * time.Millisecond) 36 m.tick() 37 if m.Snapshot().RateMean() >= rateMean { 38 t.Error("m.RateMean() didn't decrease") 39 } 40 } 41 42 func TestMeterNonzero(t *testing.T) { 43 m := NewMeter() 44 m.Mark(3) 45 if count := m.Snapshot().Count(); count != 3 { 46 t.Errorf("m.Count(): 3 != %v\n", count) 47 } 48 } 49 50 func TestMeterStop(t *testing.T) { 51 l := len(arbiter.meters) 52 m := NewMeter() 53 if l+1 != len(arbiter.meters) { 54 t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters)) 55 } 56 m.Stop() 57 if l != len(arbiter.meters) { 58 t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters)) 59 } 60 } 61 62 func TestMeterZero(t *testing.T) { 63 m := NewMeter().Snapshot() 64 if count := m.Count(); count != 0 { 65 t.Errorf("m.Count(): 0 != %v\n", count) 66 } 67 } 68 69 func TestMeterRepeat(t *testing.T) { 70 m := NewMeter() 71 for i := 0; i < 101; i++ { 72 m.Mark(int64(i)) 73 } 74 if count := m.Snapshot().Count(); count != 5050 { 75 t.Errorf("m.Count(): 5050 != %v\n", count) 76 } 77 for i := 0; i < 101; i++ { 78 m.Mark(int64(i)) 79 } 80 if count := m.Snapshot().Count(); count != 10100 { 81 t.Errorf("m.Count(): 10100 != %v\n", count) 82 } 83 }