github.com/theQRL/go-zond@v0.1.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  	ma := meterArbiter{
    32  		ticker: time.NewTicker(time.Millisecond),
    33  		meters: make(map[*StandardMeter]struct{}),
    34  	}
    35  	defer ma.ticker.Stop()
    36  	m := newStandardMeter()
    37  	ma.meters[m] = struct{}{}
    38  	m.Mark(1)
    39  	ma.tickMeters()
    40  	rateMean := m.Snapshot().RateMean()
    41  	time.Sleep(100 * time.Millisecond)
    42  	ma.tickMeters()
    43  	if m.Snapshot().RateMean() >= rateMean {
    44  		t.Error("m.RateMean() didn't decrease")
    45  	}
    46  }
    47  
    48  func TestMeterNonzero(t *testing.T) {
    49  	m := NewMeter()
    50  	m.Mark(3)
    51  	if count := m.Snapshot().Count(); count != 3 {
    52  		t.Errorf("m.Count(): 3 != %v\n", count)
    53  	}
    54  }
    55  
    56  func TestMeterStop(t *testing.T) {
    57  	l := len(arbiter.meters)
    58  	m := NewMeter()
    59  	if l+1 != len(arbiter.meters) {
    60  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    61  	}
    62  	m.Stop()
    63  	if l != len(arbiter.meters) {
    64  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    65  	}
    66  }
    67  
    68  func TestMeterZero(t *testing.T) {
    69  	m := NewMeter().Snapshot()
    70  	if count := m.Count(); count != 0 {
    71  		t.Errorf("m.Count(): 0 != %v\n", count)
    72  	}
    73  }
    74  
    75  func TestMeterRepeat(t *testing.T) {
    76  	m := NewMeter()
    77  	for i := 0; i < 101; i++ {
    78  		m.Mark(int64(i))
    79  	}
    80  	if count := m.Snapshot().Count(); count != 5050 {
    81  		t.Errorf("m.Count(): 5050 != %v\n", count)
    82  	}
    83  	for i := 0; i < 101; i++ {
    84  		m.Mark(int64(i))
    85  	}
    86  	if count := m.Snapshot().Count(); count != 10100 {
    87  		t.Errorf("m.Count(): 10100 != %v\n", count)
    88  	}
    89  }