github.com/aitimate-0/go-ethereum@v1.9.7/metrics/resetting_timer_test.go (about)

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