github.com/zjj1991/quorum@v0.0.0-20190524123704-ae4b0a1e1a19/metrics/ewma_test.go (about) 1 package metrics 2 3 import "testing" 4 5 func BenchmarkEWMA(b *testing.B) { 6 a := NewEWMA1() 7 b.ResetTimer() 8 for i := 0; i < b.N; i++ { 9 a.Update(1) 10 a.Tick() 11 } 12 } 13 14 func TestEWMA1(t *testing.T) { 15 a := NewEWMA1() 16 a.Update(3) 17 a.Tick() 18 if rate := a.Rate(); 0.6 != rate { 19 t.Errorf("initial a.Rate(): 0.6 != %v\n", rate) 20 } 21 elapseMinute(a) 22 if rate := a.Rate(); 0.22072766470286553 != rate { 23 t.Errorf("1 minute a.Rate(): 0.22072766470286553 != %v\n", rate) 24 } 25 elapseMinute(a) 26 if rate := a.Rate(); 0.08120116994196772 != rate { 27 t.Errorf("2 minute a.Rate(): 0.08120116994196772 != %v\n", rate) 28 } 29 elapseMinute(a) 30 if rate := a.Rate(); 0.029872241020718428 != rate { 31 t.Errorf("3 minute a.Rate(): 0.029872241020718428 != %v\n", rate) 32 } 33 elapseMinute(a) 34 if rate := a.Rate(); 0.01098938333324054 != rate { 35 t.Errorf("4 minute a.Rate(): 0.01098938333324054 != %v\n", rate) 36 } 37 elapseMinute(a) 38 if rate := a.Rate(); 0.004042768199451294 != rate { 39 t.Errorf("5 minute a.Rate(): 0.004042768199451294 != %v\n", rate) 40 } 41 elapseMinute(a) 42 if rate := a.Rate(); 0.0014872513059998212 != rate { 43 t.Errorf("6 minute a.Rate(): 0.0014872513059998212 != %v\n", rate) 44 } 45 elapseMinute(a) 46 if rate := a.Rate(); 0.0005471291793327122 != rate { 47 t.Errorf("7 minute a.Rate(): 0.0005471291793327122 != %v\n", rate) 48 } 49 elapseMinute(a) 50 if rate := a.Rate(); 0.00020127757674150815 != rate { 51 t.Errorf("8 minute a.Rate(): 0.00020127757674150815 != %v\n", rate) 52 } 53 elapseMinute(a) 54 if rate := a.Rate(); 7.404588245200814e-05 != rate { 55 t.Errorf("9 minute a.Rate(): 7.404588245200814e-05 != %v\n", rate) 56 } 57 elapseMinute(a) 58 if rate := a.Rate(); 2.7239957857491083e-05 != rate { 59 t.Errorf("10 minute a.Rate(): 2.7239957857491083e-05 != %v\n", rate) 60 } 61 elapseMinute(a) 62 if rate := a.Rate(); 1.0021020474147462e-05 != rate { 63 t.Errorf("11 minute a.Rate(): 1.0021020474147462e-05 != %v\n", rate) 64 } 65 elapseMinute(a) 66 if rate := a.Rate(); 3.6865274119969525e-06 != rate { 67 t.Errorf("12 minute a.Rate(): 3.6865274119969525e-06 != %v\n", rate) 68 } 69 elapseMinute(a) 70 if rate := a.Rate(); 1.3561976441886433e-06 != rate { 71 t.Errorf("13 minute a.Rate(): 1.3561976441886433e-06 != %v\n", rate) 72 } 73 elapseMinute(a) 74 if rate := a.Rate(); 4.989172314621449e-07 != rate { 75 t.Errorf("14 minute a.Rate(): 4.989172314621449e-07 != %v\n", rate) 76 } 77 elapseMinute(a) 78 if rate := a.Rate(); 1.8354139230109722e-07 != rate { 79 t.Errorf("15 minute a.Rate(): 1.8354139230109722e-07 != %v\n", rate) 80 } 81 } 82 83 func TestEWMA5(t *testing.T) { 84 a := NewEWMA5() 85 a.Update(3) 86 a.Tick() 87 if rate := a.Rate(); 0.6 != rate { 88 t.Errorf("initial a.Rate(): 0.6 != %v\n", rate) 89 } 90 elapseMinute(a) 91 if rate := a.Rate(); 0.49123845184678905 != rate { 92 t.Errorf("1 minute a.Rate(): 0.49123845184678905 != %v\n", rate) 93 } 94 elapseMinute(a) 95 if rate := a.Rate(); 0.4021920276213837 != rate { 96 t.Errorf("2 minute a.Rate(): 0.4021920276213837 != %v\n", rate) 97 } 98 elapseMinute(a) 99 if rate := a.Rate(); 0.32928698165641596 != rate { 100 t.Errorf("3 minute a.Rate(): 0.32928698165641596 != %v\n", rate) 101 } 102 elapseMinute(a) 103 if rate := a.Rate(); 0.269597378470333 != rate { 104 t.Errorf("4 minute a.Rate(): 0.269597378470333 != %v\n", rate) 105 } 106 elapseMinute(a) 107 if rate := a.Rate(); 0.2207276647028654 != rate { 108 t.Errorf("5 minute a.Rate(): 0.2207276647028654 != %v\n", rate) 109 } 110 elapseMinute(a) 111 if rate := a.Rate(); 0.18071652714732128 != rate { 112 t.Errorf("6 minute a.Rate(): 0.18071652714732128 != %v\n", rate) 113 } 114 elapseMinute(a) 115 if rate := a.Rate(); 0.14795817836496392 != rate { 116 t.Errorf("7 minute a.Rate(): 0.14795817836496392 != %v\n", rate) 117 } 118 elapseMinute(a) 119 if rate := a.Rate(); 0.12113791079679326 != rate { 120 t.Errorf("8 minute a.Rate(): 0.12113791079679326 != %v\n", rate) 121 } 122 elapseMinute(a) 123 if rate := a.Rate(); 0.09917933293295193 != rate { 124 t.Errorf("9 minute a.Rate(): 0.09917933293295193 != %v\n", rate) 125 } 126 elapseMinute(a) 127 if rate := a.Rate(); 0.08120116994196763 != rate { 128 t.Errorf("10 minute a.Rate(): 0.08120116994196763 != %v\n", rate) 129 } 130 elapseMinute(a) 131 if rate := a.Rate(); 0.06648189501740036 != rate { 132 t.Errorf("11 minute a.Rate(): 0.06648189501740036 != %v\n", rate) 133 } 134 elapseMinute(a) 135 if rate := a.Rate(); 0.05443077197364752 != rate { 136 t.Errorf("12 minute a.Rate(): 0.05443077197364752 != %v\n", rate) 137 } 138 elapseMinute(a) 139 if rate := a.Rate(); 0.04456414692860035 != rate { 140 t.Errorf("13 minute a.Rate(): 0.04456414692860035 != %v\n", rate) 141 } 142 elapseMinute(a) 143 if rate := a.Rate(); 0.03648603757513079 != rate { 144 t.Errorf("14 minute a.Rate(): 0.03648603757513079 != %v\n", rate) 145 } 146 elapseMinute(a) 147 if rate := a.Rate(); 0.0298722410207183831020718428 != rate { 148 t.Errorf("15 minute a.Rate(): 0.0298722410207183831020718428 != %v\n", rate) 149 } 150 } 151 152 func TestEWMA15(t *testing.T) { 153 a := NewEWMA15() 154 a.Update(3) 155 a.Tick() 156 if rate := a.Rate(); 0.6 != rate { 157 t.Errorf("initial a.Rate(): 0.6 != %v\n", rate) 158 } 159 elapseMinute(a) 160 if rate := a.Rate(); 0.5613041910189706 != rate { 161 t.Errorf("1 minute a.Rate(): 0.5613041910189706 != %v\n", rate) 162 } 163 elapseMinute(a) 164 if rate := a.Rate(); 0.5251039914257684 != rate { 165 t.Errorf("2 minute a.Rate(): 0.5251039914257684 != %v\n", rate) 166 } 167 elapseMinute(a) 168 if rate := a.Rate(); 0.4912384518467888184678905 != rate { 169 t.Errorf("3 minute a.Rate(): 0.4912384518467888184678905 != %v\n", rate) 170 } 171 elapseMinute(a) 172 if rate := a.Rate(); 0.459557003018789 != rate { 173 t.Errorf("4 minute a.Rate(): 0.459557003018789 != %v\n", rate) 174 } 175 elapseMinute(a) 176 if rate := a.Rate(); 0.4299187863442732 != rate { 177 t.Errorf("5 minute a.Rate(): 0.4299187863442732 != %v\n", rate) 178 } 179 elapseMinute(a) 180 if rate := a.Rate(); 0.4021920276213831 != rate { 181 t.Errorf("6 minute a.Rate(): 0.4021920276213831 != %v\n", rate) 182 } 183 elapseMinute(a) 184 if rate := a.Rate(); 0.37625345116383313 != rate { 185 t.Errorf("7 minute a.Rate(): 0.37625345116383313 != %v\n", rate) 186 } 187 elapseMinute(a) 188 if rate := a.Rate(); 0.3519877317060185 != rate { 189 t.Errorf("8 minute a.Rate(): 0.3519877317060185 != %v\n", rate) 190 } 191 elapseMinute(a) 192 if rate := a.Rate(); 0.3292869816564153165641596 != rate { 193 t.Errorf("9 minute a.Rate(): 0.3292869816564153165641596 != %v\n", rate) 194 } 195 elapseMinute(a) 196 if rate := a.Rate(); 0.3080502714195546 != rate { 197 t.Errorf("10 minute a.Rate(): 0.3080502714195546 != %v\n", rate) 198 } 199 elapseMinute(a) 200 if rate := a.Rate(); 0.2881831806538789 != rate { 201 t.Errorf("11 minute a.Rate(): 0.2881831806538789 != %v\n", rate) 202 } 203 elapseMinute(a) 204 if rate := a.Rate(); 0.26959737847033216 != rate { 205 t.Errorf("12 minute a.Rate(): 0.26959737847033216 != %v\n", rate) 206 } 207 elapseMinute(a) 208 if rate := a.Rate(); 0.2522102307052083 != rate { 209 t.Errorf("13 minute a.Rate(): 0.2522102307052083 != %v\n", rate) 210 } 211 elapseMinute(a) 212 if rate := a.Rate(); 0.23594443252115815 != rate { 213 t.Errorf("14 minute a.Rate(): 0.23594443252115815 != %v\n", rate) 214 } 215 elapseMinute(a) 216 if rate := a.Rate(); 0.2207276647028646247028654470286553 != rate { 217 t.Errorf("15 minute a.Rate(): 0.2207276647028646247028654470286553 != %v\n", rate) 218 } 219 } 220 221 func elapseMinute(a EWMA) { 222 for i := 0; i < 12; i++ { 223 a.Tick() 224 } 225 }