sample_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package metrics
  2. import (
  3. "math/rand"
  4. "runtime"
  5. "testing"
  6. "time"
  7. )
  8. // Benchmark{Compute,Copy}{1000,1000000} demonstrate that, even for relatively
  9. // expensive computations like Variance, the cost of copying the Sample, as
  10. // approximated by a make and copy, is much greater than the cost of the
  11. // computation for small samples and only slightly less for large samples.
  12. func BenchmarkCompute1000(b *testing.B) {
  13. s := make([]int64, 1000)
  14. for i := 0; i < len(s); i++ {
  15. s[i] = int64(i)
  16. }
  17. b.ResetTimer()
  18. for i := 0; i < b.N; i++ {
  19. SampleVariance(s)
  20. }
  21. }
  22. func BenchmarkCompute1000000(b *testing.B) {
  23. s := make([]int64, 1000000)
  24. for i := 0; i < len(s); i++ {
  25. s[i] = int64(i)
  26. }
  27. b.ResetTimer()
  28. for i := 0; i < b.N; i++ {
  29. SampleVariance(s)
  30. }
  31. }
  32. func BenchmarkCopy1000(b *testing.B) {
  33. s := make([]int64, 1000)
  34. for i := 0; i < len(s); i++ {
  35. s[i] = int64(i)
  36. }
  37. b.ResetTimer()
  38. for i := 0; i < b.N; i++ {
  39. sCopy := make([]int64, len(s))
  40. copy(sCopy, s)
  41. }
  42. }
  43. func BenchmarkCopy1000000(b *testing.B) {
  44. s := make([]int64, 1000000)
  45. for i := 0; i < len(s); i++ {
  46. s[i] = int64(i)
  47. }
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. sCopy := make([]int64, len(s))
  51. copy(sCopy, s)
  52. }
  53. }
  54. func BenchmarkExpDecaySample257(b *testing.B) {
  55. benchmarkSample(b, NewExpDecaySample(257, 0.015))
  56. }
  57. func BenchmarkExpDecaySample514(b *testing.B) {
  58. benchmarkSample(b, NewExpDecaySample(514, 0.015))
  59. }
  60. func BenchmarkExpDecaySample1028(b *testing.B) {
  61. benchmarkSample(b, NewExpDecaySample(1028, 0.015))
  62. }
  63. func BenchmarkUniformSample257(b *testing.B) {
  64. benchmarkSample(b, NewUniformSample(257))
  65. }
  66. func BenchmarkUniformSample514(b *testing.B) {
  67. benchmarkSample(b, NewUniformSample(514))
  68. }
  69. func BenchmarkUniformSample1028(b *testing.B) {
  70. benchmarkSample(b, NewUniformSample(1028))
  71. }
  72. func TestExpDecaySample10(t *testing.T) {
  73. rand.Seed(1)
  74. s := NewExpDecaySample(100, 0.99)
  75. for i := 0; i < 10; i++ {
  76. s.Update(int64(i))
  77. }
  78. if size := s.Count(); 10 != size {
  79. t.Errorf("s.Count(): 10 != %v\n", size)
  80. }
  81. if size := s.Size(); 10 != size {
  82. t.Errorf("s.Size(): 10 != %v\n", size)
  83. }
  84. if l := len(s.Values()); 10 != l {
  85. t.Errorf("len(s.Values()): 10 != %v\n", l)
  86. }
  87. for _, v := range s.Values() {
  88. if v > 10 || v < 0 {
  89. t.Errorf("out of range [0, 10): %v\n", v)
  90. }
  91. }
  92. }
  93. func TestExpDecaySample100(t *testing.T) {
  94. rand.Seed(1)
  95. s := NewExpDecaySample(1000, 0.01)
  96. for i := 0; i < 100; i++ {
  97. s.Update(int64(i))
  98. }
  99. if size := s.Count(); 100 != size {
  100. t.Errorf("s.Count(): 100 != %v\n", size)
  101. }
  102. if size := s.Size(); 100 != size {
  103. t.Errorf("s.Size(): 100 != %v\n", size)
  104. }
  105. if l := len(s.Values()); 100 != l {
  106. t.Errorf("len(s.Values()): 100 != %v\n", l)
  107. }
  108. for _, v := range s.Values() {
  109. if v > 100 || v < 0 {
  110. t.Errorf("out of range [0, 100): %v\n", v)
  111. }
  112. }
  113. }
  114. func TestExpDecaySample1000(t *testing.T) {
  115. rand.Seed(1)
  116. s := NewExpDecaySample(100, 0.99)
  117. for i := 0; i < 1000; i++ {
  118. s.Update(int64(i))
  119. }
  120. if size := s.Count(); 1000 != size {
  121. t.Errorf("s.Count(): 1000 != %v\n", size)
  122. }
  123. if size := s.Size(); 100 != size {
  124. t.Errorf("s.Size(): 100 != %v\n", size)
  125. }
  126. if l := len(s.Values()); 100 != l {
  127. t.Errorf("len(s.Values()): 100 != %v\n", l)
  128. }
  129. for _, v := range s.Values() {
  130. if v > 1000 || v < 0 {
  131. t.Errorf("out of range [0, 1000): %v\n", v)
  132. }
  133. }
  134. }
  135. // This test makes sure that the sample's priority is not amplified by using
  136. // nanosecond duration since start rather than second duration since start.
  137. // The priority becomes +Inf quickly after starting if this is done,
  138. // effectively freezing the set of samples until a rescale step happens.
  139. func TestExpDecaySampleNanosecondRegression(t *testing.T) {
  140. rand.Seed(1)
  141. s := NewExpDecaySample(100, 0.99)
  142. for i := 0; i < 100; i++ {
  143. s.Update(10)
  144. }
  145. time.Sleep(1 * time.Millisecond)
  146. for i := 0; i < 100; i++ {
  147. s.Update(20)
  148. }
  149. v := s.Values()
  150. avg := float64(0)
  151. for i := 0; i < len(v); i++ {
  152. avg += float64(v[i])
  153. }
  154. avg /= float64(len(v))
  155. if avg > 16 || avg < 14 {
  156. t.Errorf("out of range [14, 16]: %v\n", avg)
  157. }
  158. }
  159. func TestExpDecaySampleStatistics(t *testing.T) {
  160. now := time.Now()
  161. rand.Seed(1)
  162. s := NewExpDecaySample(100, 0.99)
  163. for i := 1; i <= 10000; i++ {
  164. s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
  165. }
  166. if count := s.Count(); 10000 != count {
  167. t.Errorf("s.Count(): 10000 != %v\n", count)
  168. }
  169. if min := s.Min(); 107 != min {
  170. t.Errorf("s.Min(): 107 != %v\n", min)
  171. }
  172. if max := s.Max(); 10000 != max {
  173. t.Errorf("s.Max(): 10000 != %v\n", max)
  174. }
  175. if mean := s.Mean(); 4965.98 != mean {
  176. t.Errorf("s.Mean(): 4965.98 != %v\n", mean)
  177. }
  178. if stdDev := s.StdDev(); 2959.825156930727 != stdDev {
  179. t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev)
  180. }
  181. ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
  182. if 4615 != ps[0] {
  183. t.Errorf("median: 4615 != %v\n", ps[0])
  184. }
  185. if 7672 != ps[1] {
  186. t.Errorf("75th percentile: 7672 != %v\n", ps[1])
  187. }
  188. if 9998.99 != ps[2] {
  189. t.Errorf("99th percentile: 9998.99 != %v\n", ps[2])
  190. }
  191. }
  192. func TestUniformSample(t *testing.T) {
  193. rand.Seed(1)
  194. s := NewUniformSample(100)
  195. for i := 0; i < 1000; i++ {
  196. s.Update(int64(i))
  197. }
  198. if size := s.Count(); 1000 != size {
  199. t.Errorf("s.Count(): 1000 != %v\n", size)
  200. }
  201. if size := s.Size(); 100 != size {
  202. t.Errorf("s.Size(): 100 != %v\n", size)
  203. }
  204. if l := len(s.Values()); 100 != l {
  205. t.Errorf("len(s.Values()): 100 != %v\n", l)
  206. }
  207. for _, v := range s.Values() {
  208. if v > 1000 || v < 0 {
  209. t.Errorf("out of range [0, 100): %v\n", v)
  210. }
  211. }
  212. }
  213. func TestUniformSampleDup(t *testing.T) {
  214. s1 := NewUniformSample(100)
  215. s1.Update(1)
  216. s2 := s1.Dup()
  217. s1.Update(1)
  218. if 1 != s2.Size() {
  219. t.Fatal(s2)
  220. }
  221. }
  222. func TestUniformSampleIncludesTail(t *testing.T) {
  223. rand.Seed(1)
  224. s := NewUniformSample(100)
  225. max := 100
  226. for i := 0; i < max; i++ {
  227. s.Update(int64(i))
  228. }
  229. v := s.Values()
  230. sum := 0
  231. exp := (max - 1) * max / 2
  232. for i := 0; i < len(v); i++ {
  233. sum += int(v[i])
  234. }
  235. if exp != sum {
  236. t.Errorf("sum: %v != %v\n", exp, sum)
  237. }
  238. }
  239. func TestUniformSampleStatistics(t *testing.T) {
  240. rand.Seed(1)
  241. s := NewUniformSample(100)
  242. for i := 1; i <= 10000; i++ {
  243. s.Update(int64(i))
  244. }
  245. if count := s.Count(); 10000 != count {
  246. t.Errorf("s.Count(): 10000 != %v\n", count)
  247. }
  248. if min := s.Min(); 9412 != min {
  249. t.Errorf("s.Min(): 9412 != %v\n", min)
  250. }
  251. if max := s.Max(); 10000 != max {
  252. t.Errorf("s.Max(): 10000 != %v\n", max)
  253. }
  254. if mean := s.Mean(); 9902.26 != mean {
  255. t.Errorf("s.Mean(): 9902.26 != %v\n", mean)
  256. }
  257. if stdDev := s.StdDev(); 101.8667384380201 != stdDev {
  258. t.Errorf("s.StdDev(): 101.8667384380201 != %v\n", stdDev)
  259. }
  260. ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
  261. if 9930.5 != ps[0] {
  262. t.Errorf("median: 9930.5 != %v\n", ps[0])
  263. }
  264. if 9973.75 != ps[1] {
  265. t.Errorf("75th percentile: 9973.75 != %v\n", ps[1])
  266. }
  267. if 9999.99 != ps[2] {
  268. t.Errorf("99th percentile: 9999.99 != %v\n", ps[2])
  269. }
  270. }
  271. func benchmarkSample(b *testing.B, s Sample) {
  272. var memStats runtime.MemStats
  273. runtime.ReadMemStats(&memStats)
  274. pauseTotalNs := memStats.PauseTotalNs
  275. b.ResetTimer()
  276. for i := 0; i < b.N; i++ {
  277. s.Update(1)
  278. }
  279. b.StopTimer()
  280. runtime.GC()
  281. runtime.ReadMemStats(&memStats)
  282. b.Logf("GC cost: %d ns/op", int(memStats.PauseTotalNs-pauseTotalNs)/b.N)
  283. }