-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgperf_test.go
284 lines (232 loc) · 5.37 KB
/
pgperf_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
package pgperf_test
import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"testing"
"pgperf"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/shopspring/decimal"
)
const batchSize = 1000
var (
pool *pgxpool.Pool
ctx context.Context
cancel context.CancelFunc
)
func runTests(m *testing.M) int {
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
var err error
pool, err = pgxpool.New(ctx, "postgres://postgres:postgres@localhost/postgres?sslmode=disable")
if err != nil {
panic(err)
}
defer pool.Close()
return m.Run()
}
func TestMain(m *testing.M) {
os.Exit(runTests(m))
}
func getConn(ctx context.Context) (*pgxpool.Conn, error) {
conn, err := pool.Acquire(ctx)
if err != nil {
return nil, err
}
return conn, nil
}
func getTx(ctx context.Context) (pgx.Tx, func(), error) {
conn, err := getConn(ctx)
if err != nil {
return nil, nil, err
}
tx, err := conn.Begin(ctx)
if err != nil {
return nil, conn.Release, err
}
return tx, conn.Release, nil
}
func runGetUsers(b *testing.B, variant int) {
tx, close, err := getTx(ctx)
if close != nil {
defer close()
}
if err != nil {
b.Fatalf("failed to start transaction : %v", err)
}
defer tx.Rollback(ctx)
var f func(context.Context, pgx.Tx, []int) ([]string, error)
switch variant {
case 1:
f = pgperf.GetUsers1
case 2:
f = pgperf.GetUsers2
case 3:
f = pgperf.GetUsers3
case 4:
f = pgperf.GetUsers4
default:
b.Fatalf("unknown GetUsers variant %d", variant)
}
ids := make([]int, batchSize)
for i := 0; i < b.N; i++ {
for j := 0; j < len(ids); j++ {
ids[j] = rand.Intn(1000000)
}
if _, err := f(ctx, tx, ids); err != nil {
b.Fatalf("failed to call GetUsers: %v", err)
}
}
}
func BenchmarkGetUsers1(b *testing.B) {
runGetUsers(b, 1)
}
func BenchmarkGetUsers2(b *testing.B) {
runGetUsers(b, 2)
}
func BenchmarkGetUsers3(b *testing.B) {
runGetUsers(b, 3)
}
func BenchmarkGetUsers4(b *testing.B) {
runGetUsers(b, 4)
}
func runInsertUsers(b *testing.B, variant int) {
conn, err := getConn(ctx)
if err != nil {
b.Fatalf("failed to aqcuire connection: %v", err)
}
defer conn.Release()
var f func(context.Context, pgx.Tx, []int) error
switch variant {
case 1:
f = pgperf.InsertUsers1
case 2:
f = pgperf.InsertUsers2
case 3:
f = pgperf.InsertUsers3
case 4:
f = pgperf.InsertUsers4
case 5:
f = pgperf.InsertUsers5
case 6:
f = pgperf.InsertUsers6
default:
b.Fatalf("unknown InsertUsers variant %d", variant)
}
ids := make([]int, batchSize)
for i := 0; i < b.N; i++ {
for j := 0; j < len(ids); j++ {
ids[j] = 1000001 + j
}
tx, err := conn.Begin(ctx)
if err != nil {
b.Fatalf("failed to start transaction: %v", err)
}
if err := f(ctx, tx, ids); err != nil {
tx.Rollback(ctx)
b.Fatalf("failed to call InsertUsers: %v", err)
}
tx.Rollback(ctx)
}
}
func BenchmarkInsertUsers1(b *testing.B) {
runInsertUsers(b, 1)
}
func BenchmarkInsertUsers2(b *testing.B) {
runInsertUsers(b, 2)
}
func BenchmarkInsertUsers3(b *testing.B) {
runInsertUsers(b, 3)
}
func BenchmarkInsertUsers4(b *testing.B) {
runInsertUsers(b, 4)
}
func BenchmarkInsertUsers5(b *testing.B) {
runInsertUsers(b, 5)
}
func BenchmarkInsertUsers6(b *testing.B) {
runInsertUsers(b, 6)
}
func doTrx(ctx context.Context, conn *pgxpool.Conn, from, to, amount int) {
amt := decimal.NewFromInt(int64(amount))
tx, err := conn.Begin(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
panic(err)
}
defer tx.Rollback(ctx)
// ctx, cancel := context.WithTimeout(ctx, time.Second)
// defer cancel()
if err := pgperf.TransferLock(ctx, tx, from, to, amt); err != nil {
return
}
tx.Commit(ctx)
}
const (
concurrency = 2
cardinality = 10000
)
func BenchmarkTransferLock(b *testing.B) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
conn, err := getConn(ctx)
if err != nil {
b.Fatalf("failed to acquire connection: %v", err)
}
defer conn.Release()
var totalIDRTbefore decimal.Decimal
if err := conn.QueryRow(ctx, "select sum(amount) from test.accounts where currency = 'IDRT'").Scan(&totalIDRTbefore); err != nil {
b.Fatalf("failed to get total IDRT: %v", err)
}
var ids []int
q := `select array_agg(id)
from test.accounts
where currency = 'IDRT'
and amount > 10000000`
if err := conn.QueryRow(ctx, q).Scan(&ids); err != nil {
b.Fatalf("failed to get IDRT accounts: %v", err)
}
ids = ids[:cardinality]
for i := 0; i < concurrency; i++ {
go func() {
conn, err := getConn(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
panic(fmt.Errorf("failed to acquire connection: %v", err))
}
defer conn.Release()
for {
select {
case <-ctx.Done():
return
default:
}
from := ids[rand.Intn(len(ids))]
to := ids[rand.Intn(len(ids))]
amt := rand.Intn(10)
doTrx(ctx, conn, from, to, amt)
}
}()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
from := ids[rand.Intn(len(ids))]
to := ids[rand.Intn(len(ids))]
amt := rand.Intn(10)
doTrx(ctx, conn, from, to, amt)
}
var totalIDRTafter decimal.Decimal
if err := conn.QueryRow(ctx, "select sum(amount) from test.accounts where currency = 'IDRT'").Scan(&totalIDRTafter); err != nil {
b.Fatalf("failed to get total IDRT: %v", err)
}
if !totalIDRTbefore.Equal(totalIDRTafter) {
b.Fatalf("total IDRT amount changed (before/after) %v/%v", totalIDRTbefore, totalIDRTafter)
}
}