forked from jonbodner/proteus
/
speed.go
161 lines (146 loc) · 3.76 KB
/
speed.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
package main
import (
"context"
"database/sql"
"fmt"
"math"
"time"
"github.com/jonbodner/proteus"
"github.com/jonbodner/stackerr"
_ "github.com/lib/pq"
"github.com/pkg/profile"
log "github.com/sirupsen/logrus"
)
func SelectProteus(ctx context.Context, db *sql.DB) time.Duration {
var productDao BenchProductDao
err := proteus.Build(&productDao, proteus.Postgres)
if err != nil {
panic(err)
}
start := time.Now()
for i := 0; i < 1000; i++ {
for j := 0; j < 10; j++ {
p, err := productDao.FindById(ctx, db, j)
if err != nil {
panic(err)
}
err = validate(j, p)
if err != nil {
fmt.Println(err)
}
}
}
end := time.Now()
return end.Sub(start)
}
func validate(i int, p BenchProduct) error {
if p.Id != i {
return stackerr.Errorf("should of had id %d, had %d instead", i, p.Id)
}
if p.Name != fmt.Sprintf("person%d", i) {
return stackerr.Errorf("should of had person4, had %s instead", p.Name)
}
if i%2 == 0 {
if p.Cost == nil {
return stackerr.Errorf("cost should have been non-nil")
} else {
if math.Abs(*p.Cost-1.1*float64(i)) > 0.01 {
return stackerr.Errorf("should have had %f, had %f instead", 1.1*float64(i), *p.Cost)
}
}
} else {
if p.Cost != nil {
return stackerr.Errorf("should have been nil, was %f", *p.Cost)
}
}
return nil
}
func SelectNative(ctx context.Context, db *sql.DB) time.Duration {
start := time.Now()
for i := 0; i < 1000; i++ {
var id int
var name string
var cost *float64
for j := 0; j < 10; j++ {
rows, err := db.QueryContext(ctx, "select id, name, cost from Product where id = $1", j)
if err != nil {
panic(err)
}
if rows.Next() {
err = rows.Scan(&id, &name, &cost)
if err != nil {
panic(err)
}
p := BenchProduct{Id: id, Name: name, Cost: cost}
err = validate(j, p)
if err != nil {
fmt.Println(err)
}
}
rows.Close()
}
}
end := time.Now()
return end.Sub(start)
}
type BenchProduct struct {
Id int `prof:"id"`
Name string `prof:"name"`
Cost *float64 `prof:"cost"`
}
type BenchProductDao struct {
FindById func(ctx context.Context, e proteus.ContextQuerier, id int) (BenchProduct, error) `proq:"select id, name, cost from Product where id = :id:" prop:"id"`
Insert func(ctx context.Context, e proteus.ContextExecutor, id int, name string, cost *float64) (int64, error) `proq:"insert into product(id, name, cost) values(:id:, :name:, :cost:)" prop:"id,name,cost"`
}
func main() {
ctx := context.Background()
db := setupDbPostgres(ctx)
defer profile.Start().Stop()
defer db.Close()
for i := 0; i < 5; i++ {
fmt.Printf("Round %d - Proteus first\n", i+1)
fmt.Println("Proteus: ", SelectProteus(ctx, db))
fmt.Println("Native: ", SelectNative(ctx, db))
fmt.Printf("Round %d - Native first\n", i+1)
fmt.Println("Native: ", SelectNative(ctx, db))
fmt.Println("Proteus: ", SelectProteus(ctx, db))
}
}
func setupDbPostgres(ctx context.Context) *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 product;
create table product (id integer not null primary key, name text, cost real);
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Fatalf("%q: %s\n", err, sqlStmt)
return nil
}
populate(ctx, db)
return db
}
func populate(ctx context.Context, db *sql.DB) {
productDao := BenchProductDao{}
proteus.Build(&productDao, proteus.Postgres)
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Commit()
for i := 0; i < 100; i++ {
var cost *float64
if i%2 == 0 {
c := 1.1 * float64(i)
cost = &c
}
rowCount, err := productDao.Insert(ctx, tx, i, fmt.Sprintf("person%d", i), cost)
if err != nil {
log.Fatal(err)
}
log.Debug(rowCount)
}
}