-
Notifications
You must be signed in to change notification settings - Fork 15
/
sql_benchmark_test.go
456 lines (371 loc) · 10.3 KB
/
sql_benchmark_test.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package db_sql_benchmark
import (
"database/sql"
"log"
"os"
"strconv"
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/gocraft/dbr"
"github.com/coopernurse/gorp"
"github.com/jmoiron/sqlx"
)
var mysqlDSN string
func init() {
mysqlDSN = os.Getenv("GOLANG_SQL_BENCHMARKS_DSN")
if mysqlDSN == "" {
mysqlDSN = "root@unix(/var/run/mysqld/mysqld.sock)/golang_sql_benchmarks"
}
}
// Attempts to compare different situations regarding use of prepared statements and/or interpolation
// Use a fairly simple query, but one that allows us to use placeholders or not as per our individual benchmark
// Each query should be the same regardless of how it is generated or executed
//
// select id, subject, state from tickets where subdomain_id = 1 and (state = 'open' or state = 'spam') limit 1
//
// Benchmark different prepared statment related scenarios
//
// BenchmarkPreparedStatementsNone is the case that you have a SQL string that requires no arguments/interpolation
// The database/sql package is able to issue the query directly to the server
func BenchmarkPreparedStatementsNone(b *testing.B) {
conn := mysqlConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
rows, err := conn.Query("select id, subject, state from tickets where subdomain_id = 1 and (state = 'open' or state = 'spam') limit 1")
if err != nil {
log.Fatalln(err)
}
var id int
var subject string
var state string
for rows.Next() {
err := rows.Scan(&id, &subject, &state)
if err != nil {
log.Fatalln(err)
}
}
rows.Close()
}
conn.Close()
}
// BenchmarkPreparedStatementsThrowaway is the (probably more likely) case that you have arguments you need to bind to placeholders in your SQL
// The database/sql package generates a prepared statement implicitly
func BenchmarkPreparedStatementsThrowaway(b *testing.B) {
conn := mysqlConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
rows, err := conn.Query("select id, subject, state from tickets where subdomain_id = ? and (state = ? or state = ?) limit 1", 1, "open", "spam")
if err != nil {
log.Fatalln(err)
}
var id int
var subject string
var state string
for rows.Next() {
err := rows.Scan(&id, &subject, &state)
if err != nil {
log.Fatalln(err)
}
}
rows.Close()
}
conn.Close()
}
// BenchmarkPreparedStatementsSingle is the case that the user creates a single prepared statement uses it reuses it
func BenchmarkPreparedStatementsSingle(b *testing.B) {
conn := mysqlConn()
b.ResetTimer()
stmt, err := conn.Prepare("select id, subject, state from tickets where subdomain_id = ? and (state = ? or state = ?) limit 1")
if err != nil {
log.Fatalln(err)
}
for n := 0; n < b.N; n++ {
rows, err := stmt.Query(1, "open", "spam")
if err != nil {
log.Fatalln(err)
}
var id int
var subject string
var state string
for rows.Next() {
err := rows.Scan(&id, &subject, &state)
if err != nil {
log.Fatalln(err)
}
}
rows.Close()
}
conn.Close()
}
//
// dbr
//
// Select into integers
func BenchmarkDbrSelectInts1(b *testing.B) {
benchmarkDbrSelectInts(b, 1)
}
func BenchmarkDbrSelectInts100(b *testing.B) {
benchmarkDbrSelectInts(b, 100)
}
func BenchmarkDbrSelectInts1000(b *testing.B) {
benchmarkDbrSelectInts(b, 1000)
}
func BenchmarkDbrSelectInts10000(b *testing.B) {
benchmarkDbrSelectInts(b, 10000)
}
// Select without query params
func BenchmarkDbrSelectAll1(b *testing.B) {
benchmarkDbrSelectAll(b, 1)
}
func BenchmarkDbrSelectAll100(b *testing.B) {
benchmarkDbrSelectAll(b, 100)
}
func BenchmarkDbrSelectAll1000(b *testing.B) {
benchmarkDbrSelectAll(b, 1000)
}
func BenchmarkDbrSelectAll10000(b *testing.B) {
benchmarkDbrSelectAll(b, 10000)
}
// Select with query params
func BenchmarkDbrSelectAllWithArgs1(b *testing.B) {
benchmarkDbrSelectAllWithArgs(b, 1)
}
func BenchmarkDbrSelectAllWithArgs100(b *testing.B) {
benchmarkDbrSelectAllWithArgs(b, 100)
}
func BenchmarkDbrSelectAllWithArgs1000(b *testing.B) {
benchmarkDbrSelectAllWithArgs(b, 1000)
}
func BenchmarkDbrSelectAllWithArgs10000(b *testing.B) {
benchmarkDbrSelectAllWithArgs(b, 10000)
}
//
// sqlx
//
// Select into integers
func BenchmarkSqlxSelectInts1(b *testing.B) {
benchmarkSqlxSelectInts(b, 1)
}
func BenchmarkSqlxSelectInts100(b *testing.B) {
benchmarkSqlxSelectInts(b, 100)
}
func BenchmarkSqlxSelectInts1000(b *testing.B) {
benchmarkSqlxSelectInts(b, 1000)
}
func BenchmarkSqlxSelectInts10000(b *testing.B) {
benchmarkSqlxSelectInts(b, 10000)
}
// Select without query params
func BenchmarkSqlxSelectAll1(b *testing.B) {
benchmarkSqlxSelectAll(b, 1)
}
func BenchmarkSqlxSelectAll100(b *testing.B) {
benchmarkSqlxSelectAll(b, 100)
}
func BenchmarkSqlxSelectAll1000(b *testing.B) {
benchmarkSqlxSelectAll(b, 1000)
}
func BenchmarkSqlxSelectAll10000(b *testing.B) {
benchmarkSqlxSelectAll(b, 10000)
}
// Select with query params
func BenchmarkSqlxSelectAllWithArgs1(b *testing.B) {
benchmarkSqlxSelectAllWithArgs(b, 1)
}
func BenchmarkSqlxSelectAllWithArgs100(b *testing.B) {
benchmarkSqlxSelectAllWithArgs(b, 100)
}
func BenchmarkSqlxSelectAllWithArgs1000(b *testing.B) {
benchmarkSqlxSelectAllWithArgs(b, 1000)
}
func BenchmarkSqlxSelectAllWithArgs10000(b *testing.B) {
benchmarkSqlxSelectAllWithArgs(b, 10000)
}
//
// Gorp
//
// Select into integer; Gorp only seems to support this for single ints, not slices
func BenchmarkGorpSelectInts1(b *testing.B) {
dbmap := gorpConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := dbmap.SelectInt("select id from tickets where subdomain_id = 1 limit ?", 1)
if err != nil {
log.Fatalln(err)
}
}
}
// Select without query params
func BenchmarkGorpSelectAll1(b *testing.B) {
benchmarkGorpSelectAll(b, 1)
}
func BenchmarkGorpSelectAll100(b *testing.B) {
benchmarkGorpSelectAll(b, 100)
}
func BenchmarkGorpSelectAll1000(b *testing.B) {
benchmarkGorpSelectAll(b, 1000)
}
func BenchmarkGorpSelectAll10000(b *testing.B) {
benchmarkGorpSelectAll(b, 10000)
}
// Select with query params
func BenchmarkGorpSelectAllWithArgs1(b *testing.B) {
benchmarkGorpSelectAllWithArgs(b, 1)
}
func BenchmarkGorpSelectAllWithArgs100(b *testing.B) {
benchmarkGorpSelectAllWithArgs(b, 100)
}
func BenchmarkGorpSelectAllWithArgs1000(b *testing.B) {
benchmarkGorpSelectAllWithArgs(b, 1000)
}
func BenchmarkGorpSelectAllWithArgs10000(b *testing.B) {
benchmarkGorpSelectAllWithArgs(b, 10000)
}
//
// Benchmark helpers
//
func benchmarkDbrSelectAll(b *testing.B, limit int) {
sess := dbrSess()
query := "select id, subject, state from tickets where subdomain_id = 1 and (state = 'open' or state = 'spam') limit " + strconv.Itoa(limit)
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
// _, err := sess.Select("id", "subject", "state").From("tickets").Where("subdomain_id = 1 and (state = 'open' or state = 'spam')").Limit(limit).LoadStructs(&tickets)
_, err := sess.SelectBySql(query).LoadStructs(&tickets)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkDbrSelectAllWithArgs(b *testing.B, limit int) {
sess := dbrSess()
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
// _, err := sess.Select("id", "subject", "state").From("tickets").Where("subdomain_id = ? and (state = ? or state = ?)", 1, "open", "spam").Limit(limit).LoadStructs(&tickets)
_, err := sess.SelectBySql("select id, subject, state from tickets where subdomain_id = ? and (state = ? or state = ?) limit ?", 1, "open", "spam", limit).LoadStructs(&tickets)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkDbrSelectInts(b *testing.B, limit int) {
sess := dbrSess()
b.ResetTimer()
for n := 0; n < b.N; n++ {
var counts []int
_, err := sess.SelectBySql("select id from tickets where subdomain_id = 1 limit ?", limit).LoadValues(&counts)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkSqlxSelectAll(b *testing.B, limit int) {
db := sqlxConn()
query := "select id, subject, state from tickets where subdomain_id = 1 and (state = 'open' or state = 'spam') limit " + strconv.Itoa(limit)
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
err := db.Select(&tickets, query)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkSqlxSelectAllWithArgs(b *testing.B, limit int) {
db := sqlxConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
err := db.Select(&tickets, "select id, subject, state from tickets where subdomain_id = ? and (state = ? or state = ?) limit ?", 1, "open", "spam", limit)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkSqlxSelectInts(b *testing.B, limit int) {
db := sqlxConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
var counts []int
err := db.Select(&counts, "select id from tickets where subdomain_id = 1 limit ?", limit)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkGorpSelectAll(b *testing.B, limit int) {
dbmap := gorpConn()
query := "select id, subject, state from tickets where subdomain_id = 1 and (state = 'open' or state = 'spam') limit " + strconv.Itoa(limit)
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
_, err := dbmap.Select(&tickets, query)
if err != nil {
log.Fatalln(err)
}
}
}
func benchmarkGorpSelectAllWithArgs(b *testing.B, limit int) {
dbmap := gorpConn()
b.ResetTimer()
for n := 0; n < b.N; n++ {
tickets := []*struct {
Id int64
Subject string
State string
}{}
_, err := dbmap.Select(&tickets, "select id, subject, state from tickets where subdomain_id = ? and (state = ? or state = ?) limit ?", 1, "open", "spam", limit)
if err != nil {
log.Fatalln(err)
}
}
}
//
// Connection helpers
//
func mysqlConn() *sql.DB {
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
log.Fatalln(err)
}
return db
}
func dbrSess() *dbr.Session {
return dbr.NewConnection(mysqlConn(), nil).NewSession(nil)
}
func sqlxConn() *sqlx.DB {
db, err := sqlx.Connect("mysql", mysqlDSN)
if err != nil {
log.Fatalln(err)
}
return db
}
func gorpConn() *gorp.DbMap {
db, err := sql.Open("mysql", mysqlDSN)
if err != nil {
log.Fatalln(err)
}
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDb", "UTF8"}}
return dbmap
}