-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark_test.go
118 lines (96 loc) · 2.34 KB
/
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
package prep
import (
"database/sql"
"testing"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
/*
$ go test -bench=.
BenchmarkPostgresWithoutPreparedStatements-4 20000 59941 ns/op 1183 B/op 32 allocs/op
BenchmarkPostgresWithPreparedStatements-4 50000 41560 ns/op 1021 B/op 26 allocs/op
BenchmarkMySQLWithoutPreparedStatements-4 50000 26454 ns/op 827 B/op 23 allocs/op
BenchmarkMySQLWithPreparedStatements-4 200000 9509 ns/op 634 B/op 19 allocs/op
PASS
ok github.com/hexdigest/prep 7.884s
*/
func BenchmarkPostgresWithoutPreparedStatements(b *testing.B) {
b.ReportAllocs()
db, err := sql.Open("postgres", "postgres://pg:pg@127.0.0.1:5432/pg?sslmode=disable")
if err != nil {
b.Fatal(err)
}
const query = "SELECT $1::text"
var s string
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := db.QueryRow(query, "1").Scan(&s); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkPostgresWithPreparedStatements(b *testing.B) {
b.ReportAllocs()
var (
db Connector
err error
)
db, err = sql.Open("postgres", "postgres://pg:pg@127.0.0.1:5432/pg?sslmode=disable")
if err != nil {
b.Fatal(err)
}
const query = "SELECT $1::text"
var s string
db, err = NewConnection(db, []string{query})
if err != nil {
b.Fatal(err)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := db.QueryRow(query, "1").Scan(&s); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkMySQLWithoutPreparedStatements(b *testing.B) {
b.ReportAllocs()
db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/mysql")
if err != nil {
b.Fatal(err)
}
const query = "SELECT ?"
var s string
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := db.QueryRow(query, "1").Scan(&s); err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkMySQLWithPreparedStatements(b *testing.B) {
b.ReportAllocs()
var (
db Connector
err error
)
db, err = sql.Open("mysql", "root:root@tcp(localhost:3306)/mysql")
if err != nil {
b.Fatal(err)
}
const query = "SELECT ?"
var s string
db, err = NewConnection(db, []string{query})
if err != nil {
b.Fatal(err)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := db.QueryRow(query, "1").Scan(&s); err != nil {
b.Fatal(err)
}
}
})
}