Add binary decoding benchmarks
``` BenchmarkBinaryDecodingManual-4 10479085 106 ns/op 40 B/op 2 allocs/op BenchmarkBinaryDecodingHelpers-4 4485451 263 ns/op 64 B/op 4 allocs/op BenchmarkBinaryDecodingRow-4 1999726 587 ns/op 96 B/op 5 allocs/op ```
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/jackc/pgtype"
|
"github.com/jackc/pgtype"
|
||||||
"github.com/jackc/pgtype/binary"
|
"github.com/jackc/pgtype/binary"
|
||||||
|
errors "golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MyCompositeRaw struct {
|
type MyCompositeRaw struct {
|
||||||
@@ -30,6 +31,45 @@ func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||||
|
a := pgtype.Int4{}
|
||||||
|
b := pgtype.Text{}
|
||||||
|
|
||||||
|
fieldIter, fieldCount, err := binary.NewRecordFieldIterator(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if 2 != fieldCount {
|
||||||
|
return errors.Errorf("can't scan row value, number of fields don't match: found=%d expected=2", fieldCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, fieldBytes, eof, err := fieldIter.Next()
|
||||||
|
if eof || err != nil {
|
||||||
|
return errors.New("Bad record")
|
||||||
|
}
|
||||||
|
if err = a.DecodeBinary(ci, fieldBytes); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, fieldBytes, eof, err = fieldIter.Next()
|
||||||
|
if eof || err != nil {
|
||||||
|
return errors.New("Bad record")
|
||||||
|
}
|
||||||
|
if err = b.DecodeBinary(ci, fieldBytes); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dst.a = a.Int
|
||||||
|
if b.Status == pgtype.Present {
|
||||||
|
dst.b = &b.String
|
||||||
|
} else {
|
||||||
|
dst.b = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var x []byte
|
var x []byte
|
||||||
|
|
||||||
func BenchmarkBinaryEncodingManual(b *testing.B) {
|
func BenchmarkBinaryEncodingManual(b *testing.B) {
|
||||||
@@ -70,3 +110,52 @@ func BenchmarkBinaryEncodingRow(b *testing.B) {
|
|||||||
}
|
}
|
||||||
x = buf
|
x = buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dstRaw MyCompositeRaw
|
||||||
|
|
||||||
|
func BenchmarkBinaryDecodingManual(b *testing.B) {
|
||||||
|
ci := pgtype.NewConnInfo()
|
||||||
|
buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
|
||||||
|
dst := MyCompositeRaw{}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
err := dst.DecodeBinary(ci, buf)
|
||||||
|
E(err)
|
||||||
|
}
|
||||||
|
dstRaw = dst
|
||||||
|
}
|
||||||
|
|
||||||
|
var dstMyType MyType
|
||||||
|
|
||||||
|
func BenchmarkBinaryDecodingHelpers(b *testing.B) {
|
||||||
|
ci := pgtype.NewConnInfo()
|
||||||
|
buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
|
||||||
|
dst := MyType{}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
err := dst.DecodeBinary(ci, buf)
|
||||||
|
E(err)
|
||||||
|
}
|
||||||
|
dstMyType = dst
|
||||||
|
}
|
||||||
|
|
||||||
|
var gf1 int
|
||||||
|
var gf2 *string
|
||||||
|
|
||||||
|
func BenchmarkBinaryDecodingRow(b *testing.B) {
|
||||||
|
ci := pgtype.NewConnInfo()
|
||||||
|
buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
|
||||||
|
var isNull bool
|
||||||
|
var f1 int
|
||||||
|
var f2 *string
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
err := pgtype.Row(&isNull, &f1, &f2).DecodeBinary(ci, buf)
|
||||||
|
E(err)
|
||||||
|
}
|
||||||
|
gf1 = f1
|
||||||
|
gf2 = f2
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user