gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/meter_test.go (about)

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