-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added benchmarks, PublishBatchUntilAcked for publishing many messages…
…, slight refactoring of Consumer to take the connection on constructor
- Loading branch information
Showing
7 changed files
with
401 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,8 @@ coverage-browser: | |
|
||
update-readme-badge: | ||
@go tool cover -func=$(COVERAGE_PATH) -o=$(COVERAGE_PATH).badge | ||
@go run github.com/AlexBeauchemin/[email protected] -filename=$(COVERAGE_PATH).badge | ||
@go run github.com/AlexBeauchemin/[email protected] -filename=$(COVERAGE_PATH).badge | ||
|
||
# pkg.go.dev documentation is updated via go get | ||
update-proxy-cache: | ||
@GOPROXY=https://proxy.golang.org go get github.com/danlock/rmq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
//go:build rabbit | ||
|
||
package rmq_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/danlock/rmq" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
func generatePublishings(num int, routingKey string) []rmq.Publishing { | ||
publishings := make([]rmq.Publishing, 100) | ||
for i := range publishings { | ||
publishings[i] = rmq.Publishing{ | ||
RoutingKey: routingKey, | ||
Mandatory: true, | ||
Publishing: amqp.Publishing{ | ||
Body: []byte(fmt.Sprintf("%d.%d", i, time.Now().UnixNano())), | ||
}, | ||
} | ||
} | ||
return publishings | ||
} | ||
|
||
func BenchmarkPublishAndConsumeMany(b *testing.B) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
randSuffix := fmt.Sprintf("%d.%p", time.Now().UnixNano(), b) | ||
|
||
queueName := "BenchmarkPublishAndConsumeMany" + randSuffix | ||
baseCfg := rmq.CommonConfig{Log: slog.Log} | ||
topology := rmq.Topology{ | ||
CommonConfig: baseCfg, | ||
Queues: []rmq.Queue{{ | ||
Name: queueName, | ||
Args: amqp.Table{ | ||
amqp.QueueTTLArg: time.Minute.Milliseconds(), | ||
}, | ||
}}, | ||
} | ||
|
||
subRMQConn := rmq.ConnectWithURL(ctx, rmq.ConnectConfig{CommonConfig: baseCfg, Topology: topology}, os.Getenv("TEST_AMQP_URI")) | ||
pubRMQConn := rmq.ConnectWithURL(ctx, rmq.ConnectConfig{CommonConfig: baseCfg, Topology: topology}, os.Getenv("TEST_AMQP_URI")) | ||
|
||
consumer := rmq.NewConsumer(subRMQConn, rmq.ConsumerConfig{ | ||
CommonConfig: baseCfg, | ||
Queue: topology.Queues[0], | ||
}) | ||
|
||
publisher := rmq.NewPublisher(ctx, pubRMQConn, rmq.PublisherConfig{ | ||
CommonConfig: baseCfg, | ||
LogReturns: true, | ||
}) | ||
|
||
publisher2, publisher3 := rmq.NewPublisher(ctx, pubRMQConn, rmq.PublisherConfig{ | ||
CommonConfig: baseCfg, | ||
LogReturns: true, | ||
}), rmq.NewPublisher(ctx, pubRMQConn, rmq.PublisherConfig{ | ||
CommonConfig: baseCfg, | ||
LogReturns: true, | ||
}) | ||
|
||
dot := []byte(".") | ||
errChan := make(chan error) | ||
consumeChan := consumer.Consume(ctx) | ||
|
||
publishings := generatePublishings(10000, queueName) | ||
|
||
cases := []struct { | ||
name string | ||
publishFunc func(b *testing.B) | ||
}{ | ||
{ | ||
"PublishBatchUntilAcked", | ||
func(b *testing.B) { | ||
if err := publisher.PublishBatchUntilAcked(ctx, 0, publishings...); err != nil { | ||
b.Fatalf("PublishBatchUntilAcked err %v", err) | ||
} | ||
}, | ||
}, | ||
{ | ||
"PublishBatchUntilAcked into thirds", | ||
func(b *testing.B) { | ||
errChan := make(chan error) | ||
publishers := []*rmq.Publisher{publisher, publisher, publisher} | ||
for i := range publishers { | ||
go func(i int) { | ||
errChan <- publishers[i].PublishBatchUntilAcked(ctx, 0, publishings[i:i+1]...) | ||
}(i) | ||
} | ||
successes := 0 | ||
for { | ||
select { | ||
case err := <-errChan: | ||
if err != nil { | ||
b.Fatalf("PublishBatchUntilAcked err %v", err) | ||
} | ||
successes++ | ||
if successes == len(publishers) { | ||
return | ||
} | ||
case <-ctx.Done(): | ||
b.Fatalf("PublishBatchUntilAcked timed out") | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
"PublishBatchUntilAcked on three Publishers", | ||
func(b *testing.B) { | ||
errChan := make(chan error) | ||
publishers := []*rmq.Publisher{publisher, publisher2, publisher3} | ||
for i := range publishers { | ||
go func(i int) { | ||
errChan <- publishers[i].PublishBatchUntilAcked(ctx, 0, publishings[i:i+1]...) | ||
}(i) | ||
} | ||
successes := 0 | ||
for { | ||
select { | ||
case err := <-errChan: | ||
if err != nil { | ||
b.Fatalf("PublishBatchUntilAcked err %v", err) | ||
} | ||
successes++ | ||
if successes == len(publishers) { | ||
return | ||
} | ||
case <-ctx.Done(): | ||
b.Fatalf("PublishBatchUntilAcked timed out") | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
"Concurrent PublishUntilAcked", | ||
func(b *testing.B) { | ||
errChan := make(chan error) | ||
for i := range publishings { | ||
go func(i int) { | ||
errChan <- publisher.PublishUntilAcked(ctx, 0, publishings[i]) | ||
}(i) | ||
} | ||
successes := 0 | ||
for { | ||
select { | ||
case err := <-errChan: | ||
if err != nil { | ||
b.Fatalf("PublishUntilAcked err %v", err) | ||
} | ||
successes++ | ||
if successes == len(publishings) { | ||
return | ||
} | ||
case <-ctx.Done(): | ||
b.Fatalf("PublishUntilAcked timed out") | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, bb := range cases { | ||
b.Run(bb.name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
go func(i int) (err error) { | ||
received := make(map[uint64]struct{}, len(publishings)) | ||
defer func() { errChan <- err }() | ||
for { | ||
select { | ||
case msg := <-consumeChan: | ||
rawIndex := bytes.Split(msg.Body, dot)[0] | ||
index, err := strconv.ParseUint(string(rawIndex), 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("strconv.ParseUint err %w", err) | ||
} | ||
received[index] = struct{}{} | ||
if err := msg.Ack(false); err != nil { | ||
return fmt.Errorf("msg.Ack err %w", err) | ||
} | ||
if len(received) == len(publishings) { | ||
return nil | ||
} | ||
case <-ctx.Done(): | ||
return fmt.Errorf("timed out after consuming %d publishings on bench run %d", len(received), i) | ||
} | ||
} | ||
}(i) | ||
|
||
if err := publisher.PublishBatchUntilAcked(ctx, 0, publishings...); err != nil { | ||
b.Fatalf("PublishBatchUntilAcked err %v", err) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
b.Fatalf("timed out on bench run %d", i) | ||
case err := <-errChan: | ||
if err != nil { | ||
b.Fatalf("on bench run %d consumer err %v", i, err) | ||
|
||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.