github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/tree/flamebearer_test.go (about)

     1  package tree
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  
     7  	. "github.com/onsi/ginkgo/v2/dsl/core"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("FlamebearerStruct", func() {
    12  	Context("simple case", func() {
    13  		It("sets all attributes correctly", func() {
    14  			tree := New()
    15  			tree.Insert([]byte("a;b"), uint64(1))
    16  			tree.Insert([]byte("a;c"), uint64(2))
    17  
    18  			f := tree.FlamebearerStruct(1024)
    19  			Expect(f.Names).To(Equal([]string{"total", "a", "c", "b"}))
    20  			Expect(f.Levels).To(Equal([][]int{
    21  				// i+0 = x offset (delta encoded)
    22  				// i+1 = total
    23  				// i+2 = self
    24  				// i+3 = index in names array
    25  				{0, 3, 0, 0},
    26  				{0, 3, 0, 1},
    27  				{0, 1, 1, 3, 0, 2, 2, 2},
    28  			}))
    29  			Expect(f.NumTicks).To(Equal(3))
    30  			Expect(f.MaxSelf).To(Equal(2))
    31  		})
    32  	})
    33  	Context("case with many nodes", func() {
    34  		It("groups nodes into a new \"other\" node", func() {
    35  			tree := New()
    36  			r := rand.New(rand.NewSource(123))
    37  			for i := 0; i < 2048; i++ {
    38  				tree.Insert([]byte(fmt.Sprintf("foo;bar%d", i)), uint64(r.Intn(4000)))
    39  			}
    40  
    41  			f := tree.FlamebearerStruct(10)
    42  			Expect(f.Names).To(ContainElement("other"))
    43  		})
    44  	})
    45  })