Skip to content

Commit

Permalink
use UnpackRaw method to unpack profile from compressed merged profile
Browse files Browse the repository at this point in the history
  • Loading branch information
threadedstream committed May 21, 2024
1 parent 2cd9adf commit 463f6b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ppmerge

import (
"bytes"
"compress/gzip"
"io"
"math"
Expand Down Expand Up @@ -46,6 +47,30 @@ func NewProfileUnPacker(mergedProfile *MergedProfile) *ProfileUnPacker {
}
}

func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*profile.Profile, error) {
bb := bytes.NewBuffer(compressedRawProfile)

gzReader, err := gzip.NewReader(bb)
if err != nil {
return nil, err
}

rawProfile, err := io.ReadAll(gzReader)
if err != nil {
return nil, err
}

if pu.mergedProfile == nil {
pu.mergedProfile = new(MergedProfile)
}

if err = proto.Unmarshal(rawProfile, pu.mergedProfile); err != nil {
return nil, err
}

return pu.Unpack(idx)
}

func (pu *ProfileUnPacker) Unpack(idx uint64) (*profile.Profile, error) {
var p profile.Profile
if err := pu.unpackSampleTypes(&p, idx); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ func TestMergeWrite(t *testing.T) {
require.Less(t, compressedBB.Len(), noCompactBB.Len())
}

func TestMergeUnpack(t *testing.T) {
profiles := getProfiles(t, "hprof1", "hprof2", "hprof3", "hprof4")

profileMerger := NewProfileMerger()
mergedProfile := profileMerger.Merge(profiles...)
require.NotNil(t, mergedProfile)

compressedBB := bytes.NewBuffer(nil)
require.NoError(t, profileMerger.WriteCompressed(compressedBB))
require.Greater(t, compressedBB.Len(), 0)

unpacker := NewProfileUnPacker(nil)
p, err := unpacker.UnpackRaw(compressedBB.Bytes(), 0)
require.NoError(t, err)
require.NotNil(t, p)
}

func BenchmarkProfileMerger(b *testing.B) {
profiles := getProfiles(b, "hprof1", "hprof2", "hprof3", "hprof4")

Expand Down

0 comments on commit 463f6b4

Please sign in to comment.