Skip to content

Commit

Permalink
integration: Benchmark with/without release
Browse files Browse the repository at this point in the history
This allows testing both the performance of allocation itself and
allocation during standard operation (when messages are released back to
the buffer).
  • Loading branch information
matheusd committed Aug 13, 2024
1 parent 202e2e7 commit 9cb8be8
Showing 1 changed file with 59 additions and 15 deletions.
74 changes: 59 additions & 15 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"testing"
Expand Down Expand Up @@ -2223,7 +2224,7 @@ func TestFuzzedListOutOfBounds(t *testing.T) {
}
}

func benchmarkGrowth(b *testing.B, newArena func() capnp.Arena) {
func benchmarkGrowth(b *testing.B, newArena func() capnp.Arena, release bool) {
const (
fieldValue = "1234567" // carefully chosen to be word-padded

Expand All @@ -2235,7 +2236,7 @@ func benchmarkGrowth(b *testing.B, newArena func() capnp.Arena) {
b.SetBytes(totalSize)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, seg, err := capnp.NewMessage(newArena())
msg, seg, err := capnp.NewMessage(newArena())
if err != nil {
b.Fatal(err)
}
Expand All @@ -2252,23 +2253,46 @@ func benchmarkGrowth(b *testing.B, newArena func() capnp.Arena) {
b.Fatal(err)
}
}

if release {
msg.Release()
}
}
}

func BenchmarkGrowth_SingleSegment(b *testing.B) {
benchmarkGrowth(b, func() capnp.Arena { return capnp.SingleSegment(nil) })
}
func newSingleSegmentArena() capnp.Arena { return capnp.SingleSegment(nil) }
func newMultiSegmentArena() capnp.Arena { return capnp.MultiSegment(nil) }

func BenchmarkGrowth_MultiSegment(b *testing.B) {
benchmarkGrowth(b, func() capnp.Arena { return capnp.MultiSegment(nil) })
func BenchmarkGrowth(b *testing.B) {
tests := []struct {
name string
newArena func() capnp.Arena
}{{
name: "SingleSegment",
newArena: newSingleSegmentArena,
}, {
name: "MultiSegment",
newArena: newMultiSegmentArena,
}}

for _, release := range []bool{false, true} {
release := release
for i := range tests {
tc := tests[i]
name := fmt.Sprintf("%s/release=%v", tc.name, release)
b.Run(name, func(b *testing.B) {
benchmarkGrowth(b, tc.newArena, release)
})
}
}
}

func benchmarkSmallMessage(b *testing.B, newArena func() capnp.Arena) {
func benchmarkSmallMessage(b *testing.B, newArena func() capnp.Arena, release bool) {
const fieldValue = "1234567" // carefully chosen to be word-padded
b.SetBytes(8 * 9)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, seg, err := capnp.NewMessage(newArena())
msg, seg, err := capnp.NewMessage(newArena())
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -2299,13 +2323,33 @@ func benchmarkSmallMessage(b *testing.B, newArena func() capnp.Arena) {
if err := root.SetPtr(0, sub2.ToPtr()); err != nil {
b.Fatal(err)
}
}
}

func BenchmarkSmallMessage_SingleSegment(b *testing.B) {
benchmarkSmallMessage(b, func() capnp.Arena { return capnp.SingleSegment(nil) })
if release {
msg.Release()
}
}
}

func BenchmarkSmallMessage_MultiSegment(b *testing.B) {
benchmarkSmallMessage(b, func() capnp.Arena { return capnp.MultiSegment(nil) })
func BenchmarkSmallMessage(b *testing.B) {
tests := []struct {
name string
newArena func() capnp.Arena
}{{
name: "SingleSegment",
newArena: newSingleSegmentArena,
}, {
name: "MultiSegment",
newArena: newMultiSegmentArena,
}}

for _, release := range []bool{false, true} {
release := release
for i := range tests {
tc := tests[i]
name := fmt.Sprintf("%s/release=%v", tc.name, release)
b.Run(name, func(b *testing.B) {
benchmarkSmallMessage(b, tc.newArena, release)
})
}
}
}

0 comments on commit 9cb8be8

Please sign in to comment.