github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/overlap_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  //  relationship                               overlap read             overlap write
    13  // 	inside  rel = iota   // | S E |            <1                       1/1
    14  // 	match                // matching ranges    1/1                      1/1
    15  // 	outside              // | | S E            0/1                      0/1
    16  // 	overlap              // | S | E            <1                       <1
    17  // 	contain              // S | | E            1/1                      <1
    18  
    19  var _ = Describe("segment", func() {
    20  	Context("overlapRead", func() {
    21  		Context("match", func() {
    22  			It("returns correct values", func() {
    23  				Expect(overlapRead(
    24  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
    25  					testing.SimpleTime(0), testing.SimpleTime(100), // st et
    26  					10*time.Second,
    27  				).String()).To(Equal(big.NewRat(1, 1).String()))
    28  			})
    29  		})
    30  		Context("inside", func() {
    31  			It("returns correct values", func() {
    32  				Expect(overlapRead(
    33  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
    34  					testing.SimpleTime(10), testing.SimpleTime(90), // st et
    35  					10*time.Second,
    36  				).String()).To(Equal(big.NewRat(8, 10).String()))
    37  				Expect(overlapRead(
    38  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
    39  					testing.SimpleTime(0), testing.SimpleTime(90), // st et
    40  					10*time.Second,
    41  				).String()).To(Equal(big.NewRat(9, 10).String()))
    42  				Expect(overlapRead(
    43  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
    44  					testing.SimpleTime(10), testing.SimpleTime(100), // st et
    45  					10*time.Second,
    46  				).String()).To(Equal(big.NewRat(9, 10).String()))
    47  			})
    48  		})
    49  		Context("contain", func() {
    50  			It("returns correct values", func() {
    51  				Expect(overlapRead(
    52  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    53  					testing.SimpleTime(90), testing.SimpleTime(210), // st et
    54  					10*time.Second,
    55  				).String()).To(Equal(big.NewRat(1, 1).String()))
    56  				Expect(overlapRead(
    57  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    58  					testing.SimpleTime(100), testing.SimpleTime(210), // st et
    59  					10*time.Second,
    60  				).String()).To(Equal(big.NewRat(1, 1).String()))
    61  				Expect(overlapRead(
    62  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    63  					testing.SimpleTime(90), testing.SimpleTime(200), // st et
    64  					10*time.Second,
    65  				).String()).To(Equal(big.NewRat(1, 1).String()))
    66  			})
    67  		})
    68  		Context("overlap", func() {
    69  			It("returns correct values", func() {
    70  				Expect(overlapRead(
    71  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    72  					testing.SimpleTime(90), testing.SimpleTime(110), // st et
    73  					10*time.Second,
    74  				).String()).To(Equal(big.NewRat(1, 10).String()))
    75  				Expect(overlapRead(
    76  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    77  					testing.SimpleTime(190), testing.SimpleTime(210), // st et
    78  					10*time.Second,
    79  				).String()).To(Equal(big.NewRat(1, 10).String()))
    80  			})
    81  		})
    82  		Context("outside", func() {
    83  			It("returns correct values", func() {
    84  				Expect(overlapRead(
    85  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    86  					testing.SimpleTime(90), testing.SimpleTime(100), // st et
    87  					10*time.Second,
    88  				).String()).To(Equal(big.NewRat(0, 1).String()))
    89  				Expect(overlapRead(
    90  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    91  					testing.SimpleTime(80), testing.SimpleTime(90), // st et
    92  					10*time.Second,
    93  				).String()).To(Equal(big.NewRat(0, 1).String()))
    94  				Expect(overlapRead(
    95  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
    96  					testing.SimpleTime(200), testing.SimpleTime(210), // st et
    97  					10*time.Second,
    98  				).String()).To(Equal(big.NewRat(0, 1).String()))
    99  				Expect(overlapRead(
   100  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   101  					testing.SimpleTime(210), testing.SimpleTime(220), // st et
   102  					10*time.Second,
   103  				).String()).To(Equal(big.NewRat(0, 1).String()))
   104  			})
   105  		})
   106  	})
   107  
   108  	Context("overlapWrite", func() {
   109  		Context("match", func() {
   110  			It("returns correct values", func() {
   111  				Expect(overlapWrite(
   112  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
   113  					testing.SimpleTime(0), testing.SimpleTime(100), // st et
   114  					10*time.Second,
   115  				).String()).To(Equal(big.NewRat(1, 1).String()))
   116  			})
   117  		})
   118  		Context("inside", func() {
   119  			It("returns correct values", func() {
   120  				Expect(overlapWrite(
   121  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
   122  					testing.SimpleTime(10), testing.SimpleTime(90), // st et
   123  					10*time.Second,
   124  				).String()).To(Equal(big.NewRat(1, 1).String()))
   125  				Expect(overlapWrite(
   126  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
   127  					testing.SimpleTime(0), testing.SimpleTime(90), // st et
   128  					10*time.Second,
   129  				).String()).To(Equal(big.NewRat(1, 1).String()))
   130  				Expect(overlapWrite(
   131  					testing.SimpleTime(0), testing.SimpleTime(100), // t1 t2
   132  					testing.SimpleTime(10), testing.SimpleTime(100), // st et
   133  					10*time.Second,
   134  				).String()).To(Equal(big.NewRat(1, 1).String()))
   135  			})
   136  		})
   137  		Context("contain", func() {
   138  			It("returns correct values", func() {
   139  				Expect(overlapWrite(
   140  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   141  					testing.SimpleTime(90), testing.SimpleTime(210), // st et
   142  					10*time.Second,
   143  				).String()).To(Equal(big.NewRat(10, 12).String()))
   144  				Expect(overlapWrite(
   145  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   146  					testing.SimpleTime(100), testing.SimpleTime(210), // st et
   147  					10*time.Second,
   148  				).String()).To(Equal(big.NewRat(10, 11).String()))
   149  				Expect(overlapWrite(
   150  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   151  					testing.SimpleTime(90), testing.SimpleTime(200), // st et
   152  					10*time.Second,
   153  				).String()).To(Equal(big.NewRat(10, 11).String()))
   154  			})
   155  		})
   156  		Context("overlap", func() {
   157  			It("returns correct values", func() {
   158  				Expect(overlapWrite(
   159  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   160  					testing.SimpleTime(90), testing.SimpleTime(110), // st et
   161  					10*time.Second,
   162  				).String()).To(Equal(big.NewRat(1, 2).String()))
   163  				Expect(overlapWrite(
   164  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   165  					testing.SimpleTime(190), testing.SimpleTime(210), // st et
   166  					10*time.Second,
   167  				).String()).To(Equal(big.NewRat(1, 2).String()))
   168  			})
   169  		})
   170  		Context("outside", func() {
   171  			It("returns correct values", func() {
   172  				Expect(overlapWrite(
   173  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   174  					testing.SimpleTime(90), testing.SimpleTime(100), // st et
   175  					10*time.Second,
   176  				).String()).To(Equal(big.NewRat(0, 1).String()))
   177  				Expect(overlapWrite(
   178  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   179  					testing.SimpleTime(80), testing.SimpleTime(90), // st et
   180  					10*time.Second,
   181  				).String()).To(Equal(big.NewRat(0, 1).String()))
   182  				Expect(overlapWrite(
   183  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   184  					testing.SimpleTime(200), testing.SimpleTime(210), // st et
   185  					10*time.Second,
   186  				).String()).To(Equal(big.NewRat(0, 1).String()))
   187  				Expect(overlapWrite(
   188  					testing.SimpleTime(100), testing.SimpleTime(200), // t1 t2
   189  					testing.SimpleTime(210), testing.SimpleTime(220), // st et
   190  					10*time.Second,
   191  				).String()).To(Equal(big.NewRat(0, 1).String()))
   192  			})
   193  		})
   194  	})
   195  })