github.com/phillinzzz/newBsc@v1.1.6/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); m.Count() != 47 {
    20  		t.Fatal(m.Count())
    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  	defer ma.ticker.Stop()
    30  	m := newStandardMeter()
    31  	ma.meters[m] = struct{}{}
    32  	m.Mark(1)
    33  	ma.tickMeters()
    34  	rateMean := m.RateMean()
    35  	time.Sleep(100 * time.Millisecond)
    36  	ma.tickMeters()
    37  	if m.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.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 TestMeterSnapshot(t *testing.T) {
    63  	m := NewMeter()
    64  	m.Mark(1)
    65  	if snapshot := m.Snapshot(); m.RateMean() != snapshot.RateMean() {
    66  		t.Fatal(snapshot)
    67  	}
    68  }
    69  
    70  func TestMeterZero(t *testing.T) {
    71  	m := NewMeter()
    72  	if count := m.Count(); count != 0 {
    73  		t.Errorf("m.Count(): 0 != %v\n", count)
    74  	}
    75  }
    76  
    77  func TestMeterRepeat(t *testing.T) {
    78  	m := NewMeter()
    79  	for i := 0; i < 101; i++ {
    80  		m.Mark(int64(i))
    81  	}
    82  	if count := m.Count(); count != 5050 {
    83  		t.Errorf("m.Count(): 5050 != %v\n", count)
    84  	}
    85  	for i := 0; i < 101; i++ {
    86  		m.Mark(int64(i))
    87  	}
    88  	if count := m.Count(); count != 10100 {
    89  		t.Errorf("m.Count(): 10100 != %v\n", count)
    90  	}
    91  }