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