forked from jackc/go_db_bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoalloc_test.go
125 lines (101 loc) · 2.71 KB
/
noalloc_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
package main
import (
"context"
"encoding/binary"
"fmt"
"io"
"testing"
"github.com/jackc/pgconn"
"github.com/jackc/pgproto3/v2"
"github.com/jackc/pgtype"
)
func BenchmarkPgConnSelectMultipleRowsWithWithDecodeBinaryZeroAlloc(b *testing.B) {
setup(b)
config, err := pgconn.ParseConfig("")
if err != nil {
b.Fatalf("pgconn.Connect() failed: %v", err)
}
var cr *ChunkReader
config.BuildFrontend = func(r io.Reader, w io.Writer) pgconn.Frontend {
cr, err = NewConfig(r, Config{MinBufLen: 64 * 1024})
if err != nil {
panic(fmt.Sprintf("BUG: chunkreader.NewConfig failed: %v", err))
}
frontend := pgproto3.NewFrontend(cr, w)
return frontend
}
pgConn, err := pgconn.ConnectConfig(context.Background(), config)
if err != nil {
b.Fatalf("pgconn.Connect() failed: %v", err)
}
_, err = pgConn.Prepare(context.Background(), "selectMultiplePeople", selectMultiplePeopleSQL, nil)
if err != nil {
b.Fatalf("pgConn.Prepare() failed: %v", err)
}
type personRaw struct {
Id pgtype.Int4
FirstName pgtype.GenericBinary
LastName pgtype.GenericBinary
Sex pgtype.GenericBinary
BirthDate pgtype.Date
Weight pgtype.Int4
Height pgtype.Int4
UpdateTime pgtype.Timestamptz
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := randPersonIDs[i%len(randPersonIDs)]
buf := []byte{0, 0, 0, 0}
binary.BigEndian.PutUint32(buf, uint32(id))
rr := pgConn.ExecPrepared(context.Background(), "selectMultiplePeople", [][]byte{buf}, []int16{1}, []int16{1})
var p personRaw
for rr.NextRow() {
var err error
vi := 0
err = p.Id.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.FirstName.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.LastName.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.Sex.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.BirthDate.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.Weight.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.Height.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
vi += 1
err = p.UpdateTime.DecodeBinary(nil, rr.Values()[vi])
if err != nil {
b.Fatalf("DecodeBinary failed: %v", err)
}
}
_, err := rr.Close()
if err != nil {
b.Fatalf("pgConn.ExecPrepared failed: %v", err)
}
cr.Reset()
}
}