2020-04-08 11:53:02 +03:00
|
|
|
package segmentio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-04-13 15:51:23 +03:00
|
|
|
"strings"
|
2020-04-08 11:53:02 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/broker"
|
2020-04-13 15:51:23 +03:00
|
|
|
segjson "github.com/micro/go-plugins/codec/segmentio/v2"
|
2020-04-08 11:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bm = &broker.Message{
|
|
|
|
Header: map[string]string{"hkey": "hval"},
|
|
|
|
Body: []byte("body"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPublish(t *testing.T) {
|
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:9092"}
|
|
|
|
} else {
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBroker(broker.Codec(segjson.Marshaler{}), broker.Addrs(addrs...))
|
2020-04-08 11:53:02 +03:00
|
|
|
if err := b.Connect(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := b.Disconnect(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-13 15:51:23 +03:00
|
|
|
done := make(chan bool, 1)
|
2020-04-08 11:53:02 +03:00
|
|
|
fn := func(msg broker.Event) error {
|
2020-04-13 15:51:23 +03:00
|
|
|
done <- true
|
2020-04-08 11:53:02 +03:00
|
|
|
return msg.Ack()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := b.Subscribe("test_topic", fn, broker.Queue("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sub.Unsubscribe(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := b.Publish("test_topic", bm); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
<-done
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSegmentioPublish(b *testing.B) {
|
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
b.Skip()
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:9092"}
|
|
|
|
} else {
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
brk := NewBroker(broker.Codec(segjson.Marshaler{}), broker.Addrs(addrs...))
|
2020-04-08 11:53:02 +03:00
|
|
|
if err := brk.Connect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := brk.Disconnect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSegmentioSubscribe(b *testing.B) {
|
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
b.Skip()
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
|
|
addrs = []string{"127.0.0.1:9092"}
|
|
|
|
} else {
|
|
|
|
addrs = strings.Split(addr, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
brk := NewBroker(broker.Codec(segjson.Marshaler{}), broker.Addrs(addrs...))
|
2020-04-08 11:53:02 +03:00
|
|
|
if err := brk.Connect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := brk.Disconnect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cnt := 0
|
|
|
|
done := make(chan struct{})
|
|
|
|
fn := func(msg broker.Event) error {
|
|
|
|
if cnt == 0 {
|
|
|
|
b.ResetTimer()
|
|
|
|
}
|
|
|
|
cnt++
|
2020-04-13 15:51:23 +03:00
|
|
|
if cnt == b.N {
|
2020-04-08 11:53:02 +03:00
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
return msg.Ack()
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-08 11:53:02 +03:00
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := brk.Subscribe("test_topic", fn, broker.Queue("test"))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sub.Unsubscribe(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func BenchmarkSaramaPublish(b *testing.B) {
|
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
b.Skip()
|
|
|
|
}
|
|
|
|
brk := sarama.NewBroker(broker.Addrs("127.0.0.1:9092"))
|
|
|
|
if err := brk.Connect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := brk.Disconnect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func BenchmarkSaramaSubscribe(b *testing.B) {
|
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
b.Skip()
|
|
|
|
}
|
|
|
|
brk := sarama.NewBroker(broker.Addrs("127.0.0.1:9092"))
|
|
|
|
if err := brk.Connect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := brk.Disconnect(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cnt := 0
|
|
|
|
done := make(chan struct{})
|
|
|
|
fn := func(msg broker.Event) error {
|
|
|
|
if cnt == 0 {
|
|
|
|
b.ResetTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt++
|
|
|
|
if cnt == 10000 {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
return msg.Ack()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := brk.Subscribe("test_topic", fn, broker.Queue("test"))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sub.Unsubscribe(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
*/
|