github.com/theQRL/go-zond@v0.1.1/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  float64
    14  		wantP95  float64
    15  		wantP99  float64
    16  		wantMean float64
    17  		wantMin  int64
    18  		wantMax  int64
    19  	}{
    20  		{
    21  			values:  []int64{},
    22  			start:   1,
    23  			end:     11,
    24  			wantP50: 5.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.5, wantP95: 95.94999999999999, wantP99: 99.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: 5.5, wantP95: 10, wantP99: 10,
    60  			wantMin: 1, wantMax: 10, wantMean: 5.5,
    61  		},
    62  	}
    63  	for i, 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  		snap := timer.Snapshot()
    74  
    75  		ps := snap.Percentiles([]float64{0.50, 0.95, 0.99})
    76  
    77  		if have, want := snap.Min(), tt.wantMin; have != want {
    78  			t.Fatalf("%d: min: have %d, want %d", i, have, want)
    79  		}
    80  		if have, want := snap.Max(), tt.wantMax; have != want {
    81  			t.Fatalf("%d: max: have %d, want %d", i, have, want)
    82  		}
    83  		if have, want := snap.Mean(), tt.wantMean; have != want {
    84  			t.Fatalf("%d: mean: have %v, want %v", i, have, want)
    85  		}
    86  		if have, want := ps[0], tt.wantP50; have != want {
    87  			t.Errorf("%d: p50: have %v, want %v", i, have, want)
    88  		}
    89  		if have, want := ps[1], tt.wantP95; have != want {
    90  			t.Errorf("%d: p95: have %v, want %v", i, have, want)
    91  		}
    92  		if have, want := ps[2], tt.wantP99; have != want {
    93  			t.Errorf("%d: p99: have %v, want %v", i, have, want)
    94  		}
    95  	}
    96  }
    97  
    98  func TestResettingTimerWithFivePercentiles(t *testing.T) {
    99  	tests := []struct {
   100  		values   []int64
   101  		start    int
   102  		end      int
   103  		wantP05  float64
   104  		wantP20  float64
   105  		wantP50  float64
   106  		wantP95  float64
   107  		wantP99  float64
   108  		wantMean float64
   109  		wantMin  int64
   110  		wantMax  int64
   111  	}{
   112  		{
   113  			values:  []int64{},
   114  			start:   1,
   115  			end:     11,
   116  			wantP05: 1, wantP20: 2.2, wantP50: 5.5, wantP95: 10, wantP99: 10,
   117  			wantMin: 1, wantMax: 10, wantMean: 5.5,
   118  		},
   119  		{
   120  			values:  []int64{},
   121  			start:   1,
   122  			end:     101,
   123  			wantP05: 5.050000000000001, wantP20: 20.200000000000003, wantP50: 50.5, wantP95: 95.94999999999999, wantP99: 99.99,
   124  			wantMin: 1, wantMax: 100, wantMean: 50.5,
   125  		},
   126  		{
   127  			values:  []int64{1},
   128  			start:   0,
   129  			end:     0,
   130  			wantP05: 1, wantP20: 1, wantP50: 1, wantP95: 1, wantP99: 1,
   131  			wantMin: 1, wantMax: 1, wantMean: 1,
   132  		},
   133  		{
   134  			values:  []int64{0},
   135  			start:   0,
   136  			end:     0,
   137  			wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
   138  			wantMin: 0, wantMax: 0, wantMean: 0,
   139  		},
   140  		{
   141  			values:  []int64{},
   142  			start:   0,
   143  			end:     0,
   144  			wantP05: 0, wantP20: 0, wantP50: 0, wantP95: 0, wantP99: 0,
   145  			wantMin: 0, wantMax: 0, wantMean: 0,
   146  		},
   147  		{
   148  			values:  []int64{1, 10},
   149  			start:   0,
   150  			end:     0,
   151  			wantP05: 1, wantP20: 1, wantP50: 5.5, wantP95: 10, wantP99: 10,
   152  			wantMin: 1, wantMax: 10, wantMean: 5.5,
   153  		},
   154  	}
   155  	for ind, tt := range tests {
   156  		timer := NewResettingTimer()
   157  
   158  		for i := tt.start; i < tt.end; i++ {
   159  			tt.values = append(tt.values, int64(i))
   160  		}
   161  
   162  		for _, v := range tt.values {
   163  			timer.Update(time.Duration(v))
   164  		}
   165  
   166  		snap := timer.Snapshot()
   167  
   168  		ps := snap.Percentiles([]float64{0.05, 0.20, 0.50, 0.95, 0.99})
   169  
   170  		if tt.wantMin != snap.Min() {
   171  			t.Errorf("%d: min: got %d, want %d", ind, snap.Min(), tt.wantMin)
   172  		}
   173  
   174  		if tt.wantMax != snap.Max() {
   175  			t.Errorf("%d: max: got %d, want %d", ind, snap.Max(), tt.wantMax)
   176  		}
   177  
   178  		if tt.wantMean != snap.Mean() {
   179  			t.Errorf("%d: mean: got %.2f, want %.2f", ind, snap.Mean(), tt.wantMean)
   180  		}
   181  		if tt.wantP05 != ps[0] {
   182  			t.Errorf("%d: p05: got %v, want %v", ind, ps[0], tt.wantP05)
   183  		}
   184  		if tt.wantP20 != ps[1] {
   185  			t.Errorf("%d: p20: got %v, want %v", ind, ps[1], tt.wantP20)
   186  		}
   187  		if tt.wantP50 != ps[2] {
   188  			t.Errorf("%d: p50: got %v, want %v", ind, ps[2], tt.wantP50)
   189  		}
   190  		if tt.wantP95 != ps[3] {
   191  			t.Errorf("%d: p95: got %v, want %v", ind, ps[3], tt.wantP95)
   192  		}
   193  		if tt.wantP99 != ps[4] {
   194  			t.Errorf("%d: p99: got %v, want %v", ind, ps[4], tt.wantP99)
   195  		}
   196  	}
   197  }