Skip to content

Commit

Permalink
Adds a test to verify Unmarshal's memory usage for Varint
Browse files Browse the repository at this point in the history
Validates gogo#436
  • Loading branch information
makaur committed Sep 12, 2018
1 parent 5669497 commit 89d4b68
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/packed/packed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,55 @@ import (
"unsafe"
)

func TestVarintIssue436(t *testing.T) {
n := 1 << 22 // Makes for 32 MiB

m := &runtime.MemStats{}

msgNormal := &NinRepNative{
Field8: make([]int64, n),
}
dataNormal, err := proto.Marshal(msgNormal)
if err != nil {
t.Fatal(err)
}

normalmsg := &NinRepNative{}
runtime.ReadMemStats(m)
beforeNormal := m.TotalAlloc
err = proto.Unmarshal(dataNormal, normalmsg)
runtime.ReadMemStats(m)
afterNormal := m.TotalAlloc
if err != nil {
t.Fatal(err)
}

msgPacked := &NinRepPackedNative{
Field8: make([]int64, n),
}
dataPacked, err := proto.Marshal(msgPacked)
if err != nil {
t.Fatal(err)
}

packedmsg := &NinRepPackedNative{}
runtime.ReadMemStats(m)
beforePacked := m.TotalAlloc
err = proto.Unmarshal(dataPacked, packedmsg)
runtime.ReadMemStats(m)
afterPacked := m.TotalAlloc
if err != nil {
t.Fatal(err)
}

totalNormal := afterNormal - beforeNormal
totalPacked := afterPacked - beforePacked
usedRatio := float64(totalPacked) / float64(totalNormal)
if usedRatio > 0.5 {
t.Fatalf("unmarshaling packed msg allocated too much memory:\nnormal:\t\t%d bytes\npacked:\t\t%d bytes\nused ratio:\t%.2f%%", totalNormal, totalPacked, usedRatio*100)
}
}

func TestIssue436(t *testing.T) {
n := 1 << 22 // Makes for 32 MiB

Expand Down

0 comments on commit 89d4b68

Please sign in to comment.