-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench_test.go
133 lines (114 loc) · 2.53 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
package worksheets
import (
"database/sql"
"math/rand"
"strings"
"testing"
runner "github.com/homelight/dat/sqlx-runner"
_ "github.com/lib/pq"
)
type bencher struct {
*testing.B
db *runner.DB
defs *Definitions
store *DbStore
}
func start(b *testing.B) *bencher {
// db
dbUrl := "postgres://ws_user:@localhost/ws_test?sslmode=disable"
sqlDb, err := sql.Open("postgres", dbUrl)
if err != nil {
panic(err)
}
db := runner.NewDB(sqlDb, "postgres")
// defs
defs, err := NewDefinitions(strings.NewReader(`
type parent worksheet {
1:f1 text
2:f2 text
3:f3 text
4:f4 text
5:f5 text
6:f6 []child
}
type child worksheet {
1:f1 number[1]
2:f2 number[2]
3:f3 number[3]
4:f4 number[4]
5:f5 number[5]
}
`))
if err != nil {
panic(err)
}
return &bencher{
B: b,
db: db,
defs: defs,
store: NewStore(defs),
}
}
func (b *bencher) prime(count int) {
for i := 0; i < count; i++ {
if err := RunTransaction(b.db, func(tx *runner.Tx) error {
parent := b.parentWs(rand.Intn(5))
session := b.store.Open(tx)
_, err := session.Save(parent)
return err
}); err != nil {
panic(err)
}
}
}
func (b *bencher) childWs() *Worksheet {
child := b.defs.MustNewWorksheet("child")
child.MustSet("f1", NewNumberFromInt(1697))
child.MustSet("f2", NewNumberFromInt(1759))
child.MustSet("f3", NewNumberFromInt(2153))
child.MustSet("f4", NewNumberFromInt(2161))
child.MustSet("f5", NewNumberFromInt(4229))
return child
}
func (b *bencher) parentWs(numChildren int) *Worksheet {
parent := b.defs.MustNewWorksheet("parent")
parent.MustSet("f1", NewText("Lorem ipsum dolor sit amet"))
parent.MustSet("f2", NewText("consectetur adipiscing elit."))
parent.MustSet("f3", NewText("Proin nisi ex,"))
parent.MustSet("f4", NewText("fringilla nec imperdiet sit amet,"))
parent.MustSet("f5", NewText("vehicula at ipsum."))
for 0 < numChildren {
child := b.childWs()
parent.MustAppend("f6", child)
numChildren--
}
return parent
}
func BenchmarkLoad(_b *testing.B) {
b := start(_b)
// b.prime(100000)
// choose a random parent ws to load
var parentId string
if err := b.db.SQL(`
select id
from worksheets
where name = 'parent'
order by random()
limit 1
`).QueryScalar(&parentId); err != nil {
panic(err)
}
// reset, and get ready to do the benchmark
b.ResetTimer()
// lots of loads
for i := 0; i < b.N; i++ {
err := RunTransaction(b.db, func(tx *runner.Tx) error {
session := b.store.Open(tx)
_, err := session.Load(parentId)
return err
})
if err != nil {
panic(err)
}
}
}