-
Notifications
You must be signed in to change notification settings - Fork 32
/
dbbench.go
173 lines (150 loc) · 4.24 KB
/
dbbench.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
/*
* Copyright (c) 2015-2020 by MemSQL. All rights reserved.
*
* 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"
"errors"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/vertica/vertica-sql-go"
)
func cancelOnInterrupt(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
signal.Stop(c)
cancel()
close(c)
}()
}
func runTest(db Database, df DatabaseFlavor, config *Config) {
if len(config.Setup) > 0 {
log.Printf("Performing setup")
for _, query := range config.Setup {
if _, err := db.RunQuery(nil, query, nil); err != nil {
log.Fatalf("error in setup query %q: %v", query, err)
}
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cancelOnInterrupt(cancel)
if config.Duration > 0 {
ctx, _ = context.WithTimeout(ctx, config.Duration)
}
testStats := processResults(config, makeJobResultChan(ctx, db, df, config.Jobs))
for name, stats := range testStats {
log.Printf("%s: %v", name, stats)
}
if len(config.Teardown) > 0 {
log.Printf("Performing teardown")
for _, query := range config.Teardown {
if _, err := db.RunQuery(nil, query, nil); err != nil {
log.Fatalf("error in teardown query %q: %v", query, err)
}
}
}
}
var driverName = flag.String("driver", "mysql", "Database driver to use.")
var baseDir = flag.String("base-dir", "",
"Directory to use as base for files (default directory containing runfile).")
var printVersion = flag.Bool("version", false, "Print the version and quit")
var GlobalConfig ConnectionConfig
type URLString struct {
URL *url.URL
}
func (v URLString) String() string {
if v.URL != nil {
return v.URL.String()
}
return ""
}
func (v URLString) Set(s string) error {
if s == "" {
return errors.New("empty connection URL")
} else if u, err := url.Parse(s); err != nil {
return err
} else {
GlobalConfig.OverrideFromURL(*u)
if u.Scheme != "" {
*driverName = u.Scheme
}
}
return nil
}
var u = &url.URL{}
func init() {
flag.StringVar(&GlobalConfig.Username, "username", "",
"Database connection username")
flag.StringVar(&GlobalConfig.Password, "password", "",
"Database connection password")
flag.StringVar(&GlobalConfig.Host, "host", "",
"Database connection host")
flag.IntVar(&GlobalConfig.Port, "port", 0,
"Database connection port")
flag.StringVar(&GlobalConfig.Database, "database", "",
"Database connection database")
flag.StringVar(&GlobalConfig.Params, "params", "",
"Override default connection parameters")
flag.Var(URLString{u}, "url", "Connection url (mysql://user:pass@host:port?params), parameters provided here override those provided by other options")
}
func main() {
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s [options] <runfile.ini>\n", os.Args[0])
flag.PrintDefaults()
}
if *printVersion {
fmt.Println("0.4")
return
}
if len(flag.Args()) == 0 {
flag.Usage()
log.Fatal("No config file to parse")
}
if len(flag.Args()) > 1 {
flag.Usage()
log.Fatal("Cannot have more than one config file (do you have flags after the config file??)")
}
configFile := flag.Arg(0)
if *baseDir == "" {
*baseDir = filepath.Dir(configFile)
}
flavor, ok := supportedDatabaseFlavors[*driverName]
if !ok {
log.Fatalf("Database flavor %s not supported", *driverName)
}
config, err := parseConfig(flavor, configFile, *baseDir)
if err != nil {
log.Fatalf("parsing config file %v", err)
}
if db, err := flavor.Connect(&GlobalConfig); err != nil {
log.Fatal("Error connecting to the database: ", err)
} else {
defer db.Close()
os.Chdir(*baseDir)
runTest(db, flavor, config)
}
}