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

     1  package pprof
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
     8  )
     9  
    10  func Benchmark_ProfileParser(b *testing.B) {
    11  	p, err := readPprofFixture("testdata/heap.pb.gz")
    12  	if err != nil {
    13  		b.Error(err)
    14  	}
    15  	parser := NewParser(ParserConfig{})
    16  	b.ReportAllocs()
    17  	b.ResetTimer()
    18  	for i := 0; i < b.N; i++ {
    19  		parser.Reset()
    20  		if err = parser.iterate(p, false, readNoOp); err != nil {
    21  			b.Error(err)
    22  		}
    23  	}
    24  }
    25  
    26  func readPprofFixture(path string) (*tree.Profile, error) {
    27  	f, err := os.Open(path)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	defer func() {
    32  		_ = f.Close()
    33  	}()
    34  
    35  	var p tree.Profile
    36  	if err = Decode(f, &p); err != nil {
    37  		return nil, err
    38  	}
    39  	return &p, nil
    40  }
    41  
    42  func readNoOp(*tree.ValueType, tree.Labels, *tree.Tree) (bool, error) {
    43  	return false, nil
    44  }