ewma_test.go 7.4 KB

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