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