2020-04-15 04:07:17 +03:00
|
|
|
package segmentio_test
|
2020-04-08 11:53:02 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-04-13 15:51:23 +03:00
|
|
|
"strings"
|
2020-04-15 04:07:17 +03:00
|
|
|
"sync/atomic"
|
2020-04-08 11:53:02 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/broker"
|
2020-04-15 04:07:17 +03:00
|
|
|
sarama "github.com/micro/go-plugins/broker/kafka/v2"
|
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
|
|
|
)
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
func BenchmarkSaramaCodecJsonPublish(b *testing.B) {
|
|
|
|
b.Skip()
|
2020-04-08 11:53:02 +03:00
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
2020-04-15 04:07:17 +03:00
|
|
|
b.Skip()
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
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, ",")
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
brk := sarama.NewBroker(broker.Addrs(addrs...))
|
|
|
|
if err := brk.Connect(); err != nil {
|
|
|
|
b.Fatal(err)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
defer func() {
|
2020-04-15 04:07:17 +03:00
|
|
|
if err := brk.Disconnect(); err != nil {
|
|
|
|
b.Fatal(err)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
b.ResetTimer()
|
2020-04-08 11:53:02 +03:00
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
func BenchmarkSaramaCodecSegmentioPublish(b *testing.B) {
|
2020-04-08 11:53:02 +03:00
|
|
|
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, ",")
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
brk := sarama.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
func BenchmarkSaramaCodecJsonSubscribe(b *testing.B) {
|
|
|
|
b.Skip()
|
2020-04-08 11:53:02 +03:00
|
|
|
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, ",")
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
brk := sarama.NewBroker(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
|
2020-04-15 04:07:17 +03:00
|
|
|
var done atomic.Value
|
|
|
|
done.Store(false)
|
|
|
|
exit := make(chan struct{})
|
2020-04-08 11:53:02 +03:00
|
|
|
fn := func(msg broker.Event) error {
|
|
|
|
if cnt == 0 {
|
|
|
|
b.ResetTimer()
|
|
|
|
}
|
|
|
|
cnt++
|
2020-04-15 04:07:17 +03:00
|
|
|
if cnt >= b.N {
|
|
|
|
if v, ok := done.Load().(bool); ok && !v {
|
|
|
|
done.Store(true)
|
|
|
|
close(exit)
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
return msg.Ack()
|
|
|
|
}
|
2020-04-13 15:51:23 +03:00
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
go func() {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if v := done.Load().(bool); v {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
2020-04-15 04:07:17 +03:00
|
|
|
}()
|
2020-04-08 11:53:02 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
2020-04-15 04:07:17 +03:00
|
|
|
<-exit
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
func BenchmarkSaramaCodecSegmentioSubscribe(b *testing.B) {
|
2020-04-08 11:53:02 +03:00
|
|
|
if tr := os.Getenv("TRAVIS"); len(tr) > 0 {
|
|
|
|
b.Skip()
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +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, ",")
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
brk := sarama.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
|
2020-04-15 04:07:17 +03:00
|
|
|
var done atomic.Value
|
|
|
|
done.Store(false)
|
|
|
|
exit := make(chan struct{})
|
2020-04-08 11:53:02 +03:00
|
|
|
fn := func(msg broker.Event) error {
|
|
|
|
if cnt == 0 {
|
|
|
|
b.ResetTimer()
|
|
|
|
}
|
|
|
|
cnt++
|
2020-04-15 04:07:17 +03:00
|
|
|
if cnt >= b.N {
|
|
|
|
if v, ok := done.Load().(bool); ok && !v {
|
|
|
|
done.Store(true)
|
|
|
|
close(exit)
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
return msg.Ack()
|
|
|
|
}
|
|
|
|
|
2020-04-15 04:07:17 +03:00
|
|
|
go func() {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if v := done.Load().(bool); v {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := brk.Publish("test_topic", bm); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
2020-04-15 04:07:17 +03:00
|
|
|
}()
|
2020-04-08 11:53:02 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
2020-04-15 04:07:17 +03:00
|
|
|
<-exit
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|