Skip to content

Commit ee146f3

Browse files
committed
test: add simple benchmark
Signed-off-by: Ales Pour <[email protected]>
1 parent d45636b commit ee146f3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

unit_router_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package mqtt
2020

2121
import (
22+
"sync"
2223
"testing"
2324
"time"
2425

@@ -354,3 +355,47 @@ func Test_SharedSubscription_MatchAndDispatch(t *testing.T) {
354355
}
355356

356357
}
358+
359+
func Benchmark_MatchAndDispatch(b *testing.B) {
360+
calledback := make(chan bool, 1)
361+
362+
cb := func(c Client, m Message) {
363+
calledback <- true
364+
}
365+
366+
pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
367+
pub.TopicName = "a"
368+
pub.Payload = []byte("foo")
369+
370+
msgs := make(chan *packets.PublishPacket, 1)
371+
372+
router := newRouter()
373+
router.addRoute("a", cb)
374+
375+
var wg sync.WaitGroup
376+
wg.Add(1)
377+
378+
stopped := make(chan bool)
379+
go func() {
380+
wg.Done() // started
381+
<-router.matchAndDispatch(msgs, true, &client{oboundP: make(chan *PacketAndToken, 100)})
382+
stopped <- true
383+
}()
384+
385+
wg.Wait()
386+
b.ResetTimer()
387+
388+
for i := 0; i < b.N; i++ {
389+
msgs <- pub
390+
<-calledback
391+
}
392+
393+
close(msgs)
394+
395+
select {
396+
case <-stopped:
397+
break
398+
case <-time.After(time.Second):
399+
b.Errorf("matchAndDispatch should have exited")
400+
}
401+
}

0 commit comments

Comments
 (0)