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

     1  package tree
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2/dsl/core"
     5  	. "github.com/onsi/gomega"
     6  )
     7  
     8  var (
     9  	loc1               = &Location{Id: 1}
    10  	loc2               = &Location{Id: 2}
    11  	loc3               = &Location{Id: 3}
    12  	fun1               = &Function{Id: 1}
    13  	fun2               = &Function{Id: 2}
    14  	fun3               = &Function{Id: 3}
    15  	sortedLocs         = &Profile{Location: []*Location{loc1, loc2, loc3}}
    16  	unsortedLocs       = &Profile{Location: []*Location{loc2, loc3, loc1}}
    17  	nonconsecutiveLocs = &Profile{Location: []*Location{loc1, loc3}}
    18  	sortedFuns         = &Profile{Function: []*Function{fun1, fun2, fun3}}
    19  	unsortedFuns       = &Profile{Function: []*Function{fun2, fun3, fun1}}
    20  	nonconsecutiveFuns = &Profile{Function: []*Function{fun1, fun3}}
    21  )
    22  
    23  var _ = Describe("profile finder", func() {
    24  	Describe("Sorted consecutive locations", func() {
    25  		It("returns correct results", func() {
    26  			finder := NewFinder(sortedLocs)
    27  			loc, ok := finder.FindLocation(1)
    28  			Expect(ok).To(BeTrue())
    29  			Expect(loc).To(BeIdenticalTo(loc1))
    30  			loc, ok = finder.FindLocation(2)
    31  			Expect(ok).To(BeTrue())
    32  			Expect(loc).To(BeIdenticalTo(loc2))
    33  			loc, ok = finder.FindLocation(3)
    34  			Expect(ok).To(BeTrue())
    35  			Expect(loc).To(BeIdenticalTo(loc3))
    36  			loc, ok = finder.FindLocation(0)
    37  			Expect(ok).To(BeFalse())
    38  			loc, ok = finder.FindLocation(4)
    39  			Expect(ok).To(BeFalse())
    40  		})
    41  	})
    42  	Describe("Unsorted consecutive locations", func() {
    43  		It("returns correct results", func() {
    44  			finder := NewFinder(unsortedLocs)
    45  			loc, ok := finder.FindLocation(1)
    46  			Expect(ok).To(BeTrue())
    47  			Expect(loc).To(BeIdenticalTo(loc1))
    48  			loc, ok = finder.FindLocation(2)
    49  			Expect(ok).To(BeTrue())
    50  			Expect(loc).To(BeIdenticalTo(loc2))
    51  			loc, ok = finder.FindLocation(3)
    52  			Expect(ok).To(BeTrue())
    53  			Expect(loc).To(BeIdenticalTo(loc3))
    54  			loc, ok = finder.FindLocation(0)
    55  			Expect(ok).To(BeFalse())
    56  			loc, ok = finder.FindLocation(4)
    57  			Expect(ok).To(BeFalse())
    58  		})
    59  	})
    60  	Describe("Non-consecutive locations", func() {
    61  		It("returns correct results", func() {
    62  			finder := NewFinder(nonconsecutiveLocs)
    63  			loc, ok := finder.FindLocation(1)
    64  			Expect(ok).To(BeTrue())
    65  			Expect(loc).To(BeIdenticalTo(loc1))
    66  			loc, ok = finder.FindLocation(2)
    67  			Expect(ok).To(BeFalse())
    68  			loc, ok = finder.FindLocation(3)
    69  			Expect(ok).To(BeTrue())
    70  			Expect(loc).To(BeIdenticalTo(loc3))
    71  			loc, ok = finder.FindLocation(0)
    72  			Expect(ok).To(BeFalse())
    73  			loc, ok = finder.FindLocation(4)
    74  			Expect(ok).To(BeFalse())
    75  		})
    76  	})
    77  	Describe("Sorted consecutive functions", func() {
    78  		It("returns correct results", func() {
    79  			finder := NewFinder(sortedFuns)
    80  			fun, ok := finder.FindFunction(1)
    81  			Expect(ok).To(BeTrue())
    82  			Expect(fun).To(BeIdenticalTo(fun1))
    83  			fun, ok = finder.FindFunction(2)
    84  			Expect(ok).To(BeTrue())
    85  			Expect(fun).To(BeIdenticalTo(fun2))
    86  			fun, ok = finder.FindFunction(3)
    87  			Expect(ok).To(BeTrue())
    88  			Expect(fun).To(BeIdenticalTo(fun3))
    89  			fun, ok = finder.FindFunction(0)
    90  			Expect(ok).To(BeFalse())
    91  			fun, ok = finder.FindFunction(4)
    92  			Expect(ok).To(BeFalse())
    93  		})
    94  	})
    95  	Describe("Unsorted consecutive functions", func() {
    96  		It("returns correct results", func() {
    97  			finder := NewFinder(unsortedFuns)
    98  			fun, ok := finder.FindFunction(1)
    99  			Expect(ok).To(BeTrue())
   100  			Expect(fun).To(BeIdenticalTo(fun1))
   101  			fun, ok = finder.FindFunction(2)
   102  			Expect(ok).To(BeTrue())
   103  			Expect(fun).To(BeIdenticalTo(fun2))
   104  			fun, ok = finder.FindFunction(3)
   105  			Expect(ok).To(BeTrue())
   106  			Expect(fun).To(BeIdenticalTo(fun3))
   107  			fun, ok = finder.FindFunction(0)
   108  			Expect(ok).To(BeFalse())
   109  			fun, ok = finder.FindFunction(4)
   110  			Expect(ok).To(BeFalse())
   111  		})
   112  	})
   113  	Describe("Non-consecutive functions", func() {
   114  		It("returns correct results", func() {
   115  			finder := NewFinder(nonconsecutiveFuns)
   116  			fun, ok := finder.FindFunction(1)
   117  			Expect(ok).To(BeTrue())
   118  			Expect(fun).To(BeIdenticalTo(fun1))
   119  			fun, ok = finder.FindFunction(2)
   120  			Expect(ok).To(BeFalse())
   121  			fun, ok = finder.FindFunction(3)
   122  			Expect(ok).To(BeTrue())
   123  			Expect(fun).To(BeIdenticalTo(fun3))
   124  			fun, ok = finder.FindFunction(0)
   125  			Expect(ok).To(BeFalse())
   126  			fun, ok = finder.FindFunction(4)
   127  			Expect(ok).To(BeFalse())
   128  		})
   129  	})
   130  })