gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/timer_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  	"fmt"
    21  	"math"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func BenchmarkTimer(b *testing.B) {
    27  	tm := NewTimer()
    28  	b.ResetTimer()
    29  	for i := 0; i < b.N; i++ {
    30  		tm.Update(1)
    31  	}
    32  }
    33  
    34  func TestGetOrRegisterTimer(t *testing.T) {
    35  	r := NewRegistry()
    36  	NewRegisteredTimer("foo", r).Update(47)
    37  	if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() {
    38  		t.Fatal(tm)
    39  	}
    40  }
    41  
    42  func TestTimerExtremes(t *testing.T) {
    43  	tm := NewTimer()
    44  	tm.Update(math.MaxInt64)
    45  	tm.Update(0)
    46  	if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev {
    47  		t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev)
    48  	}
    49  }
    50  
    51  func TestTimerStop(t *testing.T) {
    52  	l := len(arbiter.meters)
    53  	tm := NewTimer()
    54  	if len(arbiter.meters) != l+1 {
    55  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    56  	}
    57  	tm.Stop()
    58  	if len(arbiter.meters) != l {
    59  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    60  	}
    61  }
    62  
    63  func TestTimerFunc(t *testing.T) {
    64  	tm := NewTimer()
    65  	tm.Time(func() { time.Sleep(50e6) })
    66  	if max := tm.Max(); 50e6 > max || max > 65e6 {
    67  		t.Errorf("tm.Max(): 45e6 > %v || %v > 55e6\n", max, max)
    68  	}
    69  }
    70  
    71  func TestTimerZero(t *testing.T) {
    72  	tm := NewTimer()
    73  	if count := tm.Count(); 0 != count {
    74  		t.Errorf("tm.Count(): 0 != %v\n", count)
    75  	}
    76  	if min := tm.Min(); 0 != min {
    77  		t.Errorf("tm.Min(): 0 != %v\n", min)
    78  	}
    79  	if max := tm.Max(); 0 != max {
    80  		t.Errorf("tm.Max(): 0 != %v\n", max)
    81  	}
    82  	if mean := tm.Mean(); 0.0 != mean {
    83  		t.Errorf("tm.Mean(): 0.0 != %v\n", mean)
    84  	}
    85  	if stdDev := tm.StdDev(); 0.0 != stdDev {
    86  		t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev)
    87  	}
    88  	ps := tm.Percentiles([]float64{0.5, 0.75, 0.99})
    89  	if 0.0 != ps[0] {
    90  		t.Errorf("median: 0.0 != %v\n", ps[0])
    91  	}
    92  	if 0.0 != ps[1] {
    93  		t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
    94  	}
    95  	if 0.0 != ps[2] {
    96  		t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
    97  	}
    98  	if rate1 := tm.Rate1(); 0.0 != rate1 {
    99  		t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1)
   100  	}
   101  	if rate5 := tm.Rate5(); 0.0 != rate5 {
   102  		t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5)
   103  	}
   104  	if rate15 := tm.Rate15(); 0.0 != rate15 {
   105  		t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15)
   106  	}
   107  	if rateMean := tm.RateMean(); 0.0 != rateMean {
   108  		t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean)
   109  	}
   110  }
   111  
   112  func ExampleGetOrRegisterTimer() {
   113  	m := "account.create.latency"
   114  	t := GetOrRegisterTimer(m, nil)
   115  	t.Update(47)
   116  	fmt.Println(t.Max()) // Output: 47
   117  }