forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql_perf_test.go
215 lines (195 loc) · 4.65 KB
/
mssql_perf_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package mssql
import (
"bytes"
"context"
"database/sql/driver"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"testing"
)
// tests performance of the most common operations
func runTestServer(t *testing.B, handler func(net.Conn)) *Conn {
addr := &net.TCPAddr{IP: net.IP{127, 0, 0, 1}}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
t.Fatal("Cannot start a listener", err)
}
addr = listener.Addr().(*net.TCPAddr)
go func() {
conn, err := listener.Accept()
if err != nil {
t.Log("Failed to accept connection", err)
return
}
handler(conn)
_ = conn.Close()
}()
connStr := fmt.Sprintf("host=%s;port=%d", addr.IP.String(), addr.Port)
conn, err := driverInstance.open(context.Background(), connStr)
if err != nil {
// should not fail here
t.Fatal("Open connection failed:", err.Error())
}
return conn
}
func testConnClose(t *testing.B, conn *Conn) {
err := conn.Close()
if err != nil {
t.Fatal(err)
}
}
func BenchmarkSelect(b *testing.B) {
// Benchmark select query against mock server
conn := runTestServer(b, func(conn net.Conn) {
tdsBuf := newTdsBuffer(defaultPacketSize, conn)
// read prelogin request
packetType, err := tdsBuf.BeginRead()
if err != nil {
b.Fatal("Failed to read PRELOGIN request", err)
}
if packetType != packPrelogin {
b.Fatal("Client sent non PRELOGIN request packet type", packetType)
}
// write prelogin response
fields := map[uint8][]byte{
preloginENCRYPTION: {encryptNotSup},
}
err = writePrelogin(packReply, tdsBuf, fields)
if err != nil {
b.Fatal("Writing PRELOGIN packet failed", err)
}
// read login request
packetType, err = tdsBuf.BeginRead()
if err != nil {
b.Fatal("Failed to read LOGIN request", err)
}
if packetType != packLogin7 {
b.Fatal("Client sent non LOGIN request packet type", packetType)
}
_, err = ioutil.ReadAll(tdsBuf)
if err != nil {
b.Fatal(err)
}
// send login response
tdsBuf.BeginPacket(packReply, false)
buf := make([]byte, 1+2+2+8)
buf[0] = byte(tokenDone)
binary.LittleEndian.PutUint16(buf[1:], 0)
binary.LittleEndian.PutUint16(buf[3:], 0)
binary.LittleEndian.PutUint64(buf[5:], 0)
_, err = tdsBuf.Write(buf)
if err != nil {
b.Log("writing login reply failed", err)
return
}
err = tdsBuf.FinishPacket()
if err != nil {
b.Log("writing login reply failed", err)
return
}
// this is response for select 1 request
selectResponseBytes, err := base64.StdEncoding.DecodeString("gQEAAAAAACAAOADRAQAAAP0QAMEAAQAAAAAAAAA=")
if err != nil {
b.Fatal(err)
}
for requests := 0; ; requests++ {
// read request
_, err = tdsBuf.BeginRead()
if err != nil {
b.Log(err)
return
}
_, err = ioutil.ReadAll(tdsBuf)
if err != nil {
b.Log(err)
return
}
// send response
tdsBuf.BeginPacket(packReply, false)
if err != nil {
b.Log(err)
return
}
_, err = tdsBuf.Write(selectResponseBytes)
if err != nil {
b.Log("writing login reply failed", err)
return
}
err = tdsBuf.FinishPacket()
if err != nil {
b.Log("writing login reply failed", err)
return
}
}
})
defer testConnClose(b, conn)
values := make([]driver.Value, 1)
ctx := context.Background()
for i := 0; i < b.N; i++ {
stmt, err := conn.prepareContext(ctx, "select 1")
if err != nil {
b.Fatal(err)
}
rows, err := stmt.queryContext(ctx, nil)
if err != nil {
b.Fatal(err)
}
err = rows.Next(values)
if err != nil {
b.Fatal(err)
}
err = rows.Next(values)
if err != io.EOF {
b.Fatal("there should not be a second row")
}
err = rows.Close()
if err != nil {
b.Fatal(err)
}
err = stmt.Close()
if err != nil {
b.Fatal(err)
}
}
}
type onlyReadTransport struct {
b *testing.B
rdr *bytes.Reader
}
func (transport onlyReadTransport) Read(p []byte) (n int, err error) {
return transport.rdr.Read(p)
}
func (transport onlyReadTransport) Write(p []byte) (int, error) {
return 0, errors.New("unexpected write")
}
func (transport onlyReadTransport) Close() error {
return errors.New("unexpected close")
}
func BenchmarkSelectParser(b *testing.B) {
sess := &tdsSession{
buf: newTdsBuffer(defaultPacketSize, nil),
}
// this is response for select 1 request
selectResponseBytes, err := base64.StdEncoding.DecodeString("gQEAAAAAACAAOADRAQAAAP0QAMEAAQAAAAAAAAA=")
if err != nil {
b.Fatal(err)
}
rdr := bytes.NewReader(selectResponseBytes)
sess.buf.transport = onlyReadTransport{
rdr: rdr,
b: b,
}
for i := 0; i < b.N; i++ {
ch := make(chan tokenStruct, 5)
_, err = rdr.Seek(0, io.SeekStart)
if err != nil {
b.Fatal(err)
}
processSingleResponse(context.Background(), sess, ch, outputs{})
}
}