github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/delta_test.go (about)

     1  package phlaredb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	schemav1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
     9  	schemav1testhelper "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1/testhelper"
    10  	"github.com/grafana/pyroscope/pkg/pprof/testhelper"
    11  )
    12  
    13  func TestComputeDelta(t *testing.T) {
    14  	delta := newDeltaProfiles()
    15  	builder := testhelper.NewProfileBuilder(1).MemoryProfile()
    16  	builder.ForStacktraceString("a", "b", "c").AddSamples(1, 2, 3, 4)
    17  	builder.ForStacktraceString("a", "b", "c", "d").AddSamples(1, 2, 3, 4)
    18  
    19  	profiles, _ := schemav1testhelper.NewProfileSchema(builder, "memory")
    20  
    21  	samples := delta.computeDelta(profiles[0])
    22  	require.Empty(t, samples.StacktraceIDs)
    23  	samples = delta.computeDelta(profiles[1])
    24  	require.Empty(t, samples.StacktraceIDs)
    25  
    26  	builder = testhelper.NewProfileBuilder(1).MemoryProfile()
    27  	builder.ForStacktraceString("a", "b", "c").AddSamples(2, 4, 3, 4)
    28  	builder.ForStacktraceString("a", "b", "c", "d").AddSamples(2, 4, 3, 4)
    29  
    30  	profiles, _ = schemav1testhelper.NewProfileSchema(builder, "memory")
    31  	samples = delta.computeDelta(profiles[0])
    32  	require.NotEmpty(t, samples.StacktraceIDs)
    33  	samples = delta.computeDelta(profiles[1])
    34  	require.NotEmpty(t, samples.StacktraceIDs)
    35  }
    36  
    37  func TestDeltaSample(t *testing.T) {
    38  	new := schemav1.Samples{
    39  		StacktraceIDs: []uint32{2, 3},
    40  		Values:        []uint64{1, 1},
    41  	}
    42  	highest := map[uint32]uint64{}
    43  	_ = deltaSamples(highest, new)
    44  	require.Equal(t, 2, len(highest))
    45  	require.Equal(t, map[uint32]uint64{
    46  		2: 1,
    47  		3: 1,
    48  	}, highest)
    49  
    50  	t.Run("same stacktraces, matching counter samples, matching gauge samples", func(t *testing.T) {
    51  		new = schemav1.Samples{
    52  			StacktraceIDs: []uint32{2, 3},
    53  			Values:        []uint64{1, 1},
    54  		}
    55  		_ = deltaSamples(highest, new)
    56  		require.Equal(t, 2, len(highest))
    57  		require.Equal(t, map[uint32]uint64{
    58  			2: 1,
    59  			3: 1,
    60  		}, highest)
    61  		require.Equal(t, schemav1.Samples{
    62  			StacktraceIDs: []uint32{2, 3},
    63  			Values:        []uint64{0, 0},
    64  		}, new)
    65  	})
    66  
    67  	t.Run("same stacktraces, matching counter samples, empty gauge samples", func(t *testing.T) {
    68  		new = schemav1.Samples{
    69  			StacktraceIDs: []uint32{2, 3},
    70  			Values:        []uint64{1, 1},
    71  		}
    72  		_ = deltaSamples(highest, new)
    73  		require.Equal(t, 2, len(highest))
    74  		require.Equal(t, map[uint32]uint64{
    75  			2: 1,
    76  			3: 1,
    77  		}, highest)
    78  		require.Equal(t, schemav1.Samples{
    79  			StacktraceIDs: []uint32{2, 3},
    80  			Values:        []uint64{0, 0},
    81  		}, new)
    82  	})
    83  
    84  	t.Run("new stacktrace, and increase counter in existing stacktrace", func(t *testing.T) {
    85  		new = schemav1.Samples{
    86  			StacktraceIDs: []uint32{3, 5},
    87  			Values:        []uint64{6, 1},
    88  		}
    89  		_ = deltaSamples(highest, new)
    90  		require.Equal(t, map[uint32]uint64{
    91  			2: 1,
    92  			3: 6,
    93  			5: 1,
    94  		}, highest)
    95  	})
    96  
    97  	t.Run("same stacktraces, counter samples resetting", func(t *testing.T) {
    98  		new = schemav1.Samples{
    99  			StacktraceIDs: []uint32{3, 5},
   100  			Values:        []uint64{0, 1},
   101  		}
   102  		reset := deltaSamples(highest, new)
   103  		require.True(t, reset)
   104  		require.Equal(t, map[uint32]uint64{
   105  			2: 1,
   106  			3: 6,
   107  			5: 1,
   108  		}, highest)
   109  	})
   110  
   111  	t.Run("two new stacktraces, raise counters of existing stacktrace", func(t *testing.T) {
   112  		new = schemav1.Samples{
   113  			StacktraceIDs: []uint32{0, 1, 7},
   114  			Values:        []uint64{10, 2, 1},
   115  		}
   116  
   117  		_ = deltaSamples(highest, new)
   118  		require.Equal(t, map[uint32]uint64{
   119  			0: 10,
   120  			1: 2,
   121  			2: 1,
   122  			3: 6,
   123  			5: 1,
   124  			7: 1,
   125  		}, highest)
   126  
   127  		require.Equal(t, schemav1.Samples{
   128  			StacktraceIDs: []uint32{0, 1, 7},
   129  			Values:        []uint64{10, 2, 1},
   130  		}, new)
   131  	})
   132  }