-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
402 lines (359 loc) · 9.87 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"database/sql"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
_ "github.com/go-sql-driver/mysql"
)
var (
//params
loop bool
sleep int
reconnectInterval int
url, port, user, password string
httpPort string
pcapFileName string
pcapFilter string
regexpr string
displayBytesLimit int
srcPort, dstPort string
displayBinary bool
displayText bool
e string //execute cmd
isQuery bool
interval time.Duration
ipath string
runCount int
runStart time.Time
reqCount int
)
type TestCase int
const (
ShortConn TestCase = iota
ISSUE15190 // save_query_result
ExecInFrontendSql
Load
DumpMysql
ExecCommand
Prepare
Split
CaseEnd
)
var kase2str = map[TestCase]string{
ShortConn: "shortconn",
ISSUE15190: "issue15190",
ExecInFrontendSql: "composite",
Load: "load",
DumpMysql: "dumpmysql",
ExecCommand: "execcommand",
Prepare: "prepare",
Split: "split",
}
func (kase TestCase) String() string {
return kase2str[kase]
}
func allTestCases() []string {
ret := make([]string, 0)
for _, s := range kase2str {
ret = append(ret, s)
}
return ret
}
var kase TestCase
var conn *sql.DB
var logger *zap.Logger
var moStartTime time.Time
const maxLen = 100
var failedResults = &MultiResult{
maxCount: maxLen,
outFile: "failedrs.csv",
}
var lastResults = &MultiResult{
maxCount: 10,
outFile: "lastrs.csv",
}
var kases = []*testKase{
{
sql: "select now()",
hook: func(kase *testKase, startTime, endTime, _ time.Time) {
if ptr, ok := kase.dst[0].(*time.Time); ok {
moStartTime = *ptr
//fmt.Fprintf(os.Stderr, "now: %v\n", moTimeNow)
}
},
dst: []any{
new(time.Time),
},
},
{
sql: "select statement,account,status,response_at from system.statement_info order by response_at desc limit 5",
hook: func(kase *testKase, startTime, endTime, moTimeNow time.Time) {
if ptr, ok := kase.dst[3].(*time.Time); ok {
r := &Result{
localQueryStart: startTime,
localQueryEnd: endTime,
moTimeNow: moTimeNow,
statement: *kase.dst[0].(*string),
account: *kase.dst[1].(*string),
status: *kase.dst[2].(*string),
response_at: *kase.dst[3].(*time.Time),
}
if ptr.Before(moTimeNow.Add(-time.Hour)) {
//fmt.Fprintf(os.Stderr,"invalid time. statement: %s, account: %s, status: %s, response_at: %v\n",
// *kase.dst[0].(*string),
// *kase.dst[1].(*string),
// *kase.dst[2].(*string),
// *kase.dst[3].(*time.Time))
failedResults.Append(r)
}
lastResults.Append(r)
}
},
dst: []any{
new(string),
new(string),
new(string),
new(time.Time),
},
},
}
func main() {
flag.IntVar((*int)(&kase), "testcase", int(ShortConn), "test case kind")
flag.IntVar(&reqCount, "reqcount", 100, "request count per second")
flag.BoolVar(&loop, "loop", false, "loop")
flag.IntVar(&sleep, "sleep", 60, "sleep timeout seconds")
flag.IntVar(&reconnectInterval, "reconnect-gap", 30, "reconnect interval seconds")
flag.StringVar(&url, "url", "127.0.0.1", "url")
flag.StringVar(&port, "port", "6001", "port")
flag.StringVar(&user, "user", "dump", "user")
flag.StringVar(&password, "password", "111", "password")
flag.StringVar(&httpPort, "http-port", "8080", "http port")
flag.StringVar(&pcapFileName, "pcap-fname", "", "pcap file name")
flag.StringVar(&pcapFilter, "pcap-filter", "", "pcap filter")
flag.StringVar(®expr, "regexpr", "", "regexpr")
flag.IntVar(&displayBytesLimit, "display-bytes-limit", 0, "display bytes limit")
flag.StringVar(&srcPort, "srcport", "", "source port")
flag.StringVar(&dstPort, "dstport", "", "destination port")
flag.BoolVar(&displayBinary, "display-binary", false, "display binary")
flag.BoolVar(&displayText, "display-text", true, "display text")
flag.StringVar(&e, "e", "", "execute command and exit")
flag.BoolVar(&isQuery, "isquery", true, "sql is query")
flag.DurationVar(&interval, "interval", 0, "execute the sql every interval")
flag.StringVar(&ipath, "ipath", "", "path of routine log")
flag.Parse()
logger, _ = zap.NewProduction()
defer logger.Sync()
defer func() {
logger.Info("exit")
}()
defer func() {
closeConn()
}()
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGINT)
fmt.Println("kase", kase)
switch kase {
case ShortConn:
startTicker(sigchan, reqCount)
case ISSUE15190:
issue15190()
case ExecInFrontendSql:
composite()
case Load:
load()
case DumpMysql:
err := analyzeDir(sigchan, pcapFileName, pcapFilter, regexpr)
if err != nil {
logger.Error(err.Error())
}
case ExecCommand:
err := execCmd(sigchan, e, isQuery, interval)
if err != nil {
logger.Error(err.Error())
}
case Prepare:
err := prepare()
if err != nil {
logger.Error(err.Error())
}
case Split:
err := split(ipath)
if err != nil {
logger.Error(err.Error())
}
}
}
func test1() {
flag.BoolVar(&loop, "loop", false, "loop")
flag.IntVar(&sleep, "sleep", 60, "sleep timeout seconds")
flag.IntVar(&reconnectInterval, "reconnect-gap", 30, "reconnect interval seconds")
flag.StringVar(&url, "url", "127.0.0.1", "url")
flag.StringVar(&port, "port", "6001", "port")
flag.StringVar(&user, "user", "dump", "user")
flag.StringVar(&password, "password", "111", "password")
flag.StringVar(&httpPort, "http-port", "8080", "http port")
flag.Parse()
failedResults.Init()
defer failedResults.Close()
lastResults.Init()
defer lastResults.Close()
go httpServer()
runStart = time.Now()
runCases()
runCount++
for loop {
runCases()
if loop {
time.Sleep(time.Duration(sleep) * time.Second)
}
runCount++
}
}
func httpServer() {
if !loop {
return
}
http.HandleFunc("/status", func(writer http.ResponseWriter, request *http.Request) {
//params
_, _ = writer.Write([]byte(fmt.Sprintf("loop %v\n", loop)))
_, _ = writer.Write([]byte(fmt.Sprintf("sleep %d\n", sleep)))
_, _ = writer.Write([]byte(fmt.Sprintf("reconnectInterval %d\n", reconnectInterval)))
_, _ = writer.Write([]byte(fmt.Sprintf("url %s\n", url)))
_, _ = writer.Write([]byte(fmt.Sprintf("port %s\n", port)))
_, _ = writer.Write([]byte(fmt.Sprintf("user %s\n", user)))
_, _ = writer.Write([]byte(fmt.Sprintf("password %s\n", password)))
_, _ = writer.Write([]byte(fmt.Sprintf("httpPort %s\n", httpPort)))
//status
_, _ = writer.Write([]byte(fmt.Sprintf("runStart %v last %v\n", runStart, time.Since(runStart))))
_, _ = writer.Write([]byte(fmt.Sprintf("runCount %d\n", runCount)))
_, _ = writer.Write([]byte(fmt.Sprintf("queryStartTime(matrixone) %v\n", moStartTime)))
_, _ = writer.Write([]byte(fmt.Sprintf("\n\n")))
_, _ = writer.Write([]byte(fmt.Sprintf("test kases:\n")))
//test sql
for i, kase := range kases {
_, _ = writer.Write([]byte(fmt.Sprintf("kase %d: sql: %s\n", i, kase.sql)))
}
_, _ = writer.Write([]byte(fmt.Sprintf("\n\n")))
printResult := func(i int, result *Result) {
_, _ = writer.Write([]byte(fmt.Sprintf("result %d: %v",
i, result.String())))
}
//last results
_, _ = writer.Write([]byte(fmt.Sprintf("last succeeded kases:\n")))
lastResults.List(printResult)
//failed results
_, _ = writer.Write([]byte(fmt.Sprintf("\n\nlast failed kases:\n")))
cnt := failedResults.List(printResult)
if cnt > 0 {
_, _ = writer.Write([]byte(fmt.Sprintf("results count: %d\n", cnt)))
} else {
_, _ = writer.Write([]byte(fmt.Sprintf("no failed result\n")))
}
})
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%s", httpPort), nil)
}
func connectDb(url, port, user, password string) (*sql.DB, error) {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/?parseTime=true", user, password, url, port))
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(-1)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
err = db.Ping()
if err != nil {
return nil, err
}
return db, err
}
func establishConn() {
var err error
var errCount int
for conn == nil {
conn, err = connectDb(url, port, user, password)
if err != nil {
logger.Error("connect db failed", zap.String("url", url), zap.String("port", port), zap.String("user", user), zap.Error(err))
if conn != nil {
_ = conn.Close()
conn = nil
}
time.Sleep(time.Duration(reconnectInterval) * time.Second)
errCount++
if errCount > 3 {
break
}
}
}
}
func closeConn() {
if conn != nil {
_ = conn.Close()
}
}
func runCases() {
establishConn()
for _, kase := range kases {
err := runCase(kase)
if err != nil {
logger.Error("run case failed", zap.String("sql", kase.sql), zap.Error(err))
break
}
}
}
func runCase(kase *testKase) error {
start := time.Now()
if kase.prepare != nil {
kase.prepare(kase, start, start, start)
}
result, err := conn.Query(kase.sql)
if err != nil {
logger.Error("query failed", zap.String("sql", kase.sql), zap.Error(err))
return err
}
defer result.Close()
end := time.Now()
if !kase.dropResult {
for result.Next() {
err = result.Scan(kase.dst...)
if err != nil {
return errors.Join(err, result.Err())
}
if kase.hook != nil {
kase.hook(kase, start, end, moStartTime)
}
}
}
return err
}
func runPrepareCase(kase *testKase, stmt *sql.Stmt, args []any) error {
start := time.Now()
if kase.prepare != nil {
kase.prepare(kase, start, start, start)
}
result, err := stmt.Query(args...)
if err != nil {
logger.Error("query failed", zap.String("sql", kase.sql), zap.Error(err))
return err
}
defer result.Close()
end := time.Now()
if !kase.dropResult {
for result.Next() {
err = result.Scan(kase.dst...)
if err != nil {
return errors.Join(err, result.Err())
}
if kase.hook != nil {
kase.hook(kase, start, end, moStartTime)
}
}
}
return err
}