github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/convert/speedscope/speedscope_test.go (about)

     1  package speedscope
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/pyroscope-io/pyroscope/pkg/ingestion"
    10  	"github.com/pyroscope-io/pyroscope/pkg/storage/metadata"
    11  	"github.com/pyroscope-io/pyroscope/pkg/storage/segment"
    12  
    13  	"github.com/pyroscope-io/pyroscope/pkg/storage"
    14  )
    15  
    16  type mockIngester struct{ actual []*storage.PutInput }
    17  
    18  func (m *mockIngester) Put(_ context.Context, p *storage.PutInput) error {
    19  	m.actual = append(m.actual, p)
    20  	return nil
    21  }
    22  
    23  var _ = Describe("Speedscope", func() {
    24  	It("Can parse an event-format profile", func() {
    25  		data, err := os.ReadFile("testdata/simple.speedscope.json")
    26  		Expect(err).ToNot(HaveOccurred())
    27  
    28  		key, err := segment.ParseKey("foo")
    29  		Expect(err).ToNot(HaveOccurred())
    30  
    31  		ingester := new(mockIngester)
    32  		profile := &RawProfile{RawData: data}
    33  
    34  		md := ingestion.Metadata{Key: key, SampleRate: 100}
    35  		err = profile.Parse(context.Background(), ingester, nil, md)
    36  		Expect(err).ToNot(HaveOccurred())
    37  
    38  		Expect(ingester.actual).To(HaveLen(1))
    39  		input := ingester.actual[0]
    40  
    41  		Expect(input.Units).To(Equal(metadata.SamplesUnits))
    42  		Expect(input.Key.Normalized()).To(Equal("foo{}"))
    43  		expectedResult := `a;b 500
    44  a;b;c 500
    45  a;b;d 400
    46  `
    47  		Expect(input.Val.String()).To(Equal(expectedResult))
    48  		Expect(input.SampleRate).To(Equal(uint32(10000)))
    49  	})
    50  
    51  	It("Can parse a sample-format profile", func() {
    52  		data, err := os.ReadFile("testdata/two-sampled.speedscope.json")
    53  		Expect(err).ToNot(HaveOccurred())
    54  
    55  		key, err := segment.ParseKey("foo{x=y}")
    56  		Expect(err).ToNot(HaveOccurred())
    57  
    58  		ingester := new(mockIngester)
    59  		profile := &RawProfile{RawData: data}
    60  
    61  		md := ingestion.Metadata{Key: key, SampleRate: 100}
    62  		err = profile.Parse(context.Background(), ingester, nil, md)
    63  		Expect(err).ToNot(HaveOccurred())
    64  
    65  		Expect(ingester.actual).To(HaveLen(2))
    66  
    67  		input := ingester.actual[0]
    68  		Expect(input.Units).To(Equal(metadata.SamplesUnits))
    69  		Expect(input.Key.Normalized()).To(Equal("foo.seconds{x=y}"))
    70  		expectedResult := `a;b 500
    71  a;b;c 500
    72  a;b;d 400
    73  `
    74  		Expect(input.Val.String()).To(Equal(expectedResult))
    75  		Expect(input.SampleRate).To(Equal(uint32(100)))
    76  	})
    77  })