sample_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 TestExpDecaySampleSnapshot(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. snapshot := s.Snapshot()
  167. s.Update(1)
  168. testExpDecaySampleStatistics(t, snapshot)
  169. }
  170. func TestExpDecaySampleStatistics(t *testing.T) {
  171. now := time.Now()
  172. rand.Seed(1)
  173. s := NewExpDecaySample(100, 0.99)
  174. for i := 1; i <= 10000; i++ {
  175. s.(*ExpDecaySample).update(now.Add(time.Duration(i)), int64(i))
  176. }
  177. testExpDecaySampleStatistics(t, s)
  178. }
  179. func TestUniformSample(t *testing.T) {
  180. rand.Seed(1)
  181. s := NewUniformSample(100)
  182. for i := 0; i < 1000; i++ {
  183. s.Update(int64(i))
  184. }
  185. if size := s.Count(); 1000 != size {
  186. t.Errorf("s.Count(): 1000 != %v\n", size)
  187. }
  188. if size := s.Size(); 100 != size {
  189. t.Errorf("s.Size(): 100 != %v\n", size)
  190. }
  191. if l := len(s.Values()); 100 != l {
  192. t.Errorf("len(s.Values()): 100 != %v\n", l)
  193. }
  194. for _, v := range s.Values() {
  195. if v > 1000 || v < 0 {
  196. t.Errorf("out of range [0, 100): %v\n", v)
  197. }
  198. }
  199. }
  200. func TestUniformSampleIncludesTail(t *testing.T) {
  201. rand.Seed(1)
  202. s := NewUniformSample(100)
  203. max := 100
  204. for i := 0; i < max; i++ {
  205. s.Update(int64(i))
  206. }
  207. v := s.Values()
  208. sum := 0
  209. exp := (max - 1) * max / 2
  210. for i := 0; i < len(v); i++ {
  211. sum += int(v[i])
  212. }
  213. if exp != sum {
  214. t.Errorf("sum: %v != %v\n", exp, sum)
  215. }
  216. }
  217. func TestUniformSampleSnapshot(t *testing.T) {
  218. s := NewUniformSample(100)
  219. for i := 1; i <= 10000; i++ {
  220. s.Update(int64(i))
  221. }
  222. snapshot := s.Snapshot()
  223. s.Update(1)
  224. testUniformSampleStatistics(t, snapshot)
  225. }
  226. func TestUniformSampleStatistics(t *testing.T) {
  227. rand.Seed(1)
  228. s := NewUniformSample(100)
  229. for i := 1; i <= 10000; i++ {
  230. s.Update(int64(i))
  231. }
  232. testUniformSampleStatistics(t, s)
  233. }
  234. func benchmarkSample(b *testing.B, s Sample) {
  235. var memStats runtime.MemStats
  236. runtime.ReadMemStats(&memStats)
  237. pauseTotalNs := memStats.PauseTotalNs
  238. b.ResetTimer()
  239. for i := 0; i < b.N; i++ {
  240. s.Update(1)
  241. }
  242. b.StopTimer()
  243. runtime.GC()
  244. runtime.ReadMemStats(&memStats)
  245. b.Logf("GC cost: %d ns/op", int(memStats.PauseTotalNs-pauseTotalNs)/b.N)
  246. }
  247. func testExpDecaySampleStatistics(t *testing.T, s Sample) {
  248. if count := s.Count(); 10000 != count {
  249. t.Errorf("s.Count(): 10000 != %v\n", count)
  250. }
  251. if min := s.Min(); 107 != min {
  252. t.Errorf("s.Min(): 107 != %v\n", min)
  253. }
  254. if max := s.Max(); 10000 != max {
  255. t.Errorf("s.Max(): 10000 != %v\n", max)
  256. }
  257. if mean := s.Mean(); 4965.98 != mean {
  258. t.Errorf("s.Mean(): 4965.98 != %v\n", mean)
  259. }
  260. if stdDev := s.StdDev(); 2959.825156930727 != stdDev {
  261. t.Errorf("s.StdDev(): 2959.825156930727 != %v\n", stdDev)
  262. }
  263. ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
  264. if 4615 != ps[0] {
  265. t.Errorf("median: 4615 != %v\n", ps[0])
  266. }
  267. if 7672 != ps[1] {
  268. t.Errorf("75th percentile: 7672 != %v\n", ps[1])
  269. }
  270. if 9998.99 != ps[2] {
  271. t.Errorf("99th percentile: 9998.99 != %v\n", ps[2])
  272. }
  273. }
  274. func testUniformSampleStatistics(t *testing.T, s Sample) {
  275. if count := s.Count(); 10000 != count {
  276. t.Errorf("s.Count(): 10000 != %v\n", count)
  277. }
  278. if min := s.Min(); 9412 != min {
  279. t.Errorf("s.Min(): 9412 != %v\n", min)
  280. }
  281. if max := s.Max(); 10000 != max {
  282. t.Errorf("s.Max(): 10000 != %v\n", max)
  283. }
  284. if mean := s.Mean(); 9902.26 != mean {
  285. t.Errorf("s.Mean(): 9902.26 != %v\n", mean)
  286. }
  287. if stdDev := s.StdDev(); 101.8667384380201 != stdDev {
  288. t.Errorf("s.StdDev(): 101.8667384380201 != %v\n", stdDev)
  289. }
  290. ps := s.Percentiles([]float64{0.5, 0.75, 0.99})
  291. if 9930.5 != ps[0] {
  292. t.Errorf("median: 9930.5 != %v\n", ps[0])
  293. }
  294. if 9973.75 != ps[1] {
  295. t.Errorf("75th percentile: 9973.75 != %v\n", ps[1])
  296. }
  297. if 9999.99 != ps[2] {
  298. t.Errorf("99th percentile: 9999.99 != %v\n", ps[2])
  299. }
  300. }
  301. // TestUniformSampleConcurrentUpdateCount would expose data race problems with
  302. // concurrent Update and Count calls on Sample when test is called with -race
  303. // argument
  304. func TestUniformSampleConcurrentUpdateCount(t *testing.T) {
  305. if testing.Short() {
  306. t.Skip("skipping in short mode")
  307. }
  308. s := NewUniformSample(100)
  309. for i := 0; i < 100; i++ {
  310. s.Update(int64(i))
  311. }
  312. quit := make(chan struct{})
  313. go func() {
  314. t := time.NewTicker(10 * time.Millisecond)
  315. for {
  316. select {
  317. case <-t.C:
  318. s.Update(rand.Int63())
  319. case <-quit:
  320. t.Stop()
  321. return
  322. }
  323. }
  324. }()
  325. for i := 0; i < 1000; i++ {
  326. s.Count()
  327. time.Sleep(5 * time.Millisecond)
  328. }
  329. quit <- struct{}{}
  330. }