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

     1  package symdb
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	v1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
    11  )
    12  
    13  func Test_MappingsEncoding(t *testing.T) {
    14  	type testCase struct {
    15  		description string
    16  		mappings    []v1.InMemoryMapping
    17  	}
    18  
    19  	testCases := []testCase{
    20  		{
    21  			description: "empty",
    22  			mappings:    []v1.InMemoryMapping{},
    23  		},
    24  		{
    25  			description: "zero",
    26  			mappings:    []v1.InMemoryMapping{{}},
    27  		},
    28  		{
    29  			description: "single mapping",
    30  			mappings: []v1.InMemoryMapping{
    31  				{
    32  					MemoryStart:     math.MaxUint64,
    33  					MemoryLimit:     math.MaxUint64,
    34  					FileOffset:      math.MaxUint64,
    35  					Filename:        1,
    36  					BuildId:         2,
    37  					HasFunctions:    true,
    38  					HasFilenames:    false,
    39  					HasLineNumbers:  false,
    40  					HasInlineFrames: false,
    41  				},
    42  			},
    43  		},
    44  		{
    45  			description: "optional fields mix",
    46  			mappings: []v1.InMemoryMapping{
    47  				// Block size == 3
    48  				{MemoryStart: math.MaxUint64},
    49  				{},
    50  				{},
    51  
    52  				{},
    53  				{MemoryLimit: math.MaxUint64},
    54  				{},
    55  
    56  				{},
    57  				{},
    58  				{FileOffset: math.MaxUint64},
    59  
    60  				{MemoryStart: math.MaxUint64},
    61  				{MemoryLimit: math.MaxUint64},
    62  				{FileOffset: math.MaxUint64},
    63  
    64  				{},
    65  				{},
    66  				{},
    67  			},
    68  		},
    69  		{
    70  			description: "flag combinations",
    71  			mappings: []v1.InMemoryMapping{
    72  				{HasFunctions: false, HasFilenames: false, HasLineNumbers: false, HasInlineFrames: false},
    73  				{HasFunctions: false, HasFilenames: false, HasLineNumbers: false, HasInlineFrames: true},
    74  				{HasFunctions: false, HasFilenames: false, HasLineNumbers: true, HasInlineFrames: false},
    75  				{HasFunctions: false, HasFilenames: false, HasLineNumbers: true, HasInlineFrames: true},
    76  				{HasFunctions: false, HasFilenames: true, HasLineNumbers: false, HasInlineFrames: false},
    77  				{HasFunctions: false, HasFilenames: true, HasLineNumbers: false, HasInlineFrames: true},
    78  				{HasFunctions: false, HasFilenames: true, HasLineNumbers: true, HasInlineFrames: false},
    79  				{HasFunctions: false, HasFilenames: true, HasLineNumbers: true, HasInlineFrames: true},
    80  				{HasFunctions: true, HasFilenames: false, HasLineNumbers: false, HasInlineFrames: false},
    81  				{HasFunctions: true, HasFilenames: false, HasLineNumbers: false, HasInlineFrames: true},
    82  				{HasFunctions: true, HasFilenames: false, HasLineNumbers: true, HasInlineFrames: false},
    83  				{HasFunctions: true, HasFilenames: false, HasLineNumbers: true, HasInlineFrames: true},
    84  				{HasFunctions: true, HasFilenames: true, HasLineNumbers: false, HasInlineFrames: false},
    85  				{HasFunctions: true, HasFilenames: true, HasLineNumbers: false, HasInlineFrames: true},
    86  				{HasFunctions: true, HasFilenames: true, HasLineNumbers: true, HasInlineFrames: false},
    87  				{HasFunctions: true, HasFilenames: true, HasLineNumbers: true, HasInlineFrames: true},
    88  			},
    89  		},
    90  	}
    91  
    92  	for _, tc := range testCases {
    93  		tc := tc
    94  		t.Run(tc.description, func(t *testing.T) {
    95  			var buf bytes.Buffer
    96  			w := newTestFileWriter(&buf)
    97  			e := newMappingsEncoder()
    98  			e.blockSize = 3
    99  			h, err := writeSymbolsBlock(w, tc.mappings, e)
   100  			require.NoError(t, err)
   101  
   102  			d, err := newMappingsDecoder(h)
   103  			require.NoError(t, err)
   104  			out := make([]v1.InMemoryMapping, h.Length)
   105  			require.NoError(t, d.decode(out, &buf))
   106  			require.Equal(t, tc.mappings, out)
   107  		})
   108  	}
   109  }