github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/serialization_test.go (about) 1 package segment 2 3 import ( 4 "bytes" 5 "log" 6 "math/big" 7 "time" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 "github.com/pyroscope-io/pyroscope/pkg/testing" 12 ) 13 14 var serializedExampleV1 = "\x01({\"sampleRate\":0,\"spyName\":\"\",\"units\":\"\"}" + 15 "\x01\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x03\x01\x03\x00\x80\x92\xb8Ø\xfe\xff\xff" + 16 "\xff\x01\x01\x01\x00\x00\x8a\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x00\x00\x94\x92" + 17 "\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x00" 18 19 var serializedExampleV2 = "\x02={\"aggregationType\":\"\",\"sampleRate\":0,\"spyName\":\"\",\"units\":\"\"}" + 20 "\x01\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x03\x03\x01\x03\x00\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00" + 21 "\x00\x8a\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00\x00\x94\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00" 22 23 var serializedExampleV3 = "\x03={\"aggregationType\":\"\",\"sampleRate\":0,\"spyName\":\"\",\"units\":\"\"}" + 24 "\x01\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x03\x03\x01\x03\x00\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00" + 25 "\x00\x8a\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00\x00\x94\x92\xb8Ø\xfe\xff\xff\xff\x01\x01\x01\x01\x00" + 26 "\x80\x92\xb8Ø\xfe\xff\xff\xff\x01\x00" 27 28 var _ = Describe("stree", func() { 29 Context("Serialize / Deserialize", func() { 30 It("both functions work properly", func() { 31 s := New() 32 s.Put(testing.SimpleTime(0), 33 testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 34 s.Put(testing.SimpleTime(10), 35 testing.SimpleTime(19), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 36 s.Put(testing.SimpleTime(20), 37 testing.SimpleTime(29), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 38 39 s.watermarks = watermarks{absoluteTime: testing.SimpleTime(100)} 40 41 var buf bytes.Buffer 42 s.Serialize(&buf) 43 serialized := buf.Bytes() 44 log.Printf("%q", serialized) 45 46 s, err := Deserialize(bytes.NewReader(serialized)) 47 Expect(err).ToNot(HaveOccurred()) 48 var buf2 bytes.Buffer 49 s.Serialize(&buf2) 50 serialized2 := buf2.Bytes() 51 Expect(string(serialized2)).To(Equal(string(serialized))) 52 }) 53 }) 54 55 Context("Serialize", func() { 56 It("serializes segment tree properly", func() { 57 s := New() 58 s.Put(testing.SimpleTime(0), 59 testing.SimpleTime(9), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 60 s.Put(testing.SimpleTime(10), 61 testing.SimpleTime(19), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 62 s.Put(testing.SimpleTime(20), 63 testing.SimpleTime(29), 1, func(de int, t time.Time, r *big.Rat, a []Addon) {}) 64 65 var buf bytes.Buffer 66 s.Serialize(&buf) 67 serialized := buf.Bytes() 68 log.Printf("q: %q", string(serialized)) 69 Expect(string(serialized)).To(Equal(serializedExampleV3)) 70 }) 71 }) 72 73 Context("Deserialize", func() { 74 Context("v1", func() { 75 It("deserializes v1 data", func() { 76 s, err := Deserialize(bytes.NewReader([]byte(serializedExampleV1))) 77 Expect(err).ToNot(HaveOccurred()) 78 Expect(s.root.children[0]).ToNot(BeNil()) 79 Expect(s.root.children[1]).ToNot(BeNil()) 80 Expect(s.root.children[2]).ToNot(BeNil()) 81 Expect(s.root.children[3]).To(BeNil()) 82 }) 83 }) 84 Context("v2", func() { 85 It("deserializes v2 data", func() { 86 s, err := Deserialize(bytes.NewReader([]byte(serializedExampleV2))) 87 Expect(err).ToNot(HaveOccurred()) 88 Expect(s.root.children[0]).ToNot(BeNil()) 89 Expect(s.root.children[1]).ToNot(BeNil()) 90 Expect(s.root.children[2]).ToNot(BeNil()) 91 Expect(s.root.children[3]).To(BeNil()) 92 Expect(s.root.writes).To(Equal(uint64(3))) 93 }) 94 }) 95 Context("v3", func() { 96 It("deserializes v3 data", func() { 97 s, err := Deserialize(bytes.NewReader([]byte(serializedExampleV3))) 98 Expect(err).ToNot(HaveOccurred()) 99 Expect(s.root.children[0]).ToNot(BeNil()) 100 Expect(s.root.children[1]).ToNot(BeNil()) 101 Expect(s.root.children[2]).ToNot(BeNil()) 102 Expect(s.root.children[3]).To(BeNil()) 103 Expect(s.root.writes).To(Equal(uint64(3))) 104 }) 105 }) 106 }) 107 108 Context("watermarks serialize / deserialize", func() { 109 It("both functions work properly", func() { 110 w := watermarks{ 111 absoluteTime: testing.SimpleTime(100), 112 levels: map[int]time.Time{ 113 0: testing.SimpleTime(100), 114 1: testing.SimpleTime(1000), 115 }, 116 } 117 118 var buf bytes.Buffer 119 err := w.serialize(&buf) 120 Expect(err).ToNot(HaveOccurred()) 121 122 s := New() 123 err = deserializeWatermarks(bytes.NewReader(buf.Bytes()), &s.watermarks) 124 Expect(err).ToNot(HaveOccurred()) 125 Expect(w).To(Equal(s.watermarks)) 126 }) 127 }) 128 })