github.com/codingfuture/orig-energi3@v0.8.4/metrics/resetting_timer_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 (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestResettingTimer(t *testing.T) {
    26  	tests := []struct {
    27  		values   []int64
    28  		start    int
    29  		end      int
    30  		wantP50  int64
    31  		wantP95  int64
    32  		wantP99  int64
    33  		wantMean float64
    34  		wantMin  int64
    35  		wantMax  int64
    36  	}{
    37  		{
    38  			values:  []int64{},
    39  			start:   1,
    40  			end:     11,
    41  			wantP50: 5, wantP95: 10, wantP99: 10,
    42  			wantMin: 1, wantMax: 10, wantMean: 5.5,
    43  		},
    44  		{
    45  			values:  []int64{},
    46  			start:   1,
    47  			end:     101,
    48  			wantP50: 50, wantP95: 95, wantP99: 99,
    49  			wantMin: 1, wantMax: 100, wantMean: 50.5,
    50  		},
    51  		{
    52  			values:  []int64{1},
    53  			start:   0,
    54  			end:     0,
    55  			wantP50: 1, wantP95: 1, wantP99: 1,
    56  			wantMin: 1, wantMax: 1, wantMean: 1,
    57  		},
    58  		{
    59  			values:  []int64{0},
    60  			start:   0,
    61  			end:     0,
    62  			wantP50: 0, wantP95: 0, wantP99: 0,
    63  			wantMin: 0, wantMax: 0, wantMean: 0,
    64  		},
    65  		{
    66  			values:  []int64{},
    67  			start:   0,
    68  			end:     0,
    69  			wantP50: 0, wantP95: 0, wantP99: 0,
    70  			wantMin: 0, wantMax: 0, wantMean: 0,
    71  		},
    72  		{
    73  			values:  []int64{1, 10},
    74  			start:   0,
    75  			end:     0,
    76  			wantP50: 1, wantP95: 10, wantP99: 10,
    77  			wantMin: 1, wantMax: 10, wantMean: 5.5,
    78  		},
    79  	}
    80  	for ind, tt := range tests {
    81  		timer := NewResettingTimer()
    82  
    83  		for i := tt.start; i < tt.end; i++ {
    84  			tt.values = append(tt.values, int64(i))
    85  		}
    86  
    87  		for _, v := range tt.values {
    88  			timer.Update(time.Duration(v))
    89  		}
    90  
    91  		snap := timer.Snapshot()
    92  
    93  		ps := snap.Percentiles([]float64{50, 95, 99})
    94  
    95  		val := snap.Values()
    96  
    97  		if len(val) > 0 {
    98  			if tt.wantMin != val[0] {
    99  				t.Fatalf("%d: min: got %d, want %d", ind, val[0], tt.wantMin)
   100  			}
   101  
   102  			if tt.wantMax != val[len(val)-1] {
   103  				t.Fatalf("%d: max: got %d, want %d", ind, val[len(val)-1], tt.wantMax)
   104  			}
   105  		}
   106  
   107  		if tt.wantMean != snap.Mean() {
   108  			t.Fatalf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean)
   109  		}
   110  
   111  		if tt.wantP50 != ps[0] {
   112  			t.Fatalf("%d: p50: got %d, want %d", ind, ps[0], tt.wantP50)
   113  		}
   114  
   115  		if tt.wantP95 != ps[1] {
   116  			t.Fatalf("%d: p95: got %d, want %d", ind, ps[1], tt.wantP95)
   117  		}
   118  
   119  		if tt.wantP99 != ps[2] {
   120  			t.Fatalf("%d: p99: got %d, want %d", ind, ps[2], tt.wantP99)
   121  		}
   122  	}
   123  }
   124  
   125  func TestResettingTimerWithFivePercentiles(t *testing.T) {
   126  	tests := []struct {
   127  		values   []int64
   128  		start    int
   129  		end      int
   130  		wantP05  int64
   131  		wantP20  int64
   132  		wantP50  int64
   133  		wantP95  int64
   134  		wantP99  int64
   135  		wantMean float64
   136  		wantMin  int64
   137  		wantMax  int64
   138  	}{
   139  		{
   140  			values:  []int64{},
   141  			start:   1,
   142  			end:     11,
   143  			wantP05: 1, wantP20: 2, wantP50: 5, wantP95: 10, wantP99: 10,
   144  			wantMin: 1, wantMax: 10, wantMean: 5.5,
   145  		},
   146  		{
   147  			values:  []int64{},
   148  			start:   1,
   149  			end:     101,
   150  			wantP05: 5, wantP20: 20, wantP50: 50, wantP95: 95, wantP99: 99,
   151  			wantMin: 1, wantMax: 100, wantMean: 50.5,
   152  		},
   153  		{
   154  			values:  []int64{1},
   155  			start:   0,
   156  			end:     0,
   157  			wantP05: 1, wantP20: 1, wantP50: 1, wantP95: 1, wantP99: 1,
   158  			wantMin: 1, wantMax: 1, wantMean: 1,
   159  		},
   160  		{
   161  			values:  []int64{0},
   162  			start:   0,
   163  			end:     0,
   164  			wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
   165  			wantMin: 0, wantMax: 0, wantMean: 0,
   166  		},
   167  		{
   168  			values:  []int64{},
   169  			start:   0,
   170  			end:     0,
   171  			wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
   172  			wantMin: 0, wantMax: 0, wantMean: 0,
   173  		},
   174  		{
   175  			values:  []int64{1, 10},
   176  			start:   0,
   177  			end:     0,
   178  			wantP05: 1, wantP20: 1, wantP50: 1, wantP95: 10, wantP99: 10,
   179  			wantMin: 1, wantMax: 10, wantMean: 5.5,
   180  		},
   181  	}
   182  	for ind, tt := range tests {
   183  		timer := NewResettingTimer()
   184  
   185  		for i := tt.start; i < tt.end; i++ {
   186  			tt.values = append(tt.values, int64(i))
   187  		}
   188  
   189  		for _, v := range tt.values {
   190  			timer.Update(time.Duration(v))
   191  		}
   192  
   193  		snap := timer.Snapshot()
   194  
   195  		ps := snap.Percentiles([]float64{5, 20, 50, 95, 99})
   196  
   197  		val := snap.Values()
   198  
   199  		if len(val) > 0 {
   200  			if tt.wantMin != val[0] {
   201  				t.Fatalf("%d: min: got %d, want %d", ind, val[0], tt.wantMin)
   202  			}
   203  
   204  			if tt.wantMax != val[len(val)-1] {
   205  				t.Fatalf("%d: max: got %d, want %d", ind, val[len(val)-1], tt.wantMax)
   206  			}
   207  		}
   208  
   209  		if tt.wantMean != snap.Mean() {
   210  			t.Fatalf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean)
   211  		}
   212  
   213  		if tt.wantP05 != ps[0] {
   214  			t.Fatalf("%d: p05: got %d, want %d", ind, ps[0], tt.wantP05)
   215  		}
   216  
   217  		if tt.wantP20 != ps[1] {
   218  			t.Fatalf("%d: p20: got %d, want %d", ind, ps[1], tt.wantP20)
   219  		}
   220  
   221  		if tt.wantP50 != ps[2] {
   222  			t.Fatalf("%d: p50: got %d, want %d", ind, ps[2], tt.wantP50)
   223  		}
   224  
   225  		if tt.wantP95 != ps[3] {
   226  			t.Fatalf("%d: p95: got %d, want %d", ind, ps[3], tt.wantP95)
   227  		}
   228  
   229  		if tt.wantP99 != ps[4] {
   230  			t.Fatalf("%d: p99: got %d, want %d", ind, ps[4], tt.wantP99)
   231  		}
   232  	}
   233  }