add benchmark test

add benchmark test
This commit is contained in:
werben
2023-04-25 18:18:13 +08:00
parent ff1d36d012
commit 31fbda00bf
2 changed files with 44 additions and 0 deletions

View File

@@ -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()
}

View File

@@ -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
}
}