forked from scylladb/gocqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
235 lines (194 loc) · 5.06 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
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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
// +build all integration
package gocqlx_test
import (
"encoding/json"
"os"
"testing"
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx"
"github.com/scylladb/gocqlx/qb"
)
type benchPerson struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email []string `json:"email"`
Gender string `json:"gender"`
IPAddress string `json:"ip_address"`
}
var benchPersonSchema = `
CREATE TABLE IF NOT EXISTS gocqlx_test.bench_person (
id int,
first_name text,
last_name text,
email list<text>,
gender text,
ip_address text,
PRIMARY KEY(id)
)`
var benchPersonCols = []string{"id", "first_name", "last_name", "email", "gender", "ip_address"}
func loadFixtures() []*benchPerson {
f, err := os.Open("testdata/people.json")
if err != nil {
panic(err)
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
var v []*benchPerson
if err := json.NewDecoder(f).Decode(&v); err != nil {
panic(err)
}
return v
}
//
// Insert
//
// BenchmarkE2EGocqlInsert performs standard insert.
func BenchmarkE2EGocqlInsert(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
if err := createTable(session, benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, _ := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := session.Query(stmt)
defer q.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// prepare
p := people[i%len(people)]
if err := q.Bind(p.ID, p.FirstName, p.LastName, p.Email, p.Gender, p.IPAddress).Exec(); err != nil {
b.Fatal(err)
}
// insert
if err := q.Exec(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkE2EGocqlInsert performs insert with struct binding.
func BenchmarkE2EGocqlxInsert(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
if err := createTable(session, benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, names := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := gocqlx.Query(session.Query(stmt), names)
defer q.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := people[i%len(people)]
if err := q.BindStruct(p).Exec(); err != nil {
b.Fatal(err)
}
}
}
//
// Get
//
// BenchmarkE2EGocqlGet performs standard scan.
func BenchmarkE2EGocqlGet(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
initTable(b, session, people)
stmt, _ := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Where(qb.Eq("id")).Limit(1).ToCql()
var p benchPerson
b.ResetTimer()
for i := 0; i < b.N; i++ {
// prepare
q := session.Query(stmt)
q.Bind(people[i%len(people)].ID)
// scan
if err := q.Scan(&p.ID, &p.FirstName, &p.LastName, &p.Email, &p.Gender, &p.IPAddress); err != nil {
b.Fatal(err)
}
// release
q.Release()
}
}
// BenchmarkE2EGocqlxGet performs get.
func BenchmarkE2EGocqlxGet(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
initTable(b, session, people)
stmt, _ := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Where(qb.Eq("id")).Limit(1).ToCql()
var p benchPerson
b.ResetTimer()
for i := 0; i < b.N; i++ {
// prepare
q := session.Query(stmt)
q.Bind(people[i%len(people)].ID)
// get
gocqlx.Get(&p, q)
}
}
//
// Select
//
// BenchmarkE2EGocqlSelect performs standard loop scan.
func BenchmarkE2EGocqlSelect(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
initTable(b, session, people)
stmt, _ := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Limit(100).ToCql()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// prepare
v := make([]*benchPerson, 100)
q := session.Query(stmt)
i := q.Iter()
// loop scan
p := new(benchPerson)
for i.Scan(&p.ID, &p.FirstName, &p.LastName, &p.Email, &p.Gender, &p.IPAddress) {
v = append(v, p)
p = new(benchPerson)
}
if err := i.Close(); err != nil {
b.Fatal(err)
}
// release
q.Release()
}
}
// BenchmarkE2EGocqlSelect performs select.
func BenchmarkE2EGocqlxSelect(b *testing.B) {
people := loadFixtures()
session := createSession(b)
defer session.Close()
initTable(b, session, people)
stmt, _ := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Limit(100).ToCql()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// prepare
q := session.Query(stmt)
var v []*benchPerson
// select
if err := gocqlx.Select(&v, q); err != nil {
b.Fatal(err)
}
}
}
func initTable(b *testing.B, session *gocql.Session, people []*benchPerson) {
if err := createTable(session, benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, names := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := gocqlx.Query(session.Query(stmt), names)
for _, p := range people {
if err := q.BindStruct(p).Exec(); err != nil {
b.Fatal(err)
}
}
}