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

     1  package convert
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"fmt"
     7  	"os"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/pyroscope-io/pyroscope/pkg/agent/spy"
    12  )
    13  
    14  var _ = Describe("convert", func() {
    15  	Describe("ParsePprof", func() {
    16  		It("parses data correctly", func() {
    17  			result := []string{}
    18  
    19  			b, err := os.ReadFile("testdata/cpu.pprof")
    20  			Expect(err).ToNot(HaveOccurred())
    21  			r := bytes.NewReader(b)
    22  			g, err := gzip.NewReader(r)
    23  			Expect(err).ToNot(HaveOccurred())
    24  			p, err := ParsePprof(g)
    25  			Expect(err).ToNot(HaveOccurred())
    26  
    27  			p.Get("samples", func(labels *spy.Labels, name []byte, val int) error {
    28  				result = append(result, fmt.Sprintf("%s %d", name, val))
    29  				return nil
    30  			})
    31  			Expect(result).To(ContainElement("runtime.main;main.work 1"))
    32  		})
    33  	})
    34  
    35  	Describe("ParseGroups", func() {
    36  		It("parses data correctly", func() {
    37  			r := bytes.NewReader([]byte("foo;bar 10\nfoo;baz 20\n"))
    38  			result := []string{}
    39  			ParseGroups(r, func(name []byte, val int) {
    40  				result = append(result, fmt.Sprintf("%s %d", name, val))
    41  			})
    42  			Expect(result).To(ConsistOf("foo;bar 10", "foo;baz 20"))
    43  		})
    44  	})
    45  
    46  	Describe("ParseIndividualLines", func() {
    47  		It("parses data correctly", func() {
    48  			r := bytes.NewReader([]byte("foo;bar\nfoo;baz\n"))
    49  			result := []string{}
    50  			ParseIndividualLines(r, func(name []byte, val int) {
    51  				result = append(result, fmt.Sprintf("%s %d", name, val))
    52  			})
    53  			Expect(result).To(ConsistOf("foo;bar 1", "foo;baz 1"))
    54  		})
    55  	})
    56  })