-
Notifications
You must be signed in to change notification settings - Fork 209
/
load_benchmark_test.go
76 lines (67 loc) · 1.58 KB
/
load_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
package dbr
import (
"context"
"fmt"
"testing"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/require"
)
func BenchmarkLoadValues(b *testing.B) {
sess := mysqlSession
for _, v := range []string{
`DROP TABLE IF EXISTS suggestions`,
`CREATE TABLE suggestions (
id serial PRIMARY KEY,
title varchar(255),
body text
)`,
} {
_, err := sess.Exec(v)
require.NoError(b, err)
}
tx, err := sess.Begin()
require.NoError(b, err)
const maxRows = 100000
for i := 0; i < maxRows; i++ {
_, err := tx.InsertInto("suggestions").
Columns("title", "body").
Values("title", "body").
Exec()
require.NoError(b, err)
}
err = tx.Commit()
require.NoError(b, err)
type Suggestion struct {
Title *string
Body *string
}
for n := 10; n <= maxRows; n *= 10 {
query := fmt.Sprintf("SELECT * FROM suggestions ORDER BY id ASC LIMIT %d", n)
b.Run(fmt.Sprintf("sqlx_%d", n), func(b *testing.B) {
b.StopTimer()
db, err := sqlx.Connect("mysql", mysqlDSN)
require.NoError(b, err)
db = db.Unsafe()
defer db.Close()
for i := 0; i < b.N; i++ {
var suggs []*Suggestion
b.StartTimer()
err := db.SelectContext(context.Background(), &suggs, query)
b.StopTimer()
require.NoError(b, err)
require.Len(b, suggs, n)
}
})
b.Run(fmt.Sprintf("dbr_%d", n), func(b *testing.B) {
b.StopTimer()
for i := 0; i < b.N; i++ {
var suggs []*Suggestion
b.StartTimer()
_, err := sess.SelectBySql(query).LoadContext(context.Background(), &suggs)
b.StopTimer()
require.NoError(b, err)
require.Len(b, suggs, n)
}
})
}
}