Skip to content

Commit

Permalink
chore: various fixes and small refactorings
Browse files Browse the repository at this point in the history
- Add benchmark for slice of ints.
- Rename several variables.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Aug 2, 2022
1 parent 8a48bf0 commit 76e5695
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 79 deletions.
64 changes: 64 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,67 @@ func BenchmarkCustom(b *testing.B) {
}
}
}

func BenchmarkSlice(b *testing.B) {
type structWithSlice struct {
Field []int `protobuf:"1"`
}

type structType struct {
Field structWithSlice `protobuf:"1"`
}

o := structType{
Field: structWithSlice{
Field: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 1500, 1600},
},
}

encoded, err := protoenc.Marshal(&o)
require.NoError(b, err)

b.ResetTimer()
b.ReportAllocs()

target := &structType{}
for i := 0; i < b.N; i++ {
*target = structType{}

err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkString(b *testing.B) {
type structWithString struct {
Field string `protobuf:"1"`
}

type structType struct {
Field structWithString `protobuf:"1"`
}

o := structType{
Field: structWithString{
Field: "stuff to benchmark",
},
}

encoded, err := protoenc.Marshal(&o)
require.NoError(b, err)

b.ResetTimer()
b.ReportAllocs()

target := &structType{}
for i := 0; i < b.N; i++ {
*target = structType{}

err := protoenc.Unmarshal(encoded, target)
if err != nil {
b.Fatal(err)
}
}
}
Loading

0 comments on commit 76e5695

Please sign in to comment.