github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/timeline_test.go (about) 1 package segment 2 3 import ( 4 "math/big" 5 "time" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 "github.com/pyroscope-io/pyroscope/pkg/testing" 10 ) 11 12 var _ = Describe("timeline", func() { 13 var ( 14 timeline *Timeline 15 st int 16 et int 17 ) 18 19 BeforeEach(func() { 20 st = 0 21 et = 40 22 }) 23 JustBeforeEach(func() { 24 timeline = GenerateTimeline( 25 testing.SimpleTime(st), 26 testing.SimpleTime(et), 27 ) 28 }) 29 30 Describe("PopulateTimeline", func() { 31 Context("empty segment", func() { 32 It("works as expected", func(done Done) { 33 s := New() 34 timeline.PopulateTimeline(s) 35 Expect(timeline.Samples).To(Equal([]uint64{ 36 0, 37 0, 38 0, 39 0, 40 })) 41 close(done) 42 }) 43 }) 44 Context("one level", func() { 45 It("works as expected", func() { 46 done := make(chan interface{}) 47 go func() { 48 s := New() 49 s.Put(testing.SimpleTime(0), 50 testing.SimpleTime(9), 2, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 51 s.Put(testing.SimpleTime(10), 52 testing.SimpleTime(19), 5, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 53 s.Put(testing.SimpleTime(20), 54 testing.SimpleTime(29), 0, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 55 56 timeline.PopulateTimeline(s) 57 Expect(timeline.Samples).To(Equal([]uint64{ 58 3, 59 6, 60 1, 61 0, 62 })) 63 64 close(done) 65 }() 66 Eventually(done, 5).Should(BeClosed()) 67 }) 68 }) 69 Context("multiple Levels", func() { 70 BeforeEach(func() { 71 st = 0 72 et = 365 * 24 * 60 * 60 73 }) 74 75 It("works as expected", func() { 76 done := make(chan interface{}) 77 go func() { 78 s := New() 79 s.Put(testing.SimpleTime(0), 80 testing.SimpleTime(9), 2, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 81 s.Put(testing.SimpleTime(10), 82 testing.SimpleTime(19), 5, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 83 s.Put(testing.SimpleTime(20), 84 testing.SimpleTime(29), 0, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 85 86 timeline.PopulateTimeline(s) 87 expected := make([]uint64, 3153) 88 expected[0] = 8 89 Expect(timeline.Samples).To(Equal(expected)) 90 91 close(done) 92 }() 93 Eventually(done, 5).Should(BeClosed()) 94 }) 95 }) 96 97 Context("with threshold", func() { 98 BeforeEach(func() { 99 st = 0 100 et = 365 * 24 * 60 * 60 101 }) 102 103 It("removed nodes are down-sampled", func() { 104 done := make(chan interface{}) 105 go func() { 106 s := New() 107 now := time.Now() 108 s.Put(testing.SimpleTime(0), 109 testing.SimpleTime(9), 2, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 110 s.Put(testing.SimpleTime(10), 111 testing.SimpleTime(19), 5, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 112 113 // To prevent segment root removal. 114 s.Put(now.Add(-10*time.Second), 115 now, 0, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 116 117 threshold := NewRetentionPolicy(). 118 SetLevelPeriod(0, time.Second). 119 SetLevelPeriod(1, time.Minute) 120 121 _, err := s.DeleteNodesBefore(threshold) 122 Expect(err).ToNot(HaveOccurred()) 123 timeline.PopulateTimeline(s) 124 expected := make([]uint64, 3153) 125 expected[0] = 8 126 Expect(timeline.Samples).To(Equal(expected)) 127 128 close(done) 129 }() 130 Eventually(done, 5).Should(BeClosed()) 131 }) 132 }) 133 }) 134 })