github.com/codingfuture/orig-energi3@v0.8.4/metrics/meter_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func BenchmarkMeter(b *testing.B) {
    26  	m := NewMeter()
    27  	b.ResetTimer()
    28  	for i := 0; i < b.N; i++ {
    29  		m.Mark(1)
    30  	}
    31  }
    32  
    33  func TestGetOrRegisterMeter(t *testing.T) {
    34  	r := NewRegistry()
    35  	NewRegisteredMeter("foo", r).Mark(47)
    36  	if m := GetOrRegisterMeter("foo", r); 47 != m.Count() {
    37  		t.Fatal(m)
    38  	}
    39  }
    40  
    41  func TestMeterDecay(t *testing.T) {
    42  	ma := meterArbiter{
    43  		ticker: time.NewTicker(time.Millisecond),
    44  		meters: make(map[*StandardMeter]struct{}),
    45  	}
    46  	m := newStandardMeter()
    47  	ma.meters[m] = struct{}{}
    48  	go ma.tick()
    49  	m.Mark(1)
    50  	rateMean := m.RateMean()
    51  	time.Sleep(100 * time.Millisecond)
    52  	if m.RateMean() >= rateMean {
    53  		t.Error("m.RateMean() didn't decrease")
    54  	}
    55  }
    56  
    57  func TestMeterNonzero(t *testing.T) {
    58  	m := NewMeter()
    59  	m.Mark(3)
    60  	if count := m.Count(); 3 != count {
    61  		t.Errorf("m.Count(): 3 != %v\n", count)
    62  	}
    63  }
    64  
    65  func TestMeterStop(t *testing.T) {
    66  	l := len(arbiter.meters)
    67  	m := NewMeter()
    68  	if len(arbiter.meters) != l+1 {
    69  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    70  	}
    71  	m.Stop()
    72  	if len(arbiter.meters) != l {
    73  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    74  	}
    75  }
    76  
    77  func TestMeterSnapshot(t *testing.T) {
    78  	m := NewMeter()
    79  	m.Mark(1)
    80  	if snapshot := m.Snapshot(); m.RateMean() != snapshot.RateMean() {
    81  		t.Fatal(snapshot)
    82  	}
    83  }
    84  
    85  func TestMeterZero(t *testing.T) {
    86  	m := NewMeter()
    87  	if count := m.Count(); 0 != count {
    88  		t.Errorf("m.Count(): 0 != %v\n", count)
    89  	}
    90  }