forked from brandur/connections-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
256 lines (210 loc) · 5.33 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
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"math"
"math/rand"
"os"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/jamiealquiza/tachymeter"
_ "github.com/lib/pq"
)
func main() {
loopsFlag := flag.Int("loops", 450, "number of concurrent conns")
flag.Parse()
var numLoops = *loopsFlag
var numTables = 50
dbURL := os.Getenv("DATABASE_URL")
db, err := sql.Open("postgres", dbURL)
if err != nil {
panic(fmt.Errorf("error opening database: %v", err))
}
db.SetConnMaxLifetime(time.Duration(-1))
db.SetMaxOpenConns(numLoops)
db.SetMaxIdleConns(numLoops)
fmt.Printf("\n")
fmt.Printf("Establishing connections\n")
fmt.Printf("------------------------\n")
conns := make([]*sql.Conn, numLoops)
for i := 0; i < numLoops; i++ {
conns[i], err = establishConnection(db)
if err != nil {
panic(fmt.Errorf("error establishing connections: %v", err))
}
}
defer func() {
fmt.Printf("\n")
fmt.Printf("Closing connections\n")
fmt.Printf("-------------------\n")
for i := 0; i < numLoops; i++ {
err := conns[i].Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error closing connection: %v\n", err)
}
}
}()
fmt.Printf("\n")
fmt.Printf("Creating tables\n")
fmt.Printf("---------------\n")
for i := 0; i < numTables; i++ {
_, err = conns[i].ExecContext(context.TODO(), fmt.Sprintf(`
CREATE TABLE "users_%v" (
id BIGSERIAL,
name VARCHAR(50)
)`,
i))
if err != nil {
panic(fmt.Errorf("error creating table: %v", err))
}
}
defer func() {
fmt.Printf("\n")
fmt.Printf("Dropping tables\n")
fmt.Printf("---------------\n")
for i := 0; i < numTables; i++ {
_, err = conns[i].ExecContext(context.TODO(), fmt.Sprintf(`
DROP TABLE "users_%v"
`, i))
if err != nil {
fmt.Fprintf(os.Stderr, "error dropping table: %v\n", err)
}
}
}()
fmt.Printf("\n")
fmt.Printf("Running warmup\n")
fmt.Printf("--------------\n")
// Do a couple initial runs to warm things up
for i := 0; i < numLoops; i++ {
_, err := run(conns[i], i, numTables)
if err != nil {
panic(err)
}
}
fmt.Printf("\n")
fmt.Printf("Running warmup\n")
fmt.Printf("Running benchmark\n")
fmt.Printf("-----------------\n")
fmt.Fprintf(os.Stderr, "# connections,p50,p75,p95\n")
for i := 0; i < numLoops; i++ {
err := conns[i].Close()
if err != nil {
panic(err)
}
conns[i], err = establishConnection(db)
if err != nil {
panic(err)
}
var wg sync.WaitGroup
t := tachymeter.New(&tachymeter.Config{Size: i + 1})
var numErrors int32
wg.Add(i + 1)
// Start as many Goroutines as i + 1. We'll have only one on the first
// loop, two on the second, three on the third, etc.
for j := 0; j < i+1; j++ {
conn := conns[j]
go func() {
defer wg.Done()
elapsed, err := run(conn, i, numTables)
if err != nil {
atomic.AddInt32(&numErrors, 1)
fmt.Fprintf(os.Stderr, "Error during work loop: %v\n", err)
} else {
t.AddTime(elapsed)
}
}()
}
wg.Wait()
metrics := t.Calc()
fmt.Printf("loop %v\n", i+1)
fmt.Printf("-------\n")
fmt.Printf("num errors = %v\n", numErrors)
fmt.Printf("\n")
fmt.Println(metrics.String())
fmt.Printf("\n")
fmt.Printf("\n")
fmt.Fprintf(os.Stderr, "%v,%v,%v,%v\n",
i+1,
metrics.Time.P50.Seconds(),
metrics.Time.P75.Seconds(),
metrics.Time.P95.Seconds())
}
err = db.Close()
if err != nil {
panic(fmt.Errorf("error closing database: %v", err))
}
}
const connRetries = 5
func establishConnection(db *sql.DB) (*sql.Conn, error) {
// Especially at higher parallelism, Postgres seems to have a lot of
// trouble giving us a connection. If we couldn't acquire one, retry a
// couple times with a backoff.
for i := 0; i < connRetries; i++ {
conn, err := db.Conn(context.TODO())
if err == nil {
return conn, nil
}
if i == connRetries-1 {
return nil, fmt.Errorf("error opening connection: %v", err)
}
var sleepTime float64
for j := 0; j < i+1; j++ {
sleepTime += rand.Float64()
}
// Convert seconds to nanoseconds by * 10**9
time.Sleep(time.Duration(sleepTime * math.Pow(10, 9)))
}
panic("Unreachable")
}
func run(conn *sql.Conn, workerNum, numTables int) (time.Duration, error) {
tableNum := workerNum % numTables
start := time.Now()
tx, err := conn.BeginTx(context.TODO(), nil)
if err != nil {
return time.Duration(0), fmt.Errorf("error beginning transaction: %v", err)
}
ids := make([]int64, 10)
for i := 0; i < 10; i++ {
name := uuid.New().String()
err := tx.QueryRow(fmt.Sprintf(`
INSERT INTO "users_%v" (
name
) VALUES (
$1
) RETURNING id`,
tableNum), name).Scan(&ids[i])
if err != nil {
return time.Duration(0), fmt.Errorf("error inserting row: %v", err)
}
}
for i := 0; i < 10; i++ {
var id int64
var name string
err := tx.QueryRow(fmt.Sprintf(`
SELECT * FROM "users_%v"
WHERE id = $1
`, tableNum), ids[i]).Scan(&id, &name)
if err != nil {
return time.Duration(0), fmt.Errorf("error selecting row: %v", err)
}
}
for i := 0; i < 10; i++ {
_, err := tx.Exec(fmt.Sprintf(`
DELETE FROM "users_%v"
WHERE id = $1
`, tableNum), ids[i])
if err != nil {
return time.Duration(0), fmt.Errorf("error deleting row: %v", err)
}
}
err = tx.Commit()
if err != nil {
return time.Duration(0), fmt.Errorf("error committing transaction: %v", err)
}
elapsed := time.Since(start)
return elapsed, nil
}