forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
115 lines (104 loc) · 2.32 KB
/
benchmarks_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
package pop_test
import (
"fmt"
"strconv"
"testing"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/pop/nulls"
)
func Benchmark_Create_Pop(b *testing.B) {
transaction(func(tx *pop.Connection) {
for n := 0; n < b.N; n++ {
u := &User{
Name: nulls.NewString("Mark Bates"),
}
tx.Create(u)
}
})
}
func Benchmark_Create_Raw(b *testing.B) {
transaction(func(tx *pop.Connection) {
for n := 0; n < b.N; n++ {
u := &User{
Name: nulls.NewString("Mark Bates"),
}
q := "INSERT INTO users (alive, bio, birth_date, created_at, name, price, updated_at) VALUES (:alive, :bio, :birth_date, :created_at, :name, :price, :updated_at)"
tx.Store.NamedExec(q, u)
}
})
}
func Benchmark_Update(b *testing.B) {
transaction(func(tx *pop.Connection) {
u := &User{
Name: nulls.NewString("Mark Bates"),
}
tx.Create(u)
for n := 0; n < b.N; n++ {
tx.Update(u)
}
})
}
func Benchmark_Find_Pop(b *testing.B) {
transaction(func(tx *pop.Connection) {
u := &User{
Name: nulls.NewString("Mark Bates"),
}
tx.Create(u)
for n := 0; n < b.N; n++ {
tx.Find(u, u.ID)
}
})
}
func Benchmark_Find_Raw(b *testing.B) {
transaction(func(tx *pop.Connection) {
u := &User{
Name: nulls.NewString("Mark Bates"),
}
tx.Create(u)
for n := 0; n < b.N; n++ {
tx.Store.Get(u, "select * from users where id = ?", u.ID)
}
})
}
func Benchmark_translateOne(b *testing.B) {
q := "select * from users where id = ? and name = ? and email = ? and a = ? and b = ? and c = ? and d = ? and e = ? and f = ?"
for n := 0; n < b.N; n++ {
translateOne(q)
}
}
func Benchmark_translateTwo(b *testing.B) {
q := "select * from users where id = ? and name = ? and email = ? and a = ? and b = ? and c = ? and d = ? and e = ? and f = ?"
for n := 0; n < b.N; n++ {
translateTwo(q)
}
}
func translateOne(sql string) string {
curr := 1
out := make([]byte, 0, len(sql))
for i := 0; i < len(sql); i++ {
if sql[i] == '?' {
str := "$" + strconv.Itoa(curr)
for _, char := range str {
out = append(out, byte(char))
}
curr += 1
} else {
out = append(out, sql[i])
}
}
return string(out)
}
func translateTwo(sql string) string {
curr := 1
csql := ""
for i := 0; i < len(sql); i++ {
x := sql[i]
if x == '?' {
csql = fmt.Sprintf("%s$%d", csql, curr)
curr++
} else {
csql += string(x)
}
}
return csql
}