Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions goroutine_merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"compress/gzip"
"io"

"github.com/threadedstream/ppmerge/profile"
"google.golang.org/protobuf/proto"

"github.com/threadedstream/ppmerge/profile"
)

type GoroutineProfileMerger struct {
Expand Down Expand Up @@ -117,7 +118,7 @@ func NewGoroutineProfileUnPacker(mergedProfile *MergedGoroutineProfile) *Gorouti
}
}

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

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

return gpu.UnpackRaw(rawProfile, idx)
}

func (gpu *GoroutineProfileUnPacker) UnpackRaw(rawProfile []byte, idx uint64) (*profile.GoroutineProfile, error) {

if gpu.mergedProfile == nil {
gpu.mergedProfile = MergedGoroutineProfileFromVTPool()
}

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

Expand Down
27 changes: 15 additions & 12 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

pprofile "github.com/google/pprof/profile"
"github.com/pkg/errors"
"github.com/threadedstream/ppmerge/profile"
"google.golang.org/protobuf/proto"

"github.com/threadedstream/ppmerge/profile"
)

const (
Expand Down Expand Up @@ -54,7 +55,18 @@ func NewProfileUnPacker(mergedProfile *MergedProfile) *ProfileUnPacker {
}
}

func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*pprofile.Profile, error) {
func (pu *ProfileUnPacker) UnpackRaw(rawProfile []byte, idx uint64) (*pprofile.Profile, error) {
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) UnpackCompressed(compressedRawProfile []byte, idx uint64) (*pprofile.Profile, error) {
bb := bytes.NewBuffer(compressedRawProfile)

gzReader, err := gzip.NewReader(bb)
Expand All @@ -66,16 +78,7 @@ func (pu *ProfileUnPacker) UnpackRaw(compressedRawProfile []byte, idx uint64) (*
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)
return pu.UnpackRaw(rawProfile, idx)
}

func (pu *ProfileUnPacker) Unpack(idx uint64) (*pprofile.Profile, error) {
Expand Down
25 changes: 22 additions & 3 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

pprofile "github.com/google/pprof/profile"
"github.com/stretchr/testify/require"

"github.com/threadedstream/ppmerge/profile"
)

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

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

t.Run("merge unpack raw", func(t *testing.T) {
profiles := getProfilesVtProto(t, false, "hprof1", "hprof2", "hprof3", "hprof4")

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

compressedBB := bytes.NewBuffer(nil)
require.NoError(t, profileMerger.WriteUncompressed(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)

})

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

unpackerOne := NewGoroutineProfileUnPacker(nil)
p, err := unpackerOne.UnpackRaw(bb.Bytes(), 0)
p, err := unpackerOne.UnpackCompressed(bb.Bytes(), 0)
require.NoError(t, err)
require.NotNil(t, p)
require.Equal(t, profiles[0].GetTotal(), p.GetTotal())
require.Equal(t, profiles[0].GetStacktraces(), p.GetStacktraces())

unpackerTwo := NewGoroutineProfileUnPacker(nil)
p, err = unpackerTwo.UnpackRaw(bb.Bytes(), 1)
p, err = unpackerTwo.UnpackCompressed(bb.Bytes(), 1)
require.NoError(t, err)
require.NotNil(t, p)
require.Equal(t, profiles[1].GetTotal(), p.GetTotal())
require.Equal(t, profiles[1].GetStacktraces(), p.GetStacktraces())

unpackerThree := NewGoroutineProfileUnPacker(nil)
p, err = unpackerThree.UnpackRaw(bb.Bytes(), 2)
p, err = unpackerThree.UnpackCompressed(bb.Bytes(), 2)
require.NoError(t, err)
require.NotNil(t, p)
require.Equal(t, profiles[2].GetTotal(), p.GetTotal())
Expand Down