-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_driver_test.go
55 lines (49 loc) · 1.43 KB
/
redis_driver_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
package queue_test
import (
"context"
"sync"
"testing"
queue "github.com/DoNewsCode/core-queue"
)
func setUpInProcessQueueBenchmark(wg *sync.WaitGroup) (*queue.Queue, func()) {
queueDispatcher := queue.NewQueue(queue.NewInProcessDriver())
ctx, cancel := context.WithCancel(context.Background())
go queueDispatcher.Consume(ctx)
queueDispatcher.Subscribe(queue.Listen(queue.JobFrom(1), func(ctx context.Context, Job queue.Job) error {
wg.Done()
return nil
}))
return queueDispatcher, cancel
}
func setUpRedisQueueBenchmark(wg *sync.WaitGroup) (*queue.Queue, func()) {
queueDispatcher := queue.NewQueue(&queue.RedisDriver{})
ctx, cancel := context.WithCancel(context.Background())
go queueDispatcher.Consume(ctx)
queueDispatcher.Subscribe(queue.Listen(queue.JobFrom(1), func(ctx context.Context, Job queue.Job) error {
wg.Done()
return nil
}))
return queueDispatcher, cancel
}
func BenchmarkRedisQueue(b *testing.B) {
var wg sync.WaitGroup
dispatcher, cancel := setUpRedisQueueBenchmark(&wg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(1)
dispatcher.Dispatch(context.Background(), queue.Adjust(queue.JobFrom(1)))
}
wg.Wait()
cancel()
}
func BenchmarkInProcessQueue(b *testing.B) {
var wg sync.WaitGroup
dispatcher, cancel := setUpInProcessQueueBenchmark(&wg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(1)
dispatcher.Dispatch(context.Background(), queue.Adjust(queue.JobFrom(1)))
}
wg.Wait()
cancel()
}