2016-03-14 21:37:51 +02:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
2016-04-28 01:16:11 +02:00
|
|
|
"sync"
|
2016-03-14 21:37:51 +02:00
|
|
|
"testing"
|
2016-06-25 04:15:56 +02:00
|
|
|
"time"
|
2016-03-14 21:37:51 +02:00
|
|
|
|
2019-01-16 20:54:04 +02:00
|
|
|
glog "github.com/go-log/log"
|
2018-11-21 12:13:03 +02:00
|
|
|
"github.com/google/uuid"
|
2019-11-16 12:55:11 +02:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-01-14 17:27:25 +02:00
|
|
|
"github.com/micro/go-micro/registry/memory"
|
2019-05-31 01:38:05 +02:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2016-03-14 21:37:51 +02:00
|
|
|
)
|
|
|
|
|
2019-11-16 12:55:11 +02:00
|
|
|
func newTestRegistry() registry.Registry {
|
|
|
|
return memory.NewRegistry(memory.Services(testData))
|
2019-01-14 17:27:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 04:15:56 +02:00
|
|
|
func sub(be *testing.B, c int) {
|
2019-01-16 20:54:04 +02:00
|
|
|
// set no op logger
|
|
|
|
log.SetLogger(glog.DefaultLogger)
|
|
|
|
|
2016-06-25 04:15:56 +02:00
|
|
|
be.StopTimer()
|
2019-01-14 17:27:25 +02:00
|
|
|
m := newTestRegistry()
|
|
|
|
|
2016-06-25 04:15:56 +02:00
|
|
|
b := NewBroker(Registry(m))
|
2018-11-21 12:13:03 +02:00
|
|
|
topic := uuid.New().String()
|
2016-06-25 04:15:56 +02:00
|
|
|
|
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
be.Fatalf("Unexpected init error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(); err != nil {
|
|
|
|
be.Fatalf("Unexpected connect error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
var subs []Subscriber
|
|
|
|
done := make(chan bool, c)
|
|
|
|
|
|
|
|
for i := 0; i < c; i++ {
|
2019-07-07 13:44:09 +02:00
|
|
|
sub, err := b.Subscribe(topic, func(p Event) error {
|
2016-06-25 04:15:56 +02:00
|
|
|
done <- true
|
|
|
|
m := p.Message()
|
|
|
|
|
|
|
|
if string(m.Body) != string(msg.Body) {
|
|
|
|
be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, Queue("shared"))
|
|
|
|
if err != nil {
|
|
|
|
be.Fatalf("Unexpected subscribe error: %v", err)
|
|
|
|
}
|
|
|
|
subs = append(subs, sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < be.N; i++ {
|
|
|
|
be.StartTimer()
|
|
|
|
if err := b.Publish(topic, msg); err != nil {
|
|
|
|
be.Fatalf("Unexpected publish error: %v", err)
|
|
|
|
}
|
|
|
|
<-done
|
|
|
|
be.StopTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sub := range subs {
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Disconnect(); err != nil {
|
|
|
|
be.Fatalf("Unexpected disconnect error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pub(be *testing.B, c int) {
|
2019-01-16 20:54:04 +02:00
|
|
|
// set no op logger
|
|
|
|
log.SetLogger(glog.DefaultLogger)
|
|
|
|
|
2016-06-25 04:15:56 +02:00
|
|
|
be.StopTimer()
|
2019-01-14 17:27:25 +02:00
|
|
|
m := newTestRegistry()
|
2016-06-25 04:15:56 +02:00
|
|
|
b := NewBroker(Registry(m))
|
2018-11-21 12:13:03 +02:00
|
|
|
topic := uuid.New().String()
|
2016-06-25 04:15:56 +02:00
|
|
|
|
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
be.Fatalf("Unexpected init error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(); err != nil {
|
|
|
|
be.Fatalf("Unexpected connect error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool, c*4)
|
|
|
|
|
2019-07-07 13:44:09 +02:00
|
|
|
sub, err := b.Subscribe(topic, func(p Event) error {
|
2016-06-25 04:15:56 +02:00
|
|
|
done <- true
|
|
|
|
m := p.Message()
|
|
|
|
if string(m.Body) != string(msg.Body) {
|
|
|
|
be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, Queue("shared"))
|
|
|
|
if err != nil {
|
|
|
|
be.Fatalf("Unexpected subscribe error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ch := make(chan int, c*4)
|
|
|
|
be.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < c; i++ {
|
|
|
|
go func() {
|
2019-11-01 17:07:53 +02:00
|
|
|
for range ch {
|
2016-06-25 04:15:56 +02:00
|
|
|
if err := b.Publish(topic, msg); err != nil {
|
|
|
|
be.Fatalf("Unexpected publish error: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < be.N; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
ch <- i
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
be.StopTimer()
|
|
|
|
sub.Unsubscribe()
|
|
|
|
close(ch)
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
if err := b.Disconnect(); err != nil {
|
|
|
|
be.Fatalf("Unexpected disconnect error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 21:37:51 +02:00
|
|
|
func TestBroker(t *testing.T) {
|
2019-01-14 17:27:25 +02:00
|
|
|
m := newTestRegistry()
|
2016-03-16 00:12:28 +02:00
|
|
|
b := NewBroker(Registry(m))
|
2016-03-14 21:37:51 +02:00
|
|
|
|
|
|
|
if err := b.Init(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected init error: %v", err)
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected connect error: %v", err)
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
2019-07-07 13:44:09 +02:00
|
|
|
sub, err := b.Subscribe("test", func(p Event) error {
|
2016-03-14 21:37:51 +02:00
|
|
|
m := p.Message()
|
|
|
|
|
|
|
|
if string(m.Body) != string(msg.Body) {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected subscribe error: %v", err)
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Publish("test", msg); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected publish error: %v", err)
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
sub.Unsubscribe()
|
|
|
|
|
|
|
|
if err := b.Disconnect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected disconnect error: %v", err)
|
2016-03-14 21:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-28 01:16:11 +02:00
|
|
|
|
|
|
|
func TestConcurrentSubBroker(t *testing.T) {
|
2019-01-14 17:27:25 +02:00
|
|
|
m := newTestRegistry()
|
2016-04-28 01:16:11 +02:00
|
|
|
b := NewBroker(Registry(m))
|
|
|
|
|
|
|
|
if err := b.Init(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected init error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected connect error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
var subs []Subscriber
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2019-07-07 13:44:09 +02:00
|
|
|
sub, err := b.Subscribe("test", func(p Event) error {
|
2016-04-28 01:16:11 +02:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
m := p.Message()
|
|
|
|
|
|
|
|
if string(m.Body) != string(msg.Body) {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected subscribe error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
subs = append(subs, sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Publish("test", msg); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected publish error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
for _, sub := range subs {
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Disconnect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected disconnect error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConcurrentPubBroker(t *testing.T) {
|
2019-01-14 17:27:25 +02:00
|
|
|
m := newTestRegistry()
|
2016-04-28 01:16:11 +02:00
|
|
|
b := NewBroker(Registry(m))
|
|
|
|
|
|
|
|
if err := b.Init(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected init error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Connect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected connect error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2019-07-07 13:44:09 +02:00
|
|
|
sub, err := b.Subscribe("test", func(p Event) error {
|
2016-04-28 01:16:11 +02:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
m := p.Message()
|
|
|
|
|
|
|
|
if string(m.Body) != string(msg.Body) {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected subscribe error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
if err := b.Publish("test", msg); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected publish error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
sub.Unsubscribe()
|
|
|
|
|
|
|
|
if err := b.Disconnect(); err != nil {
|
2016-06-25 04:15:56 +02:00
|
|
|
t.Fatalf("Unexpected disconnect error: %v", err)
|
2016-04-28 01:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-25 04:15:56 +02:00
|
|
|
|
|
|
|
func BenchmarkSub1(b *testing.B) {
|
|
|
|
sub(b, 1)
|
|
|
|
}
|
|
|
|
func BenchmarkSub8(b *testing.B) {
|
|
|
|
sub(b, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSub32(b *testing.B) {
|
|
|
|
sub(b, 32)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSub64(b *testing.B) {
|
|
|
|
sub(b, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSub128(b *testing.B) {
|
|
|
|
sub(b, 128)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPub1(b *testing.B) {
|
|
|
|
pub(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPub8(b *testing.B) {
|
|
|
|
pub(b, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPub32(b *testing.B) {
|
|
|
|
pub(b, 32)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPub64(b *testing.B) {
|
|
|
|
pub(b, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPub128(b *testing.B) {
|
|
|
|
pub(b, 128)
|
|
|
|
}
|