mirror of
https://github.com/werbenhu/eventbus.git
synced 2025-09-26 20:41:48 +08:00
add benchmark test
add benchmark test
This commit is contained in:
@@ -204,3 +204,20 @@ func Test_EventBusPublish(t *testing.T) {
|
||||
wg.Wait()
|
||||
bus.Close()
|
||||
}
|
||||
|
||||
func BenchmarkEventBusPublish(b *testing.B) {
|
||||
bus := New()
|
||||
bus.Subscribe("testtopic", busHandlerOne)
|
||||
|
||||
b.ResetTimer()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for i := 0; i < b.N; i++ {
|
||||
bus.Publish("testtopic", i)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
wg.Wait()
|
||||
bus.Close()
|
||||
}
|
||||
|
27
pipe_test.go
27
pipe_test.go
@@ -106,3 +106,30 @@ func Test_PipeClose(t *testing.T) {
|
||||
assert.Equal(t, ErrChannelClosed, err)
|
||||
p.Close()
|
||||
}
|
||||
|
||||
func BenchmarkPipePublish(b *testing.B) {
|
||||
pipe := NewPipe[int]()
|
||||
|
||||
pipe.Subscribe(pipeHandlerOne)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
pipe.Publish(i)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPipeGoChannel(b *testing.B) {
|
||||
ch := make(chan int)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
val := <-ch
|
||||
pipeHandlerOne(val)
|
||||
}
|
||||
}()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
ch <- i
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user