Add benchmark for various composite encoder implementations
``` BenchmarkBinaryEncodingManual-12 824053234 28.9 ns/op 0 B/op 0 allocs/op BenchmarkBinaryEncodingHelper-12 76815436 314 ns/op 192 B/op 5 allocs/op BenchmarkBinaryEncodingRow-12 65302958 364 ns/op 192 B/op 5 allocs/op ```
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package pgtype_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgtype"
|
||||
"github.com/jackc/pgtype/binary"
|
||||
)
|
||||
|
||||
type MyCompositeRaw struct {
|
||||
a int32
|
||||
b *string
|
||||
}
|
||||
|
||||
func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) {
|
||||
a := pgtype.Int4{src.a, pgtype.Present}
|
||||
|
||||
fieldBytes := make([]byte, 0, 64)
|
||||
fieldBytes, _ = a.EncodeBinary(ci, fieldBytes[:0])
|
||||
|
||||
newBuf = binary.RecordStart(buf, 2)
|
||||
newBuf = binary.RecordAdd(newBuf, pgtype.Int4OID, fieldBytes)
|
||||
|
||||
if src.b != nil {
|
||||
fieldBytes, _ = pgtype.Text{*src.b, pgtype.Present}.EncodeBinary(ci, fieldBytes[:0])
|
||||
newBuf = binary.RecordAdd(newBuf, pgtype.TextOID, fieldBytes)
|
||||
} else {
|
||||
newBuf = binary.RecordAddNull(newBuf, pgtype.TextOID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var x []byte
|
||||
|
||||
func BenchmarkBinaryEncodingManual(b *testing.B) {
|
||||
buf := make([]byte, 0, 128)
|
||||
ci := pgtype.NewConnInfo()
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
v := MyCompositeRaw{4, ptrS("ABCDEFG")}
|
||||
buf, _ = v.EncodeBinary(ci, buf[:0])
|
||||
}
|
||||
x = buf
|
||||
}
|
||||
|
||||
func BenchmarkBinaryEncodingHelper(b *testing.B) {
|
||||
buf := make([]byte, 0, 128)
|
||||
ci := pgtype.NewConnInfo()
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
v := MyType{4, ptrS("ABCDEFG")}
|
||||
buf, _ = v.EncodeBinary(ci, buf[:0])
|
||||
}
|
||||
x = buf
|
||||
}
|
||||
|
||||
func BenchmarkBinaryEncodingRow(b *testing.B) {
|
||||
buf := make([]byte, 0, 128)
|
||||
ci := pgtype.NewConnInfo()
|
||||
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
c := pgtype.Composite(&pgtype.Int4{}, &pgtype.Text{})
|
||||
c.Set(pgtype.Row(2, "bar"))
|
||||
buf, _ = c.EncodeBinary(ci, buf[:0])
|
||||
}
|
||||
x = buf
|
||||
}
|
||||
Reference in New Issue
Block a user