github.com/grafana/pyroscope@v1.18.0/pkg/pprof/fix_go_profile_test.go (about)

     1  package pprof
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_FixGoProfile(t *testing.T) {
    13  	p, err := OpenFile("testdata/gotruncatefix/heap_go_truncated_4.pb.gz")
    14  	require.NoError(t, err)
    15  
    16  	f := FixGoProfile(p.Profile)
    17  	s := make(map[string]struct{})
    18  	for _, x := range f.StringTable {
    19  		if _, ok := s[x]; !ok {
    20  			s[x] = struct{}{}
    21  		} else {
    22  			t.Fatal("duplicate string found")
    23  		}
    24  	}
    25  
    26  	t.Logf(" * Sample:   %6d -> %-6d", len(p.Sample), len(f.Sample))
    27  	t.Logf(" * Location: %6d -> %-6d", len(p.Location), len(f.Location))
    28  	t.Logf(" * Function: %6d -> %-6d", len(p.Function), len(f.Function))
    29  	t.Logf(" * Strings:  %6d -> %-6d", len(p.StringTable), len(f.StringTable))
    30  	// fix_test.go:24:  * Sample:     6785 -> 3797
    31  	// fix_test.go:25:  * Location:   4848 -> 4680
    32  	// fix_test.go:26:  * Function:   2801 -> 2724
    33  	// fix_test.go:27:  * Strings:    3536 -> 3458
    34  	assert.Equal(t, 2988, len(p.Sample)-len(f.Sample))
    35  	assert.Equal(t, 168, len(p.Location)-len(f.Location))
    36  	assert.Equal(t, 77, len(p.Function)-len(f.Function))
    37  	assert.Equal(t, 78, len(p.StringTable)-len(f.StringTable))
    38  }
    39  
    40  func Test_DropGoTypeParameters(t *testing.T) {
    41  	ef, err := os.Open("testdata/go_type_parameters.expected.txt")
    42  	require.NoError(t, err)
    43  	defer ef.Close()
    44  
    45  	in, err := os.Open("testdata/go_type_parameters.txt")
    46  	require.NoError(t, err)
    47  	defer in.Close()
    48  
    49  	input := bufio.NewScanner(in)
    50  	expected := bufio.NewScanner(ef)
    51  	for input.Scan() {
    52  		expected.Scan()
    53  		require.Equal(t, expected.Text(), dropGoTypeParameters(input.Text()))
    54  	}
    55  
    56  	require.NoError(t, input.Err())
    57  	require.NoError(t, expected.Err())
    58  	require.False(t, expected.Scan())
    59  }