github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/timer_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  package metrics
    10  
    11  import (
    12  	"fmt"
    13  	"math"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func BenchmarkTimer(b *testing.B) {
    19  	tm := NewTimer()
    20  	b.ResetTimer()
    21  	for i := 0; i < b.N; i++ {
    22  		tm.Update(1)
    23  	}
    24  }
    25  
    26  func TestGetOrRegisterTimer(t *testing.T) {
    27  	r := NewRegistry()
    28  	NewRegisteredTimer("foo", r).Update(47)
    29  	if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() {
    30  		t.Fatal(tm)
    31  	}
    32  }
    33  
    34  func TestTimerExtremes(t *testing.T) {
    35  	tm := NewTimer()
    36  	tm.Update(math.MaxInt64)
    37  	tm.Update(0)
    38  	if stdDev := tm.StdDev(); 4.611686018427388e+18 != stdDev {
    39  		t.Errorf("tm.StdDev(): 4.611686018427388e+18 != %v\n", stdDev)
    40  	}
    41  }
    42  
    43  func TestTimerStop(t *testing.T) {
    44  	l := len(arbiter.meters)
    45  	tm := NewTimer()
    46  	if len(arbiter.meters) != l+1 {
    47  		t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
    48  	}
    49  	tm.Stop()
    50  	if len(arbiter.meters) != l {
    51  		t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
    52  	}
    53  }
    54  
    55  func TestTimerFunc(t *testing.T) {
    56  	tm := NewTimer()
    57  	tm.Time(func() { time.Sleep(50e6) })
    58  	if max := tm.Max(); 35e6 > max || max > 145e6 {
    59  		t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max)
    60  	}
    61  }
    62  
    63  func TestTimerZero(t *testing.T) {
    64  	tm := NewTimer()
    65  	if count := tm.Count(); 0 != count {
    66  		t.Errorf("tm.Count(): 0 != %v\n", count)
    67  	}
    68  	if min := tm.Min(); 0 != min {
    69  		t.Errorf("tm.Min(): 0 != %v\n", min)
    70  	}
    71  	if max := tm.Max(); 0 != max {
    72  		t.Errorf("tm.Max(): 0 != %v\n", max)
    73  	}
    74  	if mean := tm.Mean(); 0.0 != mean {
    75  		t.Errorf("tm.Mean(): 0.0 != %v\n", mean)
    76  	}
    77  	if stdDev := tm.StdDev(); 0.0 != stdDev {
    78  		t.Errorf("tm.StdDev(): 0.0 != %v\n", stdDev)
    79  	}
    80  	ps := tm.Percentiles([]float64{0.5, 0.75, 0.99})
    81  	if 0.0 != ps[0] {
    82  		t.Errorf("median: 0.0 != %v\n", ps[0])
    83  	}
    84  	if 0.0 != ps[1] {
    85  		t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
    86  	}
    87  	if 0.0 != ps[2] {
    88  		t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
    89  	}
    90  	if rate1 := tm.Rate1(); 0.0 != rate1 {
    91  		t.Errorf("tm.Rate1(): 0.0 != %v\n", rate1)
    92  	}
    93  	if rate5 := tm.Rate5(); 0.0 != rate5 {
    94  		t.Errorf("tm.Rate5(): 0.0 != %v\n", rate5)
    95  	}
    96  	if rate15 := tm.Rate15(); 0.0 != rate15 {
    97  		t.Errorf("tm.Rate15(): 0.0 != %v\n", rate15)
    98  	}
    99  	if rateMean := tm.RateMean(); 0.0 != rateMean {
   100  		t.Errorf("tm.RateMean(): 0.0 != %v\n", rateMean)
   101  	}
   102  }
   103  
   104  func ExampleGetOrRegisterTimer() {
   105  	m := "account.create.latency"
   106  	t := GetOrRegisterTimer(m, nil)
   107  	t.Update(47)
   108  fmt.Println(t.Max()) //产量:47
   109  }