forked from jackc/pgtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite_bench_test.go
192 lines (158 loc) · 3.68 KB
/
composite_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package pgtype_test
import (
"testing"
"github.com/jackc/pgio"
"github.com/jackc/pgtype"
"github.com/stretchr/testify/require"
)
type MyCompositeRaw struct {
A int32
B *string
}
func (src MyCompositeRaw) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
buf = pgio.AppendUint32(buf, 2)
buf = pgio.AppendUint32(buf, pgtype.Int4OID)
buf = pgio.AppendInt32(buf, 4)
buf = pgio.AppendInt32(buf, src.A)
buf = pgio.AppendUint32(buf, pgtype.TextOID)
if src.B != nil {
buf = pgio.AppendInt32(buf, int32(len(*src.B)))
buf = append(buf, (*src.B)...)
} else {
buf = pgio.AppendInt32(buf, -1)
}
return buf, nil
}
func (dst *MyCompositeRaw) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
a := pgtype.Int4{}
b := pgtype.Text{}
scanner := pgtype.NewCompositeBinaryScanner(ci, src)
scanner.ScanDecoder(&a)
scanner.ScanDecoder(&b)
if scanner.Err() != nil {
return scanner.Err()
}
dst.A = a.Int
if b.Status == pgtype.Present {
dst.B = &b.String
} else {
dst.B = nil
}
return nil
}
var x []byte
func BenchmarkBinaryEncodingManual(b *testing.B) {
buf := make([]byte, 0, 128)
ci := pgtype.NewConnInfo()
v := MyCompositeRaw{4, ptrS("ABCDEFG")}
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf, _ = v.EncodeBinary(ci, buf[:0])
}
x = buf
}
func BenchmarkBinaryEncodingHelper(b *testing.B) {
buf := make([]byte, 0, 128)
ci := pgtype.NewConnInfo()
v := MyType{4, ptrS("ABCDEFG")}
b.ResetTimer()
for n := 0; n < b.N; n++ {
buf, _ = v.EncodeBinary(ci, buf[:0])
}
x = buf
}
func BenchmarkBinaryEncodingComposite(b *testing.B) {
buf := make([]byte, 0, 128)
ci := pgtype.NewConnInfo()
f1 := 2
f2 := ptrS("bar")
c, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{
{"a", pgtype.Int4OID},
{"b", pgtype.TextOID},
}, ci)
require.NoError(b, err)
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.Set([]interface{}{f1, f2})
buf, _ = c.EncodeBinary(ci, buf[:0])
}
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) {
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 BenchmarkBinaryDecodingCompositeScan(b *testing.B) {
ci := pgtype.NewConnInfo()
buf, _ := MyType{4, ptrS("ABCDEFG")}.EncodeBinary(ci, nil)
var f1 int
var f2 *string
c, err := pgtype.NewCompositeType("test", []pgtype.CompositeTypeField{
{"a", pgtype.Int4OID},
{"b", pgtype.TextOID},
}, ci)
require.NoError(b, err)
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := c.DecodeBinary(ci, buf)
if err != nil {
b.Fatal(err)
}
err = c.AssignTo([]interface{}{&f1, &f2})
if err != nil {
b.Fatal(err)
}
}
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
}