Skip to content

Commit

Permalink
test: add basic benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdawar committed Aug 11, 2024
1 parent 802d1fc commit 0f7632d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ jobs:

- name: Test
run: make test

- name: Benchmark
run: make benchmark
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
test:
go test -cover -race -count 1

benchmark:
go test -bench .
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ just test
# Or using make.
make test
```

## Benchmarks

```sh
go test -bench .
# Or Using "just".
just benchmark
# Or using make.
make benchmark
```
103 changes: 103 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package pubsub_test

import (
"context"
"strconv"
"testing"

"github.com/mdawar/pubsub"
)

func BenchmarkBrokerSubscribe(b *testing.B) {
broker := pubsub.NewBroker[string, string]()

b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Subscribe(strconv.Itoa(i))
}
}

func BenchmarkBrokerSubscribeWithCapacity(b *testing.B) {
broker := pubsub.NewBroker[string, string]()

b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.SubscribeWithCapacity(1, strconv.Itoa(i))
}
}

func BenchmarkBrokerUnsubscribe(b *testing.B) {
broker := pubsub.NewBroker[string, string]()

subs := make([]<-chan pubsub.Message[string, string], b.N)

for i := 0; i < b.N; i++ {
sub := broker.Subscribe(strconv.Itoa(i))
subs = append(subs, sub)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Unsubscribe(subs[i], strconv.Itoa(i))
}
}

func BenchmarkBrokerPublish(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
topic := "testing"

started := make(chan struct{})
done := make(chan struct{})
go func() {
events := broker.Subscribe(topic)
close(started)
for {
select {
case <-done:
return
case <-events:
}
}
}()

<-started

ctx := context.Background()

b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.Publish(ctx, topic, strconv.Itoa(i))
}
b.StopTimer()

close(done)
}

func BenchmarkBrokerTryPublish(b *testing.B) {
broker := pubsub.NewBroker[string, string]()
topic := "testing"

started := make(chan struct{})
done := make(chan struct{})
go func() {
events := broker.Subscribe(topic)
close(started)
for {
select {
case <-done:
return
case <-events:
}
}
}()

<-started

b.ResetTimer()
for i := 0; i < b.N; i++ {
broker.TryPublish(topic, strconv.Itoa(i))
}
b.StopTimer()

close(done)
}
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ _default:
# Run the tests.
test *args:
go test -race -cover -count 1 {{args}}

# Run benchmarks
benchmark *args:
go test -bench . {{args}}

0 comments on commit 0f7632d

Please sign in to comment.