Skip to content

Commit

Permalink
test: use integers.json file to benchmark integer decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Mar 20, 2022
1 parent 8bfec7d commit 7787838
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
58 changes: 40 additions & 18 deletions dec_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,50 @@ import (
)

func BenchmarkDecoder_Int(b *testing.B) {
data := []byte(`69315063`)
d := GetDecoder()
for i := 0; i < b.N; i++ {
d.ResetBytes(data)
if _, err := d.Int(); err != nil {
b.Fatal(err)
}
}
runTestdataFile("integers.json", b.Fatal, func(name string, data []byte) {
b.Run(name, func(b *testing.B) {
d := GetDecoder()
cb := func(d *Decoder) error {
_, err := d.Int()
return err
}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
d.ResetBytes(data)

if err := d.Arr(cb); err != nil {
b.Fatal(err)
}
}
})
})
}

func BenchmarkDecoder_Uint(b *testing.B) {
data := []byte(`69315063`)
d := GetDecoder()
for i := 0; i < b.N; i++ {
d.ResetBytes(data)
if _, err := d.UInt(); err != nil {
b.Fatal(err)
}
}
runTestdataFile("integers.json", b.Fatal, func(name string, data []byte) {
b.Run(name, func(b *testing.B) {
d := GetDecoder()
cb := func(d *Decoder) error {
_, err := d.UInt()
return err
}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
d.ResetBytes(data)

if err := d.Arr(cb); err != nil {
b.Fatal(err)
}
}
})
})
}

func TestDecoder_int_sizes(t *testing.T) {
func TestDecoderIntSizes(t *testing.T) {
data := []byte(`69315063`)
d := GetDecoder()
for _, size := range []int{32, 64} {
Expand All @@ -39,7 +61,7 @@ func TestDecoder_int_sizes(t *testing.T) {
}
}

func TestDecoder_uint_sizes(t *testing.T) {
func TestDecoderUintSizes(t *testing.T) {
data := []byte(`69315063`)
d := GetDecoder()
for _, size := range []int{32, 64} {
Expand Down
6 changes: 3 additions & 3 deletions enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestEncoder_byte_should_grow_buffer(t *testing.T) {
func TestEncoderByteShouldGrowBuffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.byte('1')
Expand Down Expand Up @@ -41,14 +41,14 @@ func TestEncoder(t *testing.T) {
})
}

func TestEncoder_Raw_should_grow_buffer(t *testing.T) {
func TestEncoderRawShouldGrowBuffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.RawStr("123")
should.Equal("123", string(e.Bytes()))
}

func TestEncoder_Str_should_grow_buffer(t *testing.T) {
func TestEncoderStrShouldGrowBuffer(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.Str("123")
Expand Down

0 comments on commit 7787838

Please sign in to comment.