introduced benchmarks on publishing messages

This commit is contained in:
2024-08-08 13:06:57 +01:00
parent 1b3ead2dd9
commit 8d5cd7e599
7 changed files with 184 additions and 13 deletions

55
async_bench_test.go Normal file
View File

@@ -0,0 +1,55 @@
package gubgub
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func BenchmarkAsyncTopic_Publish(b *testing.B) {
for _, tc := range benchTestCase {
b.Run(tc.Name, func(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
subscribersReady := make(chan struct{}, 1)
defer close(subscribersReady)
topicClosed := make(chan struct{}, 1)
defer close(topicClosed)
topic := NewAsyncTopic[int](ctx,
WithOnSubscribe(func(count int) {
if count == tc.Count {
subscribersReady <- struct{}{}
}
}),
WithOnClose(func() {
topicClosed <- struct{}{}
}),
)
for range tc.Count {
require.NoError(b, topic.Subscribe(tc.Subscriber))
}
<-subscribersReady
b.ResetTimer()
for i := range b.N {
_ = topic.Publish(i)
}
b.StopTimer()
cancel()
// This just helps leaving as few running Go routines as possible when the next round starts
<-topicClosed
})
}
}