github.com/civet148/go-ethereum@v1.9.7/metrics/timer_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func BenchmarkTimer(b *testing.B) {
    11  	tm := NewTimer()
    12  	b.ResetTimer()
    13  	for i := 0; i < b.N; i++ {
    14  		tm.Update(1)
    15  	}
    16  }
    17  
    18  func TestGetOrRegisterTimer(t *testing.T) {
    19  	r := NewRegistry()
    20  	NewRegisteredTimer("foo", r).Update(47)
    21  	if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() {
    22  		t.Fatal(tm)
    23  	}
    24  }
    25  
    26  func TestTimerExtremes(t *testing.T) {
    27  	tm := NewTimer()
    28  	tm.Update(math.MaxInt64)
    29  	tm.Update(0)
    30  	if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev {
    31  		t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev)
    32  	}
    33  }
    34  
    35  func TestTimerStop(t *testing.T) {
    36  	l := len(arbiter.meters)
    37  	tm := NewTimer()
    38  	if len(arbiter.meters) != l+1 {
    39  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    40  	}
    41  	tm.Stop()
    42  	if len(arbiter.meters) != l {
    43  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    44  	}
    45  }
    46  
    47  func TestTimerFunc(t *testing.T) {
    48  	tm := NewTimer()
    49  	tm.Time(func() { time.Sleep(50e6) })
    50  	if max := tm.Max(); 35e6 > max || max > 145e6 {
    51  		t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max)
    52  	}
    53  }
    54  
    55  func TestTimerZero(t *testing.T) {
    56  	tm := NewTimer()
    57  	if count := tm.Count(); 0 != count {
    58  		t.Errorf("tm.Count(): 0 != %v\n", count)
    59  	}
    60  	if min := tm.Min(); 0 != min {
    61  		t.Errorf("tm.Min(): 0 != %v\n", min)
    62  	}
    63  	if max := tm.Max(); 0 != max {
    64  		t.Errorf("tm.Max(): 0 != %v\n", max)
    65  	}
    66  	if mean := tm.Mean(); 0.0 != mean {
    67  		t.Errorf("tm.Mean(): 0.0 != %v\n", mean)
    68  	}
    69  	if stdDev := tm.StdDev(); 0.0 != stdDev {
    70  		t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev)
    71  	}
    72  	ps := tm.Percentiles([]float64{0.5, 0.75, 0.99})
    73  	if 0.0 != ps[0] {
    74  		t.Errorf("median: 0.0 != %v\n", ps[0])
    75  	}
    76  	if 0.0 != ps[1] {
    77  		t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
    78  	}
    79  	if 0.0 != ps[2] {
    80  		t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
    81  	}
    82  	if rate1 := tm.Rate1(); 0.0 != rate1 {
    83  		t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1)
    84  	}
    85  	if rate5 := tm.Rate5(); 0.0 != rate5 {
    86  		t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5)
    87  	}
    88  	if rate15 := tm.Rate15(); 0.0 != rate15 {
    89  		t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15)
    90  	}
    91  	if rateMean := tm.RateMean(); 0.0 != rateMean {
    92  		t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean)
    93  	}
    94  }
    95  
    96  func ExampleGetOrRegisterTimer() {
    97  	m := "account.create.latency"
    98  	t := GetOrRegisterTimer(m, nil)
    99  	t.Update(47)
   100  	fmt.Println(t.Max()) // Output: 47
   101  }