github.com/codingfuture/orig-energi3@v0.8.4/metrics/histogram_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import "testing"
    21  
    22  func BenchmarkHistogram(b *testing.B) {
    23  	h := NewHistogram(NewUniformSample(100))
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		h.Update(int64(i))
    27  	}
    28  }
    29  
    30  func TestGetOrRegisterHistogram(t *testing.T) {
    31  	r := NewRegistry()
    32  	s := NewUniformSample(100)
    33  	NewRegisteredHistogram("foo", r, s).Update(47)
    34  	if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
    35  		t.Fatal(h)
    36  	}
    37  }
    38  
    39  func TestHistogram10000(t *testing.T) {
    40  	h := NewHistogram(NewUniformSample(100000))
    41  	for i := 1; i <= 10000; i++ {
    42  		h.Update(int64(i))
    43  	}
    44  	testHistogram10000(t, h)
    45  }
    46  
    47  func TestHistogramEmpty(t *testing.T) {
    48  	h := NewHistogram(NewUniformSample(100))
    49  	if count := h.Count(); 0 != count {
    50  		t.Errorf("h.Count(): 0 != %v\n", count)
    51  	}
    52  	if min := h.Min(); 0 != min {
    53  		t.Errorf("h.Min(): 0 != %v\n", min)
    54  	}
    55  	if max := h.Max(); 0 != max {
    56  		t.Errorf("h.Max(): 0 != %v\n", max)
    57  	}
    58  	if mean := h.Mean(); 0.0 != mean {
    59  		t.Errorf("h.Mean(): 0.0 != %v\n", mean)
    60  	}
    61  	if stdDev := h.StdDev(); 0.0 != stdDev {
    62  		t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
    63  	}
    64  	ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
    65  	if 0.0 != ps[0] {
    66  		t.Errorf("median: 0.0 != %v\n", ps[0])
    67  	}
    68  	if 0.0 != ps[1] {
    69  		t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
    70  	}
    71  	if 0.0 != ps[2] {
    72  		t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
    73  	}
    74  }
    75  
    76  func TestHistogramSnapshot(t *testing.T) {
    77  	h := NewHistogram(NewUniformSample(100000))
    78  	for i := 1; i <= 10000; i++ {
    79  		h.Update(int64(i))
    80  	}
    81  	snapshot := h.Snapshot()
    82  	h.Update(0)
    83  	testHistogram10000(t, snapshot)
    84  }
    85  
    86  func testHistogram10000(t *testing.T, h Histogram) {
    87  	if count := h.Count(); 10000 != count {
    88  		t.Errorf("h.Count(): 10000 != %v\n", count)
    89  	}
    90  	if min := h.Min(); 1 != min {
    91  		t.Errorf("h.Min(): 1 != %v\n", min)
    92  	}
    93  	if max := h.Max(); 10000 != max {
    94  		t.Errorf("h.Max(): 10000 != %v\n", max)
    95  	}
    96  	if mean := h.Mean(); 5000.5 != mean {
    97  		t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
    98  	}
    99  	if stdDev := h.StdDev(); 2886.751331514372 != stdDev {
   100  		t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
   101  	}
   102  	ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
   103  	if 5000.5 != ps[0] {
   104  		t.Errorf("median: 5000.5 != %v\n", ps[0])
   105  	}
   106  	if 7500.75 != ps[1] {
   107  		t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
   108  	}
   109  	if 9900.99 != ps[2] {
   110  		t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
   111  	}
   112  }