diff --git a/composite_bench_test.go b/composite_bench_test.go index 323c3179..429ce9b3 100644 --- a/composite_bench_test.go +++ b/composite_bench_test.go @@ -9,12 +9,12 @@ import ( ) type MyCompositeRaw struct { - a int32 - b *string + A int32 + B *string } func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) { - a := pgtype.Int4{src.a, pgtype.Present} + a := pgtype.Int4{src.A, pgtype.Present} fieldBytes := make([]byte, 0, 64) fieldBytes, _ = a.EncodeBinary(ci, fieldBytes[:0]) @@ -22,8 +22,8 @@ func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf 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]) + 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) @@ -60,11 +60,11 @@ func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { return err } - dst.a = a.Int + dst.A = a.Int if b.Status == pgtype.Present { - dst.b = &b.String + dst.B = &b.String } else { - dst.b = nil + dst.B = nil } return nil @@ -96,7 +96,7 @@ func BenchmarkBinaryEncodingHelper(b *testing.B) { x = buf } -func BenchmarkBinaryEncodingRow(b *testing.B) { +func BenchmarkBinaryEncodingComposite(b *testing.B) { buf := make([]byte, 0, 128) ci := pgtype.NewConnInfo() f1 := 2 @@ -111,6 +111,20 @@ func BenchmarkBinaryEncodingRow(b *testing.B) { x = buf } +func BenchmarkBinaryEncodingJSON(b *testing.B) { + buf := make([]byte, 0, 128) + ci := pgtype.NewConnInfo() + v := MyCompositeRaw{4, ptrS("ABCDEFG")} + j := pgtype.JSON{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + j.Set(v) + buf, _ = j.EncodeBinary(ci, buf[:0]) + } + x = buf +} + var dstRaw MyCompositeRaw func BenchmarkBinaryDecodingManual(b *testing.B) { @@ -161,3 +175,22 @@ func BenchmarkBinaryDecodingCompositeScan(b *testing.B) { gf1 = f1 gf2 = f2 } + +func BenchmarkBinaryDecodingJSON(b *testing.B) { + ci := pgtype.NewConnInfo() + j := pgtype.JSON{} + j.Set(MyCompositeRaw{4, ptrS("ABCDEFG")}) + buf, _ := j.EncodeBinary(ci, nil) + + j = pgtype.JSON{} + dst := MyCompositeRaw{} + + b.ResetTimer() + for n := 0; n < b.N; n++ { + err := j.DecodeBinary(ci, buf) + E(err) + err = j.AssignTo(&dst) + E(err) + } + dstRaw = dst +}