-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathforeachdb.go
186 lines (161 loc) · 5.88 KB
/
foreachdb.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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package bench
import (
"context"
gosql "database/sql"
"fmt"
"net"
"net/url"
"reflect"
"runtime"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
_ "github.com/cockroachdb/cockroach/pkg/ccl"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
_ "github.com/go-sql-driver/mysql" // registers the MySQL driver to gosql
_ "github.com/lib/pq" // registers the pg driver to gosql
"github.com/stretchr/testify/require"
)
// BenchmarkFn is a function that runs a benchmark using the given SQLRunner.
type BenchmarkFn func(b *testing.B, db *sqlutils.SQLRunner)
func benchmarkCockroach(b *testing.B, f BenchmarkFn) {
s, db, _ := serverutils.StartServer(
b, base.TestServerArgs{
UseDatabase: "bench",
DisableDefaultTestTenant: true,
})
defer s.Stopper().Stop(context.TODO())
if _, err := db.Exec(`CREATE DATABASE bench`); err != nil {
b.Fatal(err)
}
f(b, sqlutils.MakeSQLRunner(db))
}
// benchmarkTenantCockroach runs the benchmark against an in-memory tenant in a
// single-node cluster.
func benchmarkTenantCockroach(b *testing.B, f BenchmarkFn) {
ctx := context.Background()
s, db, _ := serverutils.StartServer(
b, base.TestServerArgs{
UseDatabase: "bench",
DisableDefaultTestTenant: true,
})
defer s.Stopper().Stop(ctx)
// Create our own test tenant with a known name.
tenantName := "benchtenant"
_, err := db.Exec("SELECT crdb_internal.create_tenant(10, $1)", tenantName)
require.NoError(b, err)
// Get a SQL connection to the test tenant.
sqlAddr := s.(*server.TestServer).TestingGetSQLAddrForTenant(ctx, tenantName)
tenantDB := serverutils.OpenDBConn(b, sqlAddr, "bench", false, s.Stopper())
// The benchmarks sometime hit the default span limit, so we increase it.
// NOTE(andrei): Benchmarks drop the tables they're creating, so I'm not sure
// if hitting this limit is expected.
_, err = db.Exec(`ALTER TENANT ALL SET CLUSTER SETTING "spanconfig.tenant_limit" = 10000000`)
require.NoError(b, err)
_, err = tenantDB.Exec(`CREATE DATABASE bench`)
require.NoError(b, err)
f(b, sqlutils.MakeSQLRunner(tenantDB))
}
func benchmarkMultinodeCockroach(b *testing.B, f BenchmarkFn) {
tc := testcluster.StartTestCluster(b, 3,
base.TestClusterArgs{
ReplicationMode: base.ReplicationAuto,
ServerArgs: base.TestServerArgs{
UseDatabase: "bench",
DisableDefaultTestTenant: true,
},
})
if _, err := tc.Conns[0].Exec(`CREATE DATABASE bench`); err != nil {
b.Fatal(err)
}
defer tc.Stopper().Stop(context.TODO())
f(b, sqlutils.MakeRoundRobinSQLRunner(tc.Conns[0], tc.Conns[1], tc.Conns[2]))
}
func benchmarkPostgres(b *testing.B, f BenchmarkFn) {
// Note: the following uses SSL. To run this, make sure your local
// Postgres server has SSL enabled. To use Cockroach's checked-in
// testing certificates for Postgres' SSL, first determine the
// location of your Postgres server's configuration file:
// ```
// $ psql -h localhost -p 5432 -c 'SHOW config_file'
// config_file
// -----------------------------------------
// /usr/local/var/postgres/postgresql.conf
// (1 row)
//```
//
// Now open this file and set the following values:
// ```
// $ grep ^ssl /usr/local/var/postgres/postgresql.conf
// ssl = on # (change requires restart)
// ssl_cert_file = '$GOPATH/src/github.com/cockroachdb/cockroach/pkg/security/securitytest/test_certs/node.crt' # (change requires restart)
// ssl_key_file = '$GOPATH/src/github.com/cockroachdb/cockroach/pkg/security/securitytest/test_certs/node.key' # (change requires restart)
// ssl_ca_file = '$GOPATH/src/github.com/cockroachdb/cockroach/pkg/security/securitytest/test_certs/ca.crt' # (change requires restart)
// ```
// Where `$GOPATH/src/github.com/cockroachdb/cockroach`
// is replaced with your local Cockroach source directory.
// Be sure to restart Postgres for this to take effect.
pgURL := url.URL{
Scheme: "postgres",
Host: "localhost:5432",
RawQuery: "sslmode=require&dbname=postgres",
}
if conn, err := net.Dial("tcp", pgURL.Host); err != nil {
skip.IgnoreLintf(b, "unable to connect to postgres server on %s: %s", pgURL.Host, err)
} else {
conn.Close()
}
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
b.Fatal(err)
}
defer db.Close()
r := sqlutils.MakeSQLRunner(db)
r.Exec(b, `CREATE SCHEMA IF NOT EXISTS bench`)
f(b, r)
}
func benchmarkMySQL(b *testing.B, f BenchmarkFn) {
const addr = "localhost:3306"
if conn, err := net.Dial("tcp", addr); err != nil {
skip.IgnoreLintf(b, "unable to connect to mysql server on %s: %s", addr, err)
} else {
conn.Close()
}
db, err := gosql.Open("mysql", fmt.Sprintf("root@tcp(%s)/", addr))
if err != nil {
b.Fatal(err)
}
defer db.Close()
r := sqlutils.MakeSQLRunner(db)
r.Exec(b, `CREATE DATABASE IF NOT EXISTS bench`)
f(b, r)
}
// ForEachDB iterates the given benchmark over multiple database engines.
func ForEachDB(b *testing.B, fn BenchmarkFn) {
for _, dbFn := range []func(*testing.B, BenchmarkFn){
benchmarkCockroach,
benchmarkTenantCockroach,
benchmarkMultinodeCockroach,
benchmarkPostgres,
benchmarkMySQL,
} {
dbName := runtime.FuncForPC(reflect.ValueOf(dbFn).Pointer()).Name()
dbName = strings.TrimPrefix(dbName, "github.com/cockroachdb/cockroach/pkg/bench.benchmark")
b.Run(dbName, func(b *testing.B) {
dbFn(b, fn)
})
}
}