-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vtbench.go
210 lines (173 loc) · 6.04 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
/*
Copyright 2019 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 main
import (
"context"
"fmt"
"strings"
"time"
"github.com/spf13/pflag"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vtbench"
// Import and register the gRPC vtgateconn client
_ "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
// Import and register the gRPC tabletconn client
_ "vitess.io/vitess/go/vt/vttablet/grpctabletconn"
)
/*
Vtbench is a simple load testing client to compare workloads in
Vitess across the various client/server protocols.
There are a number of command line options to control the behavior,
but as a basic example, the three supported client protocols are:
Mysql protocol to vtgate:
vtbench \
--protocol mysql \
--host vtgate-host.my.domain \
--port 15306 \
--user db_username \
--db-credentials-file ./vtbench_db_creds.json \
--db @replica \
--sql "select * from loadtest_table where id=123456789" \
--threads 10 \
--count 10
GRPC to vtgate:
vtbench \
--protocol grpc-vtgate \
--host vtgate-host.my.domain \
--port 15999 \
--db @replica \
$VTTABLET_GRPC_ARGS \
--sql "select * from loadtest_table where id=123456789" \
--threads 10 \
--count 10
GRPC to vttablet:
vtbench \
--protocol grpc-vttablet \
--host tablet-loadtest-00-80.my.domain \
--port 15999 \
--db loadtest/00-80@replica \
--sql "select * from loadtest_table where id=123456789" \
--threads 10 \
--count 10
*/
var (
host, unixSocket, user, db, sql string
port int
protocol = "mysql"
deadline = 5 * time.Minute
threads = 2
count = 1000
)
func initFlags(fs *pflag.FlagSet) {
fs.StringVar(&host, "host", host, "VTGate host(s) in the form 'host1,host2,...'")
fs.IntVar(&port, "port", port, "VTGate port")
fs.StringVar(&unixSocket, "unix_socket", unixSocket, "VTGate unix socket")
fs.StringVar(&protocol, "protocol", protocol, "Client protocol, either mysql (default), grpc-vtgate, or grpc-vttablet")
fs.StringVar(&user, "user", user, "Username to connect using mysql (password comes from the db-credentials-file)")
fs.StringVar(&db, "db", db, "Database name to use when connecting / running the queries (e.g. @replica, keyspace, keyspace/shard etc)")
fs.DurationVar(&deadline, "deadline", deadline, "Maximum duration for the test run (default 5 minutes)")
fs.StringVar(&sql, "sql", sql, "SQL statement to execute")
fs.IntVar(&threads, "threads", threads, "Number of parallel threads to run")
fs.IntVar(&count, "count", count, "Number of queries per thread")
grpccommon.RegisterFlags(fs)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)
acl.RegisterFlags(fs)
servenv.RegisterMySQLServerFlags(fs)
}
func main() {
servenv.OnParseFor("vtbench", func(fs *pflag.FlagSet) {
logger := logutil.NewConsoleLogger()
fs.SetOutput(logutil.NewLoggerWriter(logger))
initFlags(fs)
_ = fs.Set("logtostderr", "true")
})
servenv.ParseFlags("vtbench")
servenv.Init()
defer exit.Recover()
clientProto := vtbench.MySQL
switch protocol {
case "", "mysql":
clientProto = vtbench.MySQL
case "grpc-vtgate":
clientProto = vtbench.GRPCVtgate
case "grpc-vttablet":
clientProto = vtbench.GRPCVttablet
default:
log.Exitf("invalid client protocol %s", protocol)
}
if (host != "" || port != 0) && unixSocket != "" {
log.Exitf("can't specify both host:port and unix_socket")
}
if host != "" && port == 0 {
log.Exitf("must specify port when using host")
}
if host == "" && port != 0 {
log.Exitf("must specify host when using port")
}
if host == "" && port == 0 && unixSocket == "" {
log.Exitf("vtbench requires either host/port or unix_socket")
}
if sql == "" {
log.Exitf("must specify sql")
}
var password string
if clientProto == vtbench.MySQL {
var err error
_, password, err = dbconfigs.GetCredentialsServer().GetUserAndPassword(user)
if err != nil {
log.Exitf("error reading password for user %v from file: %v", user, err)
}
}
connParams := vtbench.ConnParams{
Hosts: strings.Split(host, ","),
Port: port,
UnixSocket: unixSocket,
Protocol: clientProto,
DB: db,
Username: user,
Password: password,
}
b := vtbench.NewBench(threads, count, connParams, sql)
ctx, cancel := context.WithTimeout(context.Background(), deadline)
defer cancel()
fmt.Printf("Initializing test with %s protocol / %d threads / %d iterations\n",
b.ConnParams.Protocol.String(), b.Threads, b.Count)
err := b.Run(ctx)
if err != nil {
log.Exitf("error in test: %v", err)
}
fmt.Printf("Average Rows Returned: %d\n", b.Rows.Get()/int64(b.Threads*b.Count))
fmt.Printf("Average Query Time: %v\n", time.Duration(b.Timings.Time()/b.Timings.Count()))
fmt.Printf("Total Test Time: %v\n", b.TotalTime)
fmt.Printf("QPS (Per Thread): %v\n", float64(b.Count)/b.TotalTime.Seconds())
fmt.Printf("QPS (Total): %v\n", float64(b.Count*b.Threads)/b.TotalTime.Seconds())
last := int64(0)
histograms := b.Timings.Histograms()
h := histograms["query"]
buckets := h.Buckets()
fmt.Printf("Query Timings:\n")
for i, bucket := range h.Cutoffs() {
count := buckets[i]
if count != 0 {
fmt.Printf("%v-%v: %v\n", time.Duration(last), time.Duration(bucket), count)
}
last = bucket
}
}