forked from jonbodner/proteus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
324 lines (276 loc) · 7.97 KB
/
bench_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
package proteus
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"testing"
_ "github.com/lib/pq"
)
func BenchmarkProteus(b *testing.B) {
b.StopTimer()
err := Build(&personDao, Postgres)
if err != nil {
panic(err)
}
ctx := context.Background()
b.StartTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
db := setupDbPostgres()
b.StartTimer()
doPersonStuffForProteusTest(ctx, b, db)
b.StopTimer()
db.Close()
}
}
type Person struct {
Id int `prof:"id"`
Name string `prof:"name"`
Age int `prof:"age"`
}
func (p Person) String() string {
return fmt.Sprintf("Id: %d\tName:%s\tAge:%d", p.Id, p.Name, p.Age)
}
type PersonDao struct {
Create func(ctx context.Context, e ContextExecutor, name string, age int) (int64, error) `proq:"INSERT INTO PERSON(name, age) VALUES(:name:, :age:)" prop:"name,age"`
Get func(ctx context.Context, q ContextQuerier, id int) (*Person, error) `proq:"SELECT * FROM PERSON WHERE id = :id:" prop:"id"`
Update func(ctx context.Context, e ContextExecutor, id int, name string, age int) (int64, error) `proq:"UPDATE PERSON SET name = :name:, age=:age: where id=:id:" prop:"id,name,age"`
Delete func(ctx context.Context, e ContextExecutor, id int) (int64, error) `proq:"DELETE FROM PERSON WHERE id = :id:" prop:"id"`
GetAll func(ctx context.Context, q ContextQuerier) ([]Person, error) `proq:"SELECT * FROM PERSON"`
GetByAge func(ctx context.Context, q ContextQuerier, id int, ages []int, name string) ([]Person, error) `proq:"SELECT * from PERSON WHERE name=:name: and age in (:ages:) and id = :id:" prop:"id,ages,name"`
}
var personDao PersonDao
func setupDbPostgres() *sql.DB {
db, err := sql.Open("postgres", "postgres://pro_user:pro_pwd@localhost/proteus?sslmode=disable")
if err != nil {
log.Fatal(err)
}
sqlStmt := `
drop table if exists person;
create table person (id serial primary key, name text, age int);
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Fatalf("%q: %s\n", err, sqlStmt)
return nil
}
return db
}
func doPersonStuffForProteusTest(ctx context.Context, b *testing.B, wrapper ContextWrapper) (int64, *Person, []Person, error) {
_, err := personDao.Create(ctx, wrapper, "Fred", 20)
if err != nil {
b.Fatalf("create failed: %v", err)
}
_, err = personDao.Create(ctx, wrapper, "Bob", 50)
if err != nil {
b.Fatalf("create 2 failed: %v", err)
}
_, err = personDao.Create(ctx, wrapper, "Julia", 32)
if err != nil {
b.Fatalf("create 3 failed: %v", err)
}
_, err = personDao.Create(ctx, wrapper, "Pat", 37)
if err != nil {
b.Fatalf("create 4 failed: %v", err)
}
_, err = personDao.Get(ctx, wrapper, 1)
if err != nil {
b.Fatalf("get failed: %v", err)
}
_, err = personDao.GetAll(ctx, wrapper)
if err != nil {
b.Fatalf("get all failed: %v", err)
}
people, err := personDao.GetByAge(ctx, wrapper, 1, []int{20, 32}, "Fred")
if err != nil {
b.Fatalf("get by age failed: %v", err)
}
_, err = personDao.Update(ctx, wrapper, 1, "Freddie", 30)
if err != nil {
b.Fatalf("update failed: %v", err)
}
_, err = personDao.Get(ctx, wrapper, 1)
if err != nil {
b.Fatalf("get 2 failed: %v", err)
}
_, err = personDao.Delete(ctx, wrapper, 1)
if err != nil {
b.Fatalf("delete failed: %v", err)
}
count, err := personDao.Delete(ctx, wrapper, 1)
if err != nil {
b.Fatalf("delete 2 failed: %v", err)
}
person, err := personDao.Get(ctx, wrapper, 1)
if err != nil {
b.Fatalf("get 3 failed: %v", err)
}
return count, person, people, err
}
func BenchmarkStandard(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
db := setupDbPostgres()
b.StartTimer()
doPersonStuffForStandardTest(b, db)
b.StopTimer()
db.Close()
}
}
type standardPersonDao struct{}
// `proq:"INSERT INTO PERSON(name, age) VALUES(:name:, :age:)" prop:"name,age"`
func (spd standardPersonDao) Create(db *sql.DB, name string, age int) (int64, error) {
result, err := db.Exec("INSERT INTO PERSON(name, age) VALUES($1, $2)", name, age)
if err != nil {
return 0, err
}
count, err := result.RowsAffected()
return count, err
}
// `proq:"SELECT * FROM PERSON WHERE id = :id:" prop:"id"`
func (spd standardPersonDao) Get(db *sql.DB, id int) (*Person, error) {
rows, err := db.Query("SELECT age, name, id FROM PERSON WHERE id = $1", id)
if err != nil {
return nil, err
}
var p *Person
if rows.Next() {
err = rows.Err()
if err == nil {
p = &Person{}
err = rows.Scan(&p.Age, &p.Name, &p.Id)
}
}
rows.Close()
return p, err
}
// `proq:"UPDATE PERSON SET name = :name:, age=:age: where id=:id:" prop:"id,name,age"`
func (spd standardPersonDao) Update(db *sql.DB, id int, name string, age int) (int64, error) {
result, err := db.Exec("UPDATE PERSON SET name = $1, age=$2 where id=$3", name, age, id)
if err != nil {
return 0, err
}
count, err := result.RowsAffected()
return count, err
}
// `proq:"DELETE FROM PERSON WHERE id = :id:" prop:"id"`
func (spd standardPersonDao) Delete(db *sql.DB, id int) (int64, error) {
result, err := db.Exec("DELETE FROM PERSON WHERE id = $1", id)
if err != nil {
return 0, err
}
count, err := result.RowsAffected()
return count, err
}
// `proq:"SELECT * FROM PERSON"`
func (spd standardPersonDao) GetAll(db *sql.DB) ([]Person, error) {
rows, err := db.Query("SELECT age, name, id FROM PERSON")
if err != nil {
return nil, err
}
var out []Person
for rows.Next() {
err = rows.Err()
if err != nil {
break
}
p := Person{}
err = rows.Scan(&p.Age, &p.Name, &p.Id)
if err != nil {
break
}
out = append(out, p)
}
rows.Close()
return out, err
}
// `proq:"SELECT * from PERSON WHERE name=:name: and age in (:ages:) and id = :id:" prop:"id,ages,name"`
func (spd standardPersonDao) GetByAge(db *sql.DB, id int, ages []int, name string) ([]Person, error) {
startQuery := "SELECT age, name, id from PERSON WHERE name=$1 and age in (:ages:) and id = :id:"
params := make([]string, len(ages))
for i := 0; i < len(ages); i++ {
params[i] = fmt.Sprintf("$%d", i+2)
}
inClause := strings.Join(params, ",")
finalQuery := strings.Replace(startQuery, ":ages:", inClause, -1)
finalQuery = strings.Replace(finalQuery, ":id:", fmt.Sprintf("$%d", len(ages)+2), -1)
args := make([]interface{}, 0, len(ages)+2)
args = append(args, name)
for _, v := range ages {
args = append(args, v)
}
args = append(args, id)
rows, err := db.Query(finalQuery, args...)
if err != nil {
return nil, err
}
var out []Person
for rows.Next() {
err = rows.Err()
if err != nil {
break
}
p := Person{}
err = rows.Scan(&p.Age, &p.Name, &p.Id)
if err != nil {
break
}
out = append(out, p)
}
rows.Close()
return out, err
}
var sPersonDao = standardPersonDao{}
func doPersonStuffForStandardTest(b *testing.B, db *sql.DB) (int64, *Person, []Person, error) {
_, err := sPersonDao.Create(db, "Fred", 20)
if err != nil {
b.Fatalf("create failed: %v", err)
}
_, err = sPersonDao.Create(db, "Bob", 50)
if err != nil {
b.Fatalf("create 2 failed: %v", err)
}
_, err = sPersonDao.Create(db, "Julia", 32)
if err != nil {
b.Fatalf("create 3 failed: %v", err)
}
_, err = sPersonDao.Create(db, "Pat", 37)
if err != nil {
b.Fatalf("create 4 failed: %v", err)
}
_, err = sPersonDao.Get(db, 1)
if err != nil {
b.Fatalf("get failed: %v", err)
}
_, err = sPersonDao.GetAll(db)
if err != nil {
b.Fatalf("get all failed: %v", err)
}
people, err := sPersonDao.GetByAge(db, 1, []int{20, 32}, "Fred")
if err != nil {
b.Fatalf("get by age failed: %v", err)
}
_, err = sPersonDao.Update(db, 1, "Freddie", 30)
if err != nil {
b.Fatalf("update failed: %v", err)
}
_, err = sPersonDao.Get(db, 1)
if err != nil {
b.Fatalf("get 2 failed: %v", err)
}
_, err = sPersonDao.Delete(db, 1)
if err != nil {
b.Fatalf("delete failed: %v", err)
}
count, err := sPersonDao.Delete(db, 1)
if err != nil {
b.Fatalf("delete 2 failed: %v", err)
}
person, err := sPersonDao.Get(db, 1)
if err != nil {
b.Fatalf("get 3 failed: %v", err)
}
return count, person, people, err
}