-
Notifications
You must be signed in to change notification settings - Fork 21
/
benchmark_test.go
243 lines (205 loc) · 5.32 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
236
237
238
239
240
241
242
243
package tasqueue
import (
"context"
"log"
"log/slog"
"sync"
"testing"
"github.com/go-redis/redis"
rb "github.com/kalbhor/tasqueue/v2/brokers/redis"
rr "github.com/kalbhor/tasqueue/v2/results/redis"
)
// The benchmarks use redis as results & broker.
const (
redisAddr = "127.0.0.1:6379"
redisPass = ""
redisDB = 0
sampleHandler = "sample-handler"
)
// newJob returns a job configured for the sample handler with an empty byte payload.
func newJob(b *testing.B) Job {
job, err := NewJob(sampleHandler, []byte{}, JobOpts{})
if err != nil {
b.Fatal(err)
}
return job
}
// serverWithRedis returns a tasqueue server with redis as broker and results.
func serverWithRedis(b *testing.B) *Server {
lo := slog.Default()
srv, err := NewServer(ServerOpts{
Broker: rb.New(rb.Options{
Addrs: []string{redisAddr},
Password: redisPass,
DB: redisDB,
}, lo),
Results: rr.New(rr.Options{
Addrs: []string{redisAddr},
Password: redisPass,
DB: redisDB,
}, lo),
Logger: lo.Handler(),
})
if err != nil {
b.Fatal(err)
}
return srv
}
func flushRedis() {
conn := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{redisAddr},
DB: redisDB,
Password: redisPass,
})
conn.FlushDB()
conn.Close()
}
// BenchmarkJob benchmarks processing 100000 jobs.
// 10 workers are configured to consume the jobs concurrently. The benchmark starts AFTER
// enqueing all the jobs on the server.
func BenchmarkJob(b *testing.B) {
const (
num = 100000
concurrency = 10
)
flushRedis()
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
ctx, cancel := context.WithCancel(context.Background())
// Create the server
srv := serverWithRedis(b)
// Waitgroup to indicate when jobs finish
var wg sync.WaitGroup
wg.Add(num)
// handler is essentially an empty job handler.
handler := func(b []byte, ctx JobCtx) error {
wg.Done()
return nil
}
// Register the handler and enqueue the jobs.
err := srv.RegisterTask(sampleHandler, handler, TaskOpts{Concurrency: concurrency})
if err != nil {
b.Fatal(err)
}
for i := 0; i < num; i++ {
if _, err := srv.Enqueue(ctx, newJob(b)); err != nil {
b.Fatalf("could not enqueue job : %v", err)
}
}
b.StartTimer()
go srv.Start(ctx)
// Wait for jobs to complete & stop the timer.
wg.Wait()
b.StopTimer()
// Cancel the context so that the server can shutdown.
cancel()
b.StartTimer()
}
}
// BenchmarkGroup benchmarks processing 10000 chains with 10 jobs each.
// 10 workers are configured to consume the groups concurrently. The benchmark starts AFTER
// enqueing all the chains on the server.
func BenchmarkChain(b *testing.B) {
const (
num = 10000
jobNum = 10
concurrency = 10
)
flushRedis()
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
ctx, cancel := context.WithCancel(context.Background())
// Create the server
srv := serverWithRedis(b)
// Waitgroup to indicate when jobs finish
var wg sync.WaitGroup
wg.Add(num * jobNum)
// handler is essentially an empty job handler.
handler := func(x []byte, ctx JobCtx) error {
wg.Done()
return nil
}
// Register the handler and enqueue the jobs.
err := srv.RegisterTask(sampleHandler, handler, TaskOpts{Concurrency: concurrency})
if err != nil {
b.Fatal(err)
}
for i := 0; i < num; i++ {
// Create a list of jobs to generate a chain.
var jobs = make([]Job, jobNum)
for j := 0; j < jobNum; j++ {
jobs[j] = newJob(b)
}
chain, err := NewChain(jobs, ChainOpts{})
if err != nil {
log.Fatal(err)
}
if _, err := srv.EnqueueChain(ctx, chain); err != nil {
b.Fatalf("could not enqueue chain : %v", err)
}
}
b.StartTimer()
go srv.Start(ctx)
// Wait for jobs to complete & stop the timer.
wg.Wait()
b.StopTimer()
// Cancel the context so that the server can shutdown.
cancel()
b.StartTimer()
}
}
// BenchmarkGroup benchmarks processing 10000 groups with 10 jobs each.
// 10 workers are configured to consume the groups concurrently. The benchmark starts AFTER
// enqueing all the groups on the server.
func BenchmarkGroup(b *testing.B) {
const (
num = 10000
jobNum = 10
concurrency = 10
)
flushRedis()
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
ctx, cancel := context.WithCancel(context.Background())
// Create the server
srv := serverWithRedis(b)
// Waitgroup to indicate when jobs finish
var wg sync.WaitGroup
wg.Add(num * jobNum)
// handler is essentially an empty job handler.
handler := func(x []byte, ctx JobCtx) error {
wg.Done()
return nil
}
// Register the handler and enqueue the jobs.
err := srv.RegisterTask(sampleHandler, handler, TaskOpts{Concurrency: concurrency})
if err != nil {
b.Fatal(err)
}
for i := 0; i < num; i++ {
// Create a list of jobs to generate a group.
var jobs = make([]Job, jobNum)
for j := 0; j < jobNum; j++ {
jobs[j] = newJob(b)
}
group, err := NewGroup(jobs, GroupOpts{})
if err != nil {
b.Fatalf(err.Error())
}
if _, err := srv.EnqueueGroup(ctx, group); err != nil {
b.Fatalf("could not enqueue group : %v", err)
}
}
b.StartTimer()
go srv.Start(ctx)
// Wait for jobs to complete & stop the timer.
wg.Wait()
b.StopTimer()
// Cancel the context so that the server can shutdown.
cancel()
b.StartTimer()
}
}