-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
132 lines (105 loc) · 2.44 KB
/
benchmarks_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
package pubsub_test
import (
"context"
"strconv"
"testing"
"time"
"github.com/mdawar/pubsub"
)
func BenchmarkBrokerSubscribe(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Subscribe(strconv.Itoa(i))
}
}
func BenchmarkBrokerSubscribeWithCapacity(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.SubscribeWithCapacity(1, strconv.Itoa(i))
}
}
func BenchmarkBrokerUnsubscribe(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
subs := make([]<-chan pubsub.Message[string, string], 0, b.N)
for i := 0; i < b.N; i++ {
sub := broker.Subscribe(strconv.Itoa(i))
subs = append(subs, sub)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Unsubscribe(subs[i], strconv.Itoa(i))
}
}
func BenchmarkBrokerPublish(b *testing.B) {
// Number of subscriptions to benchmark.
cases := []int{0, 1, 2, 4, 6, 8, 10}
for _, count := range cases {
b.Run(strconv.Itoa(count), func(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
topic := "testing"
done := make(chan struct{})
for range count {
go func() {
msgs := broker.Subscribe(topic)
for {
select {
case <-done:
return
case <-msgs:
}
}
}()
}
ready := waitUntil(time.Second, func() bool {
return broker.Subscribers(topic) == count
})
if !ready {
b.Fatal("timed out waiting for subscriptions")
}
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Publish(ctx, topic, strconv.Itoa(i))
}
b.StopTimer()
close(done)
})
}
}
func BenchmarkBrokerTryPublish(b *testing.B) {
// Number of subscriptions to benchmark.
cases := []int{0, 1, 2, 4, 6, 8, 10}
for _, count := range cases {
b.Run(strconv.Itoa(count), func(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
topic := "testing"
done := make(chan struct{})
for range count {
go func() {
msgs := broker.Subscribe(topic)
for {
select {
case <-done:
return
case <-msgs:
}
}
}()
}
ready := waitUntil(time.Second, func() bool {
return broker.Subscribers(topic) == count
})
if !ready {
b.Fatal("timed out waiting for subscriptions")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.TryPublish(topic, strconv.Itoa(i))
}
b.StopTimer()
close(done)
})
}
}