Skip to content

Commit e5da4d6

Browse files
committed
Benchmark for pkg/pubsub package
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
1 parent 42eb82a commit e5da4d6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

pkg/pubsub/publisher_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pubsub
22

33
import (
4+
"fmt"
45
"testing"
56
"time"
67
)
@@ -61,3 +62,61 @@ func TestClosePublisher(t *testing.T) {
6162
}
6263
}
6364
}
65+
66+
const sampleText = "test"
67+
68+
type testSubscriber struct {
69+
dataCh chan interface{}
70+
ch chan error
71+
}
72+
73+
func (s *testSubscriber) Wait() error {
74+
return <-s.ch
75+
}
76+
77+
func newTestSubscriber(p *Publisher) *testSubscriber {
78+
ts := &testSubscriber{
79+
dataCh: p.Subscribe(),
80+
ch: make(chan error),
81+
}
82+
go func() {
83+
for data := range ts.dataCh {
84+
s, ok := data.(string)
85+
if !ok {
86+
ts.ch <- fmt.Errorf("Unexpected type %T", data)
87+
break
88+
}
89+
if s != sampleText {
90+
ts.ch <- fmt.Errorf("Unexpected text %s", s)
91+
break
92+
}
93+
}
94+
close(ts.ch)
95+
}()
96+
return ts
97+
}
98+
99+
func BenchmarkPubSub(b *testing.B) {
100+
for i := 0; i < b.N; i++ {
101+
b.StopTimer()
102+
p := NewPublisher(0, 1024)
103+
var subs [](*testSubscriber)
104+
for j := 0; j < 50; j++ {
105+
subs = append(subs, newTestSubscriber(p))
106+
}
107+
b.StartTimer()
108+
for j := 0; j < 1000; j++ {
109+
p.Publish(sampleText)
110+
}
111+
time.AfterFunc(1*time.Second, func() {
112+
for _, s := range subs {
113+
p.Evict(s.dataCh)
114+
}
115+
})
116+
for _, s := range subs {
117+
if err := s.Wait(); err != nil {
118+
b.Fatal(err)
119+
}
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)