Skip to content

Commit 463f6b4

Browse files
use UnpackRaw method to unpack profile from compressed merged profile
1 parent 2cd9adf commit 463f6b4

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

merge.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ppmerge
22

33
import (
4+
"bytes"
45
"compress/gzip"
56
"io"
67
"math"
@@ -46,6 +47,30 @@ func NewProfileUnPacker(mergedProfile *MergedProfile) *ProfileUnPacker {
4647
}
4748
}
4849

50+
func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*profile.Profile, error) {
51+
bb := bytes.NewBuffer(compressedRawProfile)
52+
53+
gzReader, err := gzip.NewReader(bb)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
rawProfile, err := io.ReadAll(gzReader)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
if pu.mergedProfile == nil {
64+
pu.mergedProfile = new(MergedProfile)
65+
}
66+
67+
if err = proto.Unmarshal(rawProfile, pu.mergedProfile); err != nil {
68+
return nil, err
69+
}
70+
71+
return pu.Unpack(idx)
72+
}
73+
4974
func (pu *ProfileUnPacker) Unpack(idx uint64) (*profile.Profile, error) {
5075
var p profile.Profile
5176
if err := pu.unpackSampleTypes(&p, idx); err != nil {

merge_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ func TestMergeWrite(t *testing.T) {
118118
require.Less(t, compressedBB.Len(), noCompactBB.Len())
119119
}
120120

121+
func TestMergeUnpack(t *testing.T) {
122+
profiles := getProfiles(t, "hprof1", "hprof2", "hprof3", "hprof4")
123+
124+
profileMerger := NewProfileMerger()
125+
mergedProfile := profileMerger.Merge(profiles...)
126+
require.NotNil(t, mergedProfile)
127+
128+
compressedBB := bytes.NewBuffer(nil)
129+
require.NoError(t, profileMerger.WriteCompressed(compressedBB))
130+
require.Greater(t, compressedBB.Len(), 0)
131+
132+
unpacker := NewProfileUnPacker(nil)
133+
p, err := unpacker.UnpackRaw(compressedBB.Bytes(), 0)
134+
require.NoError(t, err)
135+
require.NotNil(t, p)
136+
}
137+
121138
func BenchmarkProfileMerger(b *testing.B) {
122139
profiles := getProfiles(b, "hprof1", "hprof2", "hprof3", "hprof4")
123140

0 commit comments

Comments
 (0)