github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/histogram_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 "testing"
    12  
    13  func BenchmarkHistogram(b *testing.B) {
    14  	h := NewHistogram(NewUniformSample(100))
    15  	b.ResetTimer()
    16  	for i := 0; i < b.N; i++ {
    17  		h.Update(int64(i))
    18  	}
    19  }
    20  
    21  func TestGetOrRegisterHistogram(t *testing.T) {
    22  	r := NewRegistry()
    23  	s := NewUniformSample(100)
    24  	NewRegisteredHistogram("foo", r, s).Update(47)
    25  	if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
    26  		t.Fatal(h)
    27  	}
    28  }
    29  
    30  func TestHistogram10000(t *testing.T) {
    31  	h := NewHistogram(NewUniformSample(100000))
    32  	for i := 1; i <= 10000; i++ {
    33  		h.Update(int64(i))
    34  	}
    35  	testHistogram10000(t, h)
    36  }
    37  
    38  func TestHistogramEmpty(t *testing.T) {
    39  	h := NewHistogram(NewUniformSample(100))
    40  	if count := h.Count(); 0 != count {
    41  		t.Errorf("h.Count(): 0 != %v\n", count)
    42  	}
    43  	if min := h.Min(); 0 != min {
    44  		t.Errorf("h.Min(): 0 != %v\n", min)
    45  	}
    46  	if max := h.Max(); 0 != max {
    47  		t.Errorf("h.Max(): 0 != %v\n", max)
    48  	}
    49  	if mean := h.Mean(); 0.0 != mean {
    50  		t.Errorf("h.Mean(): 0.0 != %v\n", mean)
    51  	}
    52  	if stdDev := h.StdDev(); 0.0 != stdDev {
    53  		t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
    54  	}
    55  	ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
    56  	if 0.0 != ps[0] {
    57  		t.Errorf("median: 0.0 != %v\n", ps[0])
    58  	}
    59  	if 0.0 != ps[1] {
    60  		t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
    61  	}
    62  	if 0.0 != ps[2] {
    63  		t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
    64  	}
    65  }
    66  
    67  func TestHistogramSnapshot(t *testing.T) {
    68  	h := NewHistogram(NewUniformSample(100000))
    69  	for i := 1; i <= 10000; i++ {
    70  		h.Update(int64(i))
    71  	}
    72  	snapshot := h.Snapshot()
    73  	h.Update(0)
    74  	testHistogram10000(t, snapshot)
    75  }
    76  
    77  func testHistogram10000(t *testing.T, h Histogram) {
    78  	if count := h.Count(); 10000 != count {
    79  		t.Errorf("h.Count(): 10000 != %v\n", count)
    80  	}
    81  	if min := h.Min(); 1 != min {
    82  		t.Errorf("h.Min(): 1 != %v\n", min)
    83  	}
    84  	if max := h.Max(); 10000 != max {
    85  		t.Errorf("h.Max(): 10000 != %v\n", max)
    86  	}
    87  	if mean := h.Mean(); 5000.5 != mean {
    88  		t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
    89  	}
    90  	if stdDev := h.StdDev(); 2886.751331514372 != stdDev {
    91  		t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
    92  	}
    93  	ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
    94  	if 5000.5 != ps[0] {
    95  		t.Errorf("median: 5000.5 != %v\n", ps[0])
    96  	}
    97  	if 7500.75 != ps[1] {
    98  		t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
    99  	}
   100  	if 9900.99 != ps[2] {
   101  		t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
   102  	}
   103  }