forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtbench.go
238 lines (195 loc) · 5.48 KB
/
vtbench.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright 2018 The Vitess Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtbench
import (
"context"
"fmt"
"strings"
"sync"
"time"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
)
// ClientProtocol indicates how to connect
type ClientProtocol int
const (
// MySQL uses the mysql wire protocol
MySQL ClientProtocol = iota
// GRPCVtgate uses the grpc wire protocol to vttablet
GRPCVtgate
// GRPCVttablet uses the grpc wire protocol to vttablet
GRPCVttablet
)
// ProtocolString returns a string representation of the protocol
func (cp ClientProtocol) String() string {
switch cp {
case MySQL:
return "mysql"
case GRPCVtgate:
return "grpc-vtgate"
case GRPCVttablet:
return "grpc-vttablet"
default:
return fmt.Sprintf("unknown-protocol-%d", cp)
}
}
// ConnParams specifies how to connect to the vtgate(s)
type ConnParams struct {
Hosts []string
Port int
DB string
Username string
Password string
UnixSocket string
Protocol ClientProtocol
}
// Bench controls the test
type Bench struct {
ConnParams ConnParams
Threads int
Count int
Query string
threads []benchThread
// synchronization waitgroup for startup / completion
wg sync.WaitGroup
// starting gate used to block all threads on startup
lock sync.RWMutex
Rows *stats.Counter
Bytes *stats.Counter
Timings *stats.Timings
TotalTime time.Duration
}
type benchThread struct {
b *Bench
i int
conn clientConn
query string
bindVars map[string]*querypb.BindVariable
}
// NewBench creates a new bench test
func NewBench(threads, count int, cp ConnParams, query string) *Bench {
bench := Bench{
Threads: threads,
Count: count,
ConnParams: cp,
Query: query,
Rows: stats.NewCounter("", ""),
Timings: stats.NewTimings("", "", ""),
}
return &bench
}
// Run executes the test
func (b *Bench) Run(ctx context.Context) error {
err := b.createConns(ctx)
if err != nil {
return err
}
b.createThreads(ctx)
b.runTest(ctx)
return nil
}
func (b *Bench) createConns(ctx context.Context) error {
log.V(10).Infof("creating %d client connections...", b.Threads)
start := time.Now()
reportInterval := 2 * time.Second
report := start.Add(reportInterval)
for i := 0; i < b.Threads; i++ {
host := b.ConnParams.Hosts[i%len(b.ConnParams.Hosts)]
cp := b.ConnParams
cp.Hosts = []string{host}
var conn clientConn
var err error
switch b.ConnParams.Protocol {
case MySQL:
log.V(5).Infof("connecting to %s using mysql protocol...", host)
conn = &mysqlClientConn{}
err = conn.connect(ctx, cp)
case GRPCVtgate:
log.V(5).Infof("connecting to %s using grpc vtgate protocol...", host)
conn = &grpcVtgateConn{}
err = conn.connect(ctx, cp)
case GRPCVttablet:
log.V(5).Infof("connecting to %s using grpc vttablet protocol...", host)
conn = &grpcVttabletConn{}
err = conn.connect(ctx, cp)
default:
return fmt.Errorf("unimplemented connection protocol %s", b.ConnParams.Protocol.String())
}
if err != nil {
return fmt.Errorf("error connecting to %s using %v protocol: %v", host, cp.Protocol.String(), err)
}
// XXX handle normalization and per-thread query templating
query, bindVars := b.getQuery(i)
b.threads = append(b.threads, benchThread{
b: b,
i: i,
conn: conn,
query: query,
bindVars: bindVars,
})
if time.Now().After(report) {
fmt.Printf("Created %d/%d connections after %v\n", i, b.Threads, time.Since(start))
report = time.Now().Add(reportInterval)
}
}
return nil
}
func (b *Bench) getQuery(i int) (string, map[string]*querypb.BindVariable) {
query := strings.Replace(b.Query, ":thread", fmt.Sprintf("%d", i), -1)
bindVars := make(map[string]*querypb.BindVariable)
return query, bindVars
}
func (b *Bench) createThreads(ctx context.Context) {
// Create a barrier so all the threads start at the same time
b.lock.Lock()
log.V(10).Infof("starting %d threads", b.Threads)
for i := 0; i < b.Threads; i++ {
b.wg.Add(1)
go b.threads[i].clientLoop(ctx)
}
log.V(10).Infof("waiting for %d threads to start", b.Threads)
b.wg.Wait()
b.wg.Add(b.Threads)
}
func (b *Bench) runTest(ctx context.Context) error {
start := time.Now()
fmt.Printf("Starting test threads\n")
b.lock.Unlock()
// Then wait for them all to finish looping
log.V(10).Infof("waiting for %d threads to finish", b.Threads)
b.wg.Wait()
b.TotalTime = time.Since(start)
return nil
}
func (bt *benchThread) clientLoop(ctx context.Context) {
b := bt.b
// Declare that startup is finished and wait for
// the barrier
b.wg.Done()
log.V(10).Infof("thread %d waiting for startup barrier", bt.i)
b.lock.RLock()
log.V(10).Infof("thread %d starting loop", bt.i)
for i := 0; i < b.Count; i++ {
start := time.Now()
result, err := bt.conn.execute(ctx, bt.query, bt.bindVars)
b.Timings.Record("query", start)
if err != nil {
log.Errorf("query error: %v", err)
break
} else {
b.Rows.Add(int64(result.RowsAffected))
}
}
b.wg.Done()
}