-
Notifications
You must be signed in to change notification settings - Fork 122
/
benchmark_test.go
229 lines (186 loc) · 5.13 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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
//go:build all || integration
// +build all integration
package gocqlx_test
import (
"encoding/json"
"os"
"testing"
"github.com/scylladb/gocqlx/v3"
"github.com/scylladb/gocqlx/v3/gocqlxtest"
"github.com/scylladb/gocqlx/v3/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"}
//
// Insert
//
// BenchmarkBaseGocqlInsert performs standard insert.
func BenchmarkBaseGocqlInsert(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.CreateSession(b)
defer session.Close()
if err := session.ExecStmt(benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, _ := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := session.Session.Query(stmt)
defer q.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
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)
}
}
}
// BenchmarkGocqlInsert performs insert with struct binding.
func BenchmarkGocqlxInsert(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.CreateSession(b)
defer session.Close()
if err := session.ExecStmt(benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, names := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := 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
//
// BenchmarkBaseGocqlGet performs standard scan.
func BenchmarkBaseGocqlGet(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.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()
q := session.Session.Query(stmt)
defer q.Release()
var p benchPerson
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Bind(people[i%len(people)].ID)
if err := q.Scan(&p.ID, &p.FirstName, &p.LastName, &p.Email, &p.Gender, &p.IPAddress); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkGocqlxGet performs get.
func BenchmarkGocqlxGet(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.CreateSession(b)
defer session.Close()
initTable(b, session, people)
stmt, names := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Where(qb.Eq("id")).Limit(1).ToCql()
q := session.Query(stmt, names)
defer q.Release()
var p benchPerson
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Bind(people[i%len(people)].ID)
if err := q.Get(&p); err != nil {
b.Fatal(err)
}
}
}
//
// Select
//
// BenchmarkBaseGocqlSelect performs standard loop scan with a slice of
// pointers.
func BenchmarkBaseGocqlSelect(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.CreateSession(b)
defer session.Close()
initTable(b, session, people)
stmt, _ := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Limit(100).ToCql()
q := session.Session.Query(stmt)
defer q.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
iter := q.Iter()
v := make([]*benchPerson, 100)
p := new(benchPerson)
for iter.Scan(&p.ID, &p.FirstName, &p.LastName, &p.Email, &p.Gender, &p.IPAddress) {
v = append(v, p) // nolint:staticcheck
p = new(benchPerson)
}
if err := iter.Close(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkGocqlSelect performs select to a slice pointers.
func BenchmarkGocqlxSelect(b *testing.B) {
people := loadFixtures()
session := gocqlxtest.CreateSession(b)
defer session.Close()
initTable(b, session, people)
stmt, names := qb.Select("gocqlx_test.bench_person").Columns(benchPersonCols...).Limit(100).ToCql()
q := session.Query(stmt, names)
defer q.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var v []*benchPerson
if err := q.Select(&v); err != nil {
b.Fatal(err)
}
}
}
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
}
func initTable(b *testing.B, session gocqlx.Session, people []*benchPerson) {
b.Helper()
if err := session.ExecStmt(benchPersonSchema); err != nil {
b.Fatal(err)
}
stmt, names := qb.Insert("gocqlx_test.bench_person").Columns(benchPersonCols...).ToCql()
q := session.Query(stmt, names)
for _, p := range people {
if err := q.BindStruct(p).Exec(); err != nil {
b.Fatal(err)
}
}
}