Skip to content

Commit a4ebe18

Browse files
Merge pull request #10 from ashat1701/master
Change compressed and raw format
2 parents 848fd1e + 6aec6bd commit a4ebe18

3 files changed

Lines changed: 46 additions & 18 deletions

File tree

goroutine_merger.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"compress/gzip"
66
"io"
77

8-
"github.com/threadedstream/ppmerge/profile"
98
"google.golang.org/protobuf/proto"
9+
10+
"github.com/threadedstream/ppmerge/profile"
1011
)
1112

1213
type GoroutineProfileMerger struct {
@@ -117,7 +118,7 @@ func NewGoroutineProfileUnPacker(mergedProfile *MergedGoroutineProfile) *Gorouti
117118
}
118119
}
119120

120-
func (gpu *GoroutineProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*profile.GoroutineProfile, error) {
121+
func (gpu *GoroutineProfileUnPacker) UnpackCompressed(compressedRawProfile []byte, idx uint64) (*profile.GoroutineProfile, error) {
121122
bb := bytes.NewBuffer(compressedRawProfile)
122123

123124
gzReader, err := gzip.NewReader(bb)
@@ -130,11 +131,16 @@ func (gpu *GoroutineProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx
130131
return nil, err
131132
}
132133

134+
return gpu.UnpackRaw(rawProfile, idx)
135+
}
136+
137+
func (gpu *GoroutineProfileUnPacker) UnpackRaw(rawProfile []byte, idx uint64) (*profile.GoroutineProfile, error) {
138+
133139
if gpu.mergedProfile == nil {
134140
gpu.mergedProfile = MergedGoroutineProfileFromVTPool()
135141
}
136142

137-
if err = proto.Unmarshal(rawProfile, gpu.mergedProfile); err != nil {
143+
if err := proto.Unmarshal(rawProfile, gpu.mergedProfile); err != nil {
138144
return nil, err
139145
}
140146

merge.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010

1111
pprofile "github.com/google/pprof/profile"
1212
"github.com/pkg/errors"
13-
"github.com/threadedstream/ppmerge/profile"
1413
"google.golang.org/protobuf/proto"
14+
15+
"github.com/threadedstream/ppmerge/profile"
1516
)
1617

1718
const (
@@ -54,7 +55,18 @@ func NewProfileUnPacker(mergedProfile *MergedProfile) *ProfileUnPacker {
5455
}
5556
}
5657

57-
func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*pprofile.Profile, error) {
58+
func (pu *ProfileUnPacker) UnpackRaw(rawProfile []byte, idx uint64) (*pprofile.Profile, error) {
59+
if pu.mergedProfile == nil {
60+
pu.mergedProfile = new(MergedProfile)
61+
}
62+
if err := proto.Unmarshal(rawProfile, pu.mergedProfile); err != nil {
63+
return nil, err
64+
}
65+
66+
return pu.Unpack(idx)
67+
}
68+
69+
func (pu *ProfileUnPacker) UnpackCompressed(compressedRawProfile []byte, idx uint64) (*pprofile.Profile, error) {
5870
bb := bytes.NewBuffer(compressedRawProfile)
5971

6072
gzReader, err := gzip.NewReader(bb)
@@ -66,16 +78,7 @@ func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*
6678
if err != nil {
6779
return nil, err
6880
}
69-
70-
if pu.mergedProfile == nil {
71-
pu.mergedProfile = new(MergedProfile)
72-
}
73-
74-
if err = proto.Unmarshal(rawProfile, pu.mergedProfile); err != nil {
75-
return nil, err
76-
}
77-
78-
return pu.Unpack(idx)
81+
return pu.UnpackRaw(rawProfile, idx)
7982
}
8083

8184
func (pu *ProfileUnPacker) Unpack(idx uint64) (*pprofile.Profile, error) {

merge_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
pprofile "github.com/google/pprof/profile"
1111
"github.com/stretchr/testify/require"
12+
1213
"github.com/threadedstream/ppmerge/profile"
1314
)
1415

@@ -193,10 +194,28 @@ func TestMergeUnpack(t *testing.T) {
193194
require.NoError(t, profileMerger.WriteCompressed(compressedBB))
194195
require.Greater(t, compressedBB.Len(), 0)
195196

197+
unpacker := NewProfileUnPacker(nil)
198+
p, err := unpacker.UnpackCompressed(compressedBB.Bytes(), 0)
199+
require.NoError(t, err)
200+
require.NotNil(t, p)
201+
})
202+
203+
t.Run("merge unpack raw", func(t *testing.T) {
204+
profiles := getProfilesVtProto(t, false, "hprof1", "hprof2", "hprof3", "hprof4")
205+
206+
profileMerger := NewProfileMerger()
207+
mergedProfile := profileMerger.Merge(profiles...)
208+
require.NotNil(t, mergedProfile)
209+
210+
compressedBB := bytes.NewBuffer(nil)
211+
require.NoError(t, profileMerger.WriteUncompressed(compressedBB))
212+
require.Greater(t, compressedBB.Len(), 0)
213+
196214
unpacker := NewProfileUnPacker(nil)
197215
p, err := unpacker.UnpackRaw(compressedBB.Bytes(), 0)
198216
require.NoError(t, err)
199217
require.NotNil(t, p)
218+
200219
})
201220

202221
t.Run("merge unpack debug goroutine profiles", func(t *testing.T) {
@@ -242,21 +261,21 @@ func TestMergeUnpack(t *testing.T) {
242261
require.NoError(t, profileMerger.WriteCompressed(bb))
243262

244263
unpackerOne := NewGoroutineProfileUnPacker(nil)
245-
p, err := unpackerOne.UnpackRaw(bb.Bytes(), 0)
264+
p, err := unpackerOne.UnpackCompressed(bb.Bytes(), 0)
246265
require.NoError(t, err)
247266
require.NotNil(t, p)
248267
require.Equal(t, profiles[0].GetTotal(), p.GetTotal())
249268
require.Equal(t, profiles[0].GetStacktraces(), p.GetStacktraces())
250269

251270
unpackerTwo := NewGoroutineProfileUnPacker(nil)
252-
p, err = unpackerTwo.UnpackRaw(bb.Bytes(), 1)
271+
p, err = unpackerTwo.UnpackCompressed(bb.Bytes(), 1)
253272
require.NoError(t, err)
254273
require.NotNil(t, p)
255274
require.Equal(t, profiles[1].GetTotal(), p.GetTotal())
256275
require.Equal(t, profiles[1].GetStacktraces(), p.GetStacktraces())
257276

258277
unpackerThree := NewGoroutineProfileUnPacker(nil)
259-
p, err = unpackerThree.UnpackRaw(bb.Bytes(), 2)
278+
p, err = unpackerThree.UnpackCompressed(bb.Bytes(), 2)
260279
require.NoError(t, err)
261280
require.NotNil(t, p)
262281
require.Equal(t, profiles[2].GetTotal(), p.GetTotal())

0 commit comments

Comments
 (0)