github.com/JFJun/bsc@v1.0.0/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)
    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  	go ma.tick()
    33  	m.Mark(1)
    34  	rateMean := m.RateMean()
    35  	time.Sleep(100 * time.Millisecond)
    36  	if m.RateMean() >= rateMean {
    37  		t.Error("m.RateMean() didn't decrease")
    38  	}
    39  }
    40  
    41  func TestMeterNonzero(t *testing.T) {
    42  	m := NewMeter()
    43  	m.Mark(3)
    44  	if count := m.Count(); count != 3 {
    45  		t.Errorf("m.Count(): 3 != %v\n", count)
    46  	}
    47  }
    48  
    49  func TestMeterStop(t *testing.T) {
    50  	l := len(arbiter.meters)
    51  	m := NewMeter()
    52  	if l+1 != len(arbiter.meters) {
    53  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    54  	}
    55  	m.Stop()
    56  	if l != len(arbiter.meters) {
    57  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    58  	}
    59  }
    60  
    61  func TestMeterSnapshot(t *testing.T) {
    62  	m := NewMeter()
    63  	m.Mark(1)
    64  	if snapshot := m.Snapshot(); m.RateMean() != snapshot.RateMean() {
    65  		t.Fatal(snapshot)
    66  	}
    67  }
    68  
    69  func TestMeterZero(t *testing.T) {
    70  	m := NewMeter()
    71  	if count := m.Count(); count != 0 {
    72  		t.Errorf("m.Count(): 0 != %v\n", count)
    73  	}
    74  }